Python programming function definition and identifier scope
Functions can be defied in a Python program using either one of these methods: Lambda Expression or def function declaration.
One of these methods can be preferred depending on the requirement of the function. If the function requires more logical operation before reach its goal, it's better to use def function, but if the function can reach its intended goal with one statement, it's advised to use lambda expression.
Lambda Expression function declaration
Declaring function using lambda expression is very simple, because lambda expression syntax is straightforward. This is the syntax for function declaration using lambda expression: lambda <argument list> : <return value expression>.
As you can see above that is only required to define function using lambda expression, which returns its expression value automatically, so there is no need to provide the return expression explicitly.
Example of lambda expression in action:
f = lambda x, y : x +y
The variable f will be used to invoke the function. Our lambda expression takes two arguments, but it can take zero to any number of arguments. In this function will take two arguments, and calculates the summation of two numbers. The result is automatically returned.
Example of lambda expression invocation:
f(2,2); this function call will return 4 as the result.
Default arguments and keyword argument
One of the unique feature about Python functions is that they can be arguments with default values. This feature allows Python functions to be invoked without specifying argument values. Python is going to use the specified default values for arguments that are not explicitly set, when function is invoked.
Example of function declaration with default argument:
f = lambda x, y = 20 : x + y
This function can be invoked in one of these two ways: the first one is by specifying both arguments values, and the second one is by specifying only the first argument; python will use the default value for the second argument.
Invocation 1: f(10, 10); the result of this invocation will be 20, because x = 10 and y =10.
Invocation 2: f(10); the result of this invocation will be 30, because x = 10, and y will use the default argument value, which is 20.
Function can have more than one default argument, and default arguments must be placed after non-default arguments in the function definition.
This is the default argument with default value set, and is explicitly set during the function call in order for the preceding default argument to use its default value.
Example of keyword argument in action:
f = lambda x, y = 1, z =20 : x + y + z
This function has two default arguments, but in our invocation we need to use only the default value of y, which is 1, and specify the value of z. To do this, we will use the keyword argument.
f(10, z=10); the result for this will be 21, because x = 10, y = 1, which is the default value, and z = 10, which was explicitly set to 10.
Def function declaration
We can also define functions in a Python program using the def keyword. In this declaration we start with the def keyword followed by the function name, followed by brackets - if arguments are available, they are specified inside the brackets, followed by colon symbol, and last followed by the function body - contains all function functionality.
Example of def function declaration:
This function will print hello, world to the user when invoked. All operations that can be applied to the lambda expression function can also be applied to the function defined using def keyword. This includes default arguments and keyword arguments.
The main advantage of using def function declaration over lambda expression function is function body of def functions can take more than on statement, while in other hand the body of lambda expression takes only one statement.
Identifier scope in Python
Identifier scope in Python means the accessibility of identifier in the Python code. Python provides two scopes: global scope and local scope.
The different between these two scopes is that any identifier defined in a global scope is accessible in the entire code includes statement in local scope, but identifiers defined in a local scope is only accessible in the scope in which it's defined. Example of this is, when identifier is defined inside a function, the identifier will only be accessible inside that function.