1.2.1.2 PCEP-30-02 Practice Test Compendium β Functions
Functions β continued
10) A function definition can declare default values for some or all of its parameters. When the invocation does not provide arguments for these parameters, the default values are taken into consideration. Note: parameters with default values must not precede the ones without them. For example, the following snippet prints 1 2 3
to the screen:
11) Note that the following invocation is incorrect and will raise the TypeError
exception, because the a parameter is set twice (once with the positional passing and once with the keyword passing) while the c
parameter is not set at all.
12) A scope is the part of the code where a certain name is properly recognizable.
13) A variable existing outside a function has a scope which includes the function's bodies.
14) A variable defined inside the function has a scope inside the function's body only.
15) If a certain variable is used inside a function and the variableβs name is listed as an argument of the global
keyword, it has global scope, and it is also recognizable outside the function. For example, the following snippet outputs 2
to the screen:
Note: removing the line containing the global
keyword will spoil the code and the UnboundLocalError
exception will be raised.
16) Changing the parameter's value doesn't propagate it outside the function. For example, the following snippet outputs [1]
to the screen:
17) If the parameter is a list or a dictionary, changing its contents propagates them outside the function. For example, the following snippet outputs [2]
to the screen:
18) Recursion is a programming technique in which the function invokes itself to perform a complex task. For example, the following snippet contains a function that evaluates the factorial of its argument and prints 120
to the screen:
Last updated