Function in Python
Function in Python
Function in Python
Functions help break our program into smaller and modular chunks. As our program
grows larger and larger, functions make it more organized and manageable.
Syntax of Function
def function_name(parameters):
"""docstring"""
statement(s)
6.One or more valid python statements that make up the function body. Statements must
have the same indentation level (usually 4 spaces).
7.An optional return statement to return a value from the function.
Example of a function
def greet(name):
"""
This function greets to
the person passed in as
a parameter
"""
print("Hello, " + name + ". Good morning!")
Once we have defined a function, we can call it from another function, program, or even
the Python prompt. To call a function we simply type the function name with appropriate
parameters.
>>> greet('Paul')
Hello, Paul. Good morning!
Try running the above code in the Python program with the function definition to see the
output.
def greet(name):
"""
This function greets to
the person passed in as
a parameter
"""
print("Hello, " + name + ". Good morning!")
greet('Paul')
Run Code
Note : In python, the function definition should always be present before the
# function call
greet('Paul')
# function definition
def greet(name):
"""
This function greets to
the person passed in as
a parameter
"""
print("Hello, " + name + ". Good morning!")
# Error: name 'greet' is not defined
Python Recursion
In this tutorial, you will learn to create a recursive function (a function that calls itself).
What is recursion?
Recursion is the process of defining something in terms of itself.
A physical world example would be to place two parallel mirrors facing each other. Any
object in between them would be reflected recursively.
Factorial of a number is the product of all the integers from 1 to that number. For
example, the factorial of 6 (denoted as 6!) is 1*2*3*4*5*6 = 720 .
Example of a recursive function
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
return (x * factorial(x-1))
num = 3
print("The factorial of", num, "is", factorial(num))
Run Code
Output
The factorial of 3 is 6
Lambda functions can have any number of arguments but only one expression. The
expression is evaluated and returned. Lambda functions can be used wherever function
objects are required.
print(double(5))
Run Code
Output
10
In the above program, lambda x: x * 2 is the lambda function. Here x is the argument and x
* 2 is the expression that gets evaluated and returned.
This function has no name. It returns a function object which is assigned to the
identifier double . We can now call it as a normal function. The statement
double = lambda x: x * 2
def double(x):
return x * 2
print(new_list)
Run Code
Output
[4, 6, 8, 12]
The function is called with all the items in the list and a new list is returned which contains items
returned by that function for each item.
Here is an example use of map() function to double all the items in a list.
# Program to double each item in a list using map()
print(new_list)
Run Code
Output
python3
Output
Inside Function: GeeksforGeeks
Example 2
Python3
Output
Inside Function [10, 20, 30, 40, 50]
While calling the function we pass values andAt the time of calling instead of passing
called as call by values. the values we pass the address of the
variables that means the location of
variables so called call by reference.
The value of each variable is calling the The address of variable in function while
function and copy variable into the variablescalling is copied into dummy variables of
of the function. a function.
The changes are made to dummy variables in By using the address we have the access
the called function that have no effect on the to the actual variable.
actual variable of the function.
We cannot change the values of the actual We can change the values of the actual
variable through a function call. variable through a function call.
The values are passed by the simple The pointers are necessary to define and
techniques store the address of variables.
All the other functions that we write on our own fall under user-defined functions. So,
our user-defined function could be a library function to someone else.
2. If repeated code occurs in a program. Function can be used to include those codes and
execute when needed by calling that function.
3. Programmars working on large project can divide the workload by making different
functions.
num1 = 5
num2 = 6
Output
Python Built in Functions
❮ PreviousNext ❯