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.
Line | Description |
---|---|
1 | Defines 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 |
2 | Checks if a is greater than b |
3 | Outputs the string that number a is greater than number b |
4 | Checks if a is smaller than b |
5 | Outputs the string that number a is smaller than number b |
7 | Calls the function max() with parameters 8 and 6 |
8 | Calls the function max() with parameters 234.2 and 253.1 |
9 | Calls 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)
8 > 6
234.2 < 253.1
65 < 65.3
Line | Description |
---|---|
1 | Defines the function add() with the input parameters a and b . The default value of the variables a and b is 10 and 2 |
2 | Returns the sum of a and b |
4 | Defines the function sub() with the input parameters a and b . The default value of the variables a and b is 10 and 2 |
5 | Returns the result of subtraction of a and b |
7 | Defines the function mul() with the input parameters a and b . The default value of the variables a and b is 10 and 2 |
8 | Returns the result of multiplying a and b |
10 | Defines the function div() with the input parameters a and b . The default value of the variables a and b is 10 and 2 |
11 | Returns the result of the division of a and b |
14 – 17 | Calls and outputs the functions without parameters. So the default values are used for the calculations |
20 – 23 | Calls 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))
12
8
20
5.0
12
1
20
6.0
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