Chapter 4 Functions
4.3 Input ArgumentsΒΆ
As introduced earlier, the passing of arguments to a function is defined by the def statement, i.e. each parameter in the function is assigned the corresponding input argument value in the function call.
In the example above, when the function call expression h(1, 2) is evaluated, the assignments x = 1 and y = a are executed before the function body is executed, leading to the result values x = 1 and y = 2.
Default arguments can also be defined for def functions.
In the example above, the function sum can take two or three input arguments. In the former case, the parameter c is assigned the default argument 0, and the return value a + b + c is equal to the sum of a and b.
Note in the example above that the name of the custom function, sum, is the same as a built-in function in Python. When defined, the custom sum function overrides the built-in function.
© Copyright 2024 GS Ng.