1.2.1.1 PCEP-30-02 Practice Test Compendium – Functions
Functions
01) A function is a named, separate part of the code that can be activated on demand. A function can perform an action, or return a result, or both.
02) The simplest function, which does nothing and returns no result, can be defined in the following way:
03) Activating a function is done by the function invocation (function call). The lazy()
function defined above can be invoked by the following clause:
04) Function definition must precede its invocation. Breaking this rule raises the NameError
exception.
05) A function can be equipped with an arbitrary number of parameters. The parameters behave like variables known inside the function only, and their values are set during the invocation. The invocation must provide as many arguments as needed to initialize all parameters. Breaking this rule results in raising the TypeError
exception.
06) If a function is supposed to evaluate a result, it must perform the return
expression instruction, which immediately terminates function execution and causes the function to return the expression value to the invoker. If the function does not execute the instruction, or utilizes return without an expression, the None value is returned implicitly. For example, the following snippet prints True None
to the screen:
07) 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 True False
to the screen:
08) The positional parameter passing technique is a technique based on the assumption that the arguments are associated with the parameters based upon their position (i.e. the first argument value goes to the first parameter, and so on) For example, the following snippet outputs 1 2 3
to the screen:
09) The keyword parameter passing technique is a technique based on the assumption that the arguments are associated with the parameters based upon the parameter's names, which must be explicitly specified during the invocation. For example, the following snippet outputs 1 2 3
to the screen:
Last updated