# How to call a Function in Python

Before you know the details of a function call, you need to know what is the function $u = \frac{1}{2}mv^{2}$ and why it is used in the program?

Functions are sub-programs that are used to perform various functional tasks.

There are usually two types of functions. For example

1. Built-in Function
2. User Define  Function

So, in this tutorial, we will see how to call a function from a user-defined function.

## Structure of Function

When you want to use a function in a program, you must first define the function and then call it. So, you understand that a complete User Define Function is divided into two parts.

1. Function Define
2. Function calling

### 1. Function Define

Simply put, creating a function is called function definition. And you need to use def keywords when creating functions. And in the last of the function names, you have to use the Parentheses (), so that the interpreter can understand that it is a function.

The colon : at the end of the function name indicates that the body of the function is starting from here. And you have to use indentation when you pass the statement in the body of the function.

Now if you run the program, the interpreter will not respond to you. Because you created the function, but not yet called.

def add():
a,b = 10,20.50
print(a+b)

Output :

no output....

### 2. Function Calling

Function calling means establishing a connection between a function and an interpreter. Because the interpreter never executes the direct function, unless you call the function.

Notice the figure above so you can see that the interpreter is the first to execute the line where the function was first called. After that the body of that function will be executed.

Now if you run the program, the interpreter will show you the output

def add():
a,b = 10,20.50
print(a+b)

add()

Output :

30.5

You can usually call a function in two ways

a. pass no argument

And if you do not pass any parameters in the function, you will not need to pass any parameters while calling.

def add():
n,q = 100,0
for var in range(n):
q+=var
print(q)
add()

Output :

4950

b. Pass Argument

If you pass the parameter during the function definition, then you also need to pass the parameter in the function during the function call. Notice this example below

def add(n):
q=0
for var in range(n):
q+=var
print(q)

add(200)

Output :

4950
19900

Many of you may have questions in your mind, what is the parameter and why is it passed in the function?

Simply put, the parameter is the user input variable. This allows the user to assign one or more times and different types of values ​​to the given function as input.

def cal(p,q,n):
if p=='+':
print(q+n)
elif p=='-':
print(q-n)
elif p == '*':
print(q*n)
elif p=='/':
print(q/n)

cal('-',3,4)
cal('*',3,4)
cal('/',3,4)
cal('+',3,4)

Output :

-1
12
0.75
7

Take a look at this program above and you will understand that the parameter or argument works like an input() function here.

And there are different rules for passing arguments between functions that we will discuss at the end of this tutorial.

## Function Call with a return statement

You can display on the screen in two ways what will give you the output in the function.

### 1. print statement

First, you can display the output of the function, within the function, with the help of the print function. In this case, the value of the function will be displayed directly on the screen when you call the function.

You do not need to write the print function separately when calling outside the function.

def show(p,q):
print('python is good programming language')
print(p)
print(p+q)
show('pythongalaxy',' for u')

Output :

python is good programming language
pythongalaxy
pythongalaxy for u

### 2. return Statement

Second, you can return the value of the function without printing it in the function. And to return this value you need to use the return keyword. Notice this program below

def show():
return 'python'
show()

Output :

no output...

In this case, if you just call the function, the interpreter will not respond to you. Because we don’t display the value of the function anywhere in the function, we just return it.

So, you can use this return value in the program in two ways. First, you can store in any variable. Second, you can display the return value of the function with the help of the print function.

def show():
return 'python'
name = show()
print(name)
print(show())

Output :

python
python

And you can return more than one value at a time. So, look at this program below.

def add(p,q):
return p,q,p+q
print(add(10,30))

Output :

(50, 40, 90)
(10, 30, 40)

When the interpreter returns multiple values to you, each value will be stored in a tuple.

## Pass function as a parameter

You can pass another function as a parameter when you call a function. So, check out this program below

def show(var):

a,b=10,30
return a+b

show(add())

Output :

add two numbers : 40

When the show() function is called and the add() function is passed through it. Here, the add() function in the show() function will behave like an argument variable.

You enter a function that allows you to display odd numbers on the screen?

def even(n,n1):
for var in range(n):
if var%2!=0:
print(var,end=' ')
print('\n')
print('sum of odd numbers',n1)
def odd(n2):
a=0
for var in range(n2):
if var%2!=0:
a=a+var
return a
even(10,odd(10))

Output :

1 3 5 7 9

sum of odd numbers 25

## Nested Function Call

Nested function call means how you define and call an internal function into an external function. So, look at this diagram below

def show():
print('Python')
def show1():
print('Java')
show1() #nested function call
show()

Output:

Python
Java

How do you create a new triangle by adding two opposite triangles?

In this case, two nested functions are used for two triangles and a new triangle is created by calling those two nested functions into external functions.

def triangel(n):
def triangel1(n1):
for var in range(n1):
print(' *'*var)
triangel1(n) #nested function call
def triangel2(n2):
for var in range(n2):
print(' *'*(5-var))
triangel2(n) #nested function call
triangel(5)

Output :

*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*

## Recursion Call

When a function calls itself, it is called a recursion call. In this case, the function will call itself as long as the argument of the recursive function is valid.

def show():
print('python')
show()
show()

Output :

python
.
.
.
python (max 1000 times)

A popular example of this recursive call is to determine the factorial within a given number.

def fact(n):
if n==0:
return 1
else:
return n*fact(n-1)
print(fact(10))
print(fact(0))

Output :

3628800
1

When a function calls itself, this recursive call continues to respond until infinite. As a result, it can have a bad effect on your system. To solve this problem, Python has set a maximum of 1000 times the execution of recursive calls.

import sys
print(sys.getrecursionlimit())

Output :

1000

You can use the getrecursionlimit() function to determine how many times a function will call itself. For this you need to import the sys module.

You will also need to use the setrecursionlimit() function if you want to increase the limit of recursion calls. So look at this program below

import sys
print(sys.getrecursionlimit())
sys.setrecursionlimit(3500)
print(sys.getrecursionlimit())

Output :

1000
3500

## FAQ’s on Function call

What is the difference between print statement and return statement calls?

When you use a print statement in a function, there is no need to call a function in the print function to get the output of the function. Just call and you will get the output of this function. Notice this example below.

In the second case, when you use the return statement in the function, you need to use the print function to get the output during the function call.

How do you call external function in a nested function?

When you call an external function within a nested function, this function call will be considered a recursive call. In this case, an execution loop will be created between the external function and the internal function. Since this is a recursive call, there will be a maximum of 1000 times the execution.

def show():
print('python')
def reso():
print('java')
show()
reso()
show()

Output :

python
java
.
.
.
python
java

What are global variables and local variables and what are the main differences between them?

Variables that you can use inside and outside the function are called global variables. So, you cannot use direct global variable in the function, you need to use global keyword. So, look at this example below

Variables whose limitations are present in that function are called local variables. So, you cannot use the local variable outside the function.

Local variables are defined within the function and used within the function. So, notice the example given below

## Conclusion

How to call a function is discussed in depth in this tutorial. A function call is an important part of a function. So, in this tutorial, each concept has been presented to you in a simple way with examples. So, if you like it, don’t forget to share and comment.