Function parameters

The terms parameter and argument are often used synonymously. Parameters are variables that are declared in the function signature. Arguments are values that are used when the function is called. Parameters are declared in round brackets of the function signature. The parameters generally dictate how a function must be called.

In programming languages, a distinction is made between two main forms of parameter passing: call-by-value and call-by-reference. Python uses the mechanism of call-by-reference. The more accurate expression is actually call-by-object-reference. When calling a function, not pointers to variables but pointers to the underlying objects are passed.

How many parameters are passed to a method is up to the programmer and depends on the method definition. In Python, these parameters are called positional. The instances passed in the method call are assigned to the parameters according to their position in the parameter list.

In addition to the position-related parameters, there are also the keyword arguments. Keyword parameters are directly associated with the formal parameter name, and their order in the parameter list no longer matters.

It is also possible to mix position and keyword related parameters, but all keyword parameters must be at the end of the parameter list.

Code Explanation

LineDescription
1Defines the function max() with the input parameters a and b. The data type does not matter. The function can be called with parameters of different types
2Checks if a is greater than b
3Outputs the string that number a is greater than number b
4Checks if a is smaller than b
5Outputs the string that number a is smaller than number b
7Calls the function max() with parameters 8 and 6
8Calls the function max() with parameters 234.2 and 253.1
9Calls the function max() with parameters 65 and 65.3
def max(a, b):
    if a > b:
        print(repr(a) + " > " + repr(b))
    if a < b:
        print(repr(a) + " < " + repr(b))

max(8, 6)
max(234.2, 253.1)
max(65, 65.3)
Output
8 > 6
234.2 < 253.1
65 < 65.3

Another Example

Code Explanation

LineDescription
1Defines the function add() with the input parameters a and b. The default value of the variables a and b is 10 and 2
2Returns the sum of a and b
4Defines the function sub() with the input parameters a and b. The default value of the variables a and b is 10 and 2
5Returns the result of subtraction of a and b
7Defines the function mul() with the input parameters a and b. The default value of the variables a and b is 10 and 2
8Returns the result of multiplying a and b
10Defines the function div() with the input parameters a and b. The default value of the variables a and b is 10 and 2
11Returns the result of the division of a and b
14 – 17Calls and outputs the functions without parameters. So the default values are used for the calculations
20 – 23Calls and outputs the functions with different parameters
def add(a = 10, b = 2):
    return a + b

def sub(a = 10, b = 2):
    return a - b

def mul(a = 10, b = 2):
    return a * b

def div(a = 10, b = 2):
    return a / b

# default function values
print(add())
print(sub())
print(mul())
print(div())

# custom function values
print(add(6, 6))
print(sub(4, 3))
print(mul(4, 5))
print(div(36, 6))
Output
12
8
20
5.0
12
1
20
6.0

Keyword arguments

def display(a, b, c, d):
    print("a: " + str(a)
          + " b: " + str(b)
          + " c: " + str(c)
          + " d: " + str(d))


display(b=20, d=40, a=10, c=30)
Output
a: 10 b: 20 c: 30 d: 40