Python 3 - Functions
Python 3 - Functions
Python 3 - Functions
A function is a block of organized, reusable code that is used to perform a single, related action.
Functions provide better modularity for your application and a high degree of code reusing.
As you already know, Python gives you many built-in functions like print(), etc. but you can also
create your own functions. These functions are called user-defined functions.
Defining a Function
You can define functions to provide the required functionality. Here are simple rules to define a
function in Python.
Function blocks begin with the keyword def followed by the function name and parentheses ( ( )
).
Any input parameters or arguments should be placed within these parentheses. You can also
define parameters inside these parentheses.
The first statement of a function can be an optional statement - the documentation string of the
function or docstring.
The code block within every function starts with a colon (:) and is indented.
The statement return [expression] exits a function, optionally passing back an expression to the
caller. A return statement with no arguments is the same as return None.
Syntax
def functionname( parameters ):
"function_docstring"
function_suite
return [expression]
By default, parameters have a positional behavior and you need to inform them in the same order
that they were defined.
Example
The following function takes a string as input parameter and prints it on standard screen.
https://www.tutorialspoint.com/python3/python_functions.htm 1/9
17.01.2023 17:14 Python 3 - Functions
print (str)
return
Calling a Function
Defining a function gives it a name, specifies the parameters that are to be included in the function
and structures the blocks of code.
Once the basic structure of a function is finalized, you can execute it by calling it from another
function or directly from the Python prompt. Following is an example to call the printme() function −
Live Demo
#!/usr/bin/python3
AD
Live Demo
#!/usr/bin/python3
https://www.tutorialspoint.com/python3/python_functions.htm 2/9
17.01.2023 17:14 Python 3 - Functions
mylist[2]=50
print ("Values inside the function after change: ", mylist)
return
Here, we are maintaining reference of the passed object and appending values in the same object.
Therefore, this would produce the following result −
There is one more example where argument is being passed by reference and the reference is
being overwritten inside the called function.
Live Demo
#!/usr/bin/python3
The parameter mylist is local to the function changeme. Changing mylist within the function does
not affect mylist. The function accomplishes nothing and finally this would produce the following
result −
Function Arguments
You can call a function by using the following types of formal arguments −
Required arguments
Keyword arguments
https://www.tutorialspoint.com/python3/python_functions.htm 3/9
17.01.2023 17:14 Python 3 - Functions
Default arguments
Variable-length arguments
AD
Required Arguments
Required arguments are the arguments passed to a function in correct positional order. Here, the
number of arguments in the function call should match exactly with the function definition.
To call the function printme(), you definitely need to pass one argument, otherwise it gives a syntax
error as follows −
Live Demo
#!/usr/bin/python3
Keyword Arguments
Keyword arguments are related to the function calls. When you use keyword arguments in a function
call, the caller identifies the arguments by the parameter name.
This allows you to skip arguments or place them out of order because the Python interpreter is able
to use the keywords provided to match the values with parameters. You can also make keyword
calls to the printme() function in the following ways −
Live Demo
#!/usr/bin/python3
My string
The following example gives a clearer picture. Note that the order of parameters does not matter.
Live Demo
#!/usr/bin/python3
Name: miki
Age 50
Default Arguments
A default argument is an argument that assumes a default value if a value is not provided in the
function call for that argument. The following example gives an idea on default arguments, it prints
default age if it is not passed −
Live Demo
#!/usr/bin/python3
https://www.tutorialspoint.com/python3/python_functions.htm 5/9
17.01.2023 17:14 Python 3 - Functions
Name: miki
Age 50
Name: miki
Age 35
Variable-length Arguments
You may need to process a function for more arguments than you specified while defining the
function. These arguments are called variable-length arguments and are not named in the function
definition, unlike required and default arguments.
An asterisk (*) is placed before the variable name that holds the values of all nonkeyword variable
arguments. This tuple remains empty if no additional arguments are specified during the function
call. Following is a simple example −
Live Demo
#!/usr/bin/python3
https://www.tutorialspoint.com/python3/python_functions.htm 6/9
17.01.2023 17:14 Python 3 - Functions
Output is:
10
Output is:
70
60
50
Lambda forms can take any number of arguments but return just one value in the form of an
expression. They cannot contain commands or multiple expressions.
An anonymous function cannot be a direct call to print because lambda requires an expression.
Lambda functions have their own local namespace and cannot access variables other than
those in their parameter list and those in the global namespace.
Although it appears that lambdas are a one-line version of a function, they are not equivalent to
inline statements in C or C++, whose purpose is to stack allocation by passing function, during
invocation for performance reasons.
Syntax
The syntax of lambda functions contains only a single statement, which is as follows −
Live Demo
#!/usr/bin/python3
Value of total : 30
Value of total : 40
https://www.tutorialspoint.com/python3/python_functions.htm 7/9
17.01.2023 17:14 Python 3 - Functions
All the examples given below are not returning any value. You can return a value from a function as
follows −
Live Demo
#!/usr/bin/python3
Scope of Variables
All variables in a program may not be accessible at all locations in that program. This depends on
where you have declared a variable.
The scope of a variable determines the portion of the program where you can access a particular
identifier. There are two basic scopes of variables in Python −
Global variables
Local variables
This means that local variables can be accessed only inside the function in which they are declared,
whereas global variables can be accessed throughout the program body by all functions. When you
call a function, the variables declared inside it are brought into scope. Following is a simple example
−
Live Demo
https://www.tutorialspoint.com/python3/python_functions.htm 8/9
17.01.2023 17:14 Python 3 - Functions
#!/usr/bin/python3
https://www.tutorialspoint.com/python3/python_functions.htm 9/9