In this section, you will learn
- What is a function
- How to create a function
- Types of functions
What is Python Function
A function is a small block of a program which contains a number of statements to perform a specific task. When you have a program of thousands of lines performing different tasks then you should divide the program into small modules (blocks) which increases the readability and lowers the complexity.
How to Define a Python Function
The following is the syntax to define a function:
def functionName(arguments): """This is the definition of this function""" statements return returnParam
- The keyword
defis used to define a function.
functionNameis the name of the function.
argumentsare optional. Arguments provide values to function to perform operations on.
- Colon (
:) ends the function header.
"""This is the definition of this function"""is a
docstringand is optional which describes what the function does.
statementsrefers to the body of the function.
- The statement
returnoptionally returns the result to the caller.
Python Function Example:
def language(p): """Function to print a message""" print("Programming language:", p)
Here a function
language is defined which has one argument
p passing from the caller. Inside the function, there is a docstring and a
Call a Function in Python:
A function can be called from anywhere in the program. A function can be called by its name with the required parameters.
Programming language: Python
return statement transfers the control back to the codes where the function is called.
It indicates the ending of the function definition.
The syntax of
return is as follows:
If there is no
return statement in a function, a
None object will be returned.
Example of Using
def square(n): return n*n print("Square of 4=", square(4))
Square of 4=16
In this code, the function is called in a
n*n which is evaluated and the result is returned to where the function is called (
Scope and Lifetime of Python Variables:
The scope of a variable is where a variable can be accessed. When a variable is declared inside a function, it is not accessible to the outside of that function. This type of variables is called local variable and is accessed only to the function where it is declared.
The lifetime of a variable is the time during which a variable exists in memory. When a variable is declared inside a function, the memory of it will be released when the control jumps out of the function.
See the example below:
def fun(): a = 12 print("Value of a inside function:", a) a = 24 fun() print("Value of a outside function:", a)
Value of a inside function: 12 Value of a outside function: 24
In this code, variable
a inside the function and variable
a outside the function are different variables.
If you try to access variables declared inside functions from outside, you will come across an error -
NameError name 'x' is not defined. But the variables declared outside of functions have a global scope and can be accessed from inside.
Types of Functions:
Functions in Python can be categorized into two types:
Built-in functions: have a predefined meaning and perform specific tasks.
User-defined functions: are defined by the user containing any number of statements to perform user-defined tasks.