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 Example

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)
8 > 6
234.2 < 253.1
65 < 65.3
Code Explanation

This code defines a function named max that takes two arguments, a and b.

The function first checks if a is greater than b using the if statement. If this condition is true, the function prints a string representation of a, the > operator, and a string representation of b, which indicates that a is greater than b.

Next, the function checks if a is less than b using another if statement. If this condition is true, the function prints a string representation of a, the < operator, and a string representation of b, which indicates that a is less than b.

The function is then called three times, passing 8 and 6 as the first set of arguments, 234.2 and 253.1 as the second set, and 65 and 65.3 as the third set.

Another Example

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

# custom function values
print(add(6, 6))
print(sub(4, 3))
print(mul(4, 5))
print(div(36, 6))
Code Explanation

The code defines four functions: add, sub, mul, and div. Each function takes two arguments, a and b, with default values of 10 and 2, respectively.

The add function returns the sum of a and b. The sub function returns the difference between a and b. The mul function returns the product of a and b. The div function returns the result of dividing a by b.

Next, the code prints the results of calling each function with the default arguments and then with custom arguments.

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)
a: 10 b: 20 c: 30 d: 40
Code Explanation

The display() function takes 4 parameters, a, b, c, and d. The function then prints out each of these parameters along with their respective names.

In the example, the function is called with the values of the parameters specified in a different order from their declaration in the function definition. This is possible because in Python, the arguments in a function call can be specified by keyword, meaning that the parameter name is explicitly stated along with its value.

So, in the call display(b=20, d=40, a=10, c=30), the values of a and b are explicitly specified, so they take on the values 10 and 20, respectively. Similarly, the values of c and d are explicitly set to 30 and 40, respectively.