By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Python has many in-built functions and the popular print () function is one of those functions. Similarly, we can create our own functions known as user defined functions (UDF).
A function may be defined as a block of code that is well organized and reusable multiple times to perform a number of operations in a program that demands high modularity and reusability.
Syntax of a Python function
def functionname ( parameters or arguments ): "docstring" Block of code or indented statements return [expression]
Rules to define a Python function - A function block has a structure which starts with keyword def followed by the name of function and parentheses (). - Parentheses can have number of input arguments or parameters within it. Also, we can define values for these parameters inside these parentheses. - Python function can have its first statement as an optional statement known as the docstring or the documentation string of that function. - A code block within a Python function starts with a colon ( : ) and all statements in the block are indented. - A Python function ends with return [expression], this exits a function returning an expression value and control to the caller. A return may or may not return an expression value.
Calling a Python function Python function can be called anywhere in the program with function name and passing the values to its parameters. This is to be made sure that we should send equal number of values to equal number of parameters i.e. if there is one parameter in a function then we just need to send one value, similarly if there are two parameters then we need to send two values and so on. Such type of parameters are also known as required arguments to a function.
Below is an example that demonstrates calling of a Python function.
Passing by reference vs Passing by value All parameters or arguments in Python are passed by reference. Therefore, if we change a parameter that refers to within a function, this change will be reflected in the actual calling function as shown in the below example. In the second case, although we have passed arguments by reference the change of list overwritten is visible only in the local scope (parameter elelist) of the function yet outside the function the actual list remains unaffected as shown in the above example for listFunction2.
Python function arguments A function in Python can be called with the following types of formal arguments. - Required arguments: In case of the required arguments, the arguments to a function are passed in correct positional order. Therefore, the number of arguments defined in a function should match exactly the number of arguments passed to that function. If there is mismatch, system will throw an error as shown in the below example. - Keyword arguments: In python, when we use keyword argument in a function call then the caller identifies the arguments by its name. This feature of Python, provides the flexibility to place the arguments out of order as Python interpreter uses the keywords to match the values with parameters. This is demonstrated in the below example. Here we have entered arguments which are out of sequence with parameter but interpreter by using the keywords processed them correctly. - Default arguments: Python has a feature of the default arguments. Default argument assumes a default value if a value is defaulted or not provided in the function call for that argument. This is demonstrated in the below example.
- Variable-length arguments: Python supports the feature of specifying variable length arguments. These arguments are not named in the function definition as compared to required and default arguments. The syntax and example for variable-length arguments are shown below.
def functionname([formal_args,] *var_args_tuple ): "docstring" Block of code or statements return [expression]
In the below example, during the first function call when single argument is passed, it just printed that argument and variable-length argument remained untouched. In the next example, when three arguments are passed then variable-length argument parsed the additional arguments and helped program to print them.
Python Anonymous Functions When function in Python are not declared in the standard manner using def keyword but with use of lambda keyword are known as Anonymous functions in Python.
Syntax is shown below:
Syntax
lambda [arg1 [,arg2,.....argn]]:expression
Below are the features of the anonymous function. - Lambda can accept any number of arguments but return just one value as expression. They do not contain multiple expressions or commands. - Since lambda requires an expression therefore, anonymous function cannot make a direct call to print. - Lambda functions can have their own local namespace but cannot access variables other than those present in their parameter list and in the global namespace.
Below is an example of anonymous function.
Python return Statement Using return statement in Python, we can return a value as expression and the control back to the caller.
Below is an example.
Scope of Variables in Python There are two scopes of variables defined in Python. - Global Variables: These are the variables defined outside the function body and can be accessed from anywhere within the program. - Local Variables: These are the variables defined inside the function body and have a local scope of accessibility.
Below is the example to demonstrate local and global scope of a variable in Python. In the above example, the variable total present inside the function has a local scope and the variable total declared at the top of the program has global scope. When both of the variable values printed shows the different result due to their accessibility scope.
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.