Python Functions
In Previous Blog_Post
In previous blog post we discuss about the Python List in which we learn various Functions of List so if you want to learn Python or Python Lists You can visit Our previous Blog - Python Lists
In Today's Blog
In Today's Blog We Will Learn About the Python Functions now without any delay let's start today's blog.
Contents -
- Functions
- Types of Functions
- Built in Functions
- User Defined Functions
- Importance of Functions
- How to Define Functions
- Parameters
- Type of Parameters
- Formal
- Actual
- Default
- User Defined Functions
- Functions without Parameters
- Functions with Parameters
- Example of Actual and Formal Parameters
- Concept of Default Parameters
- Passing parameters
- Positional
- Default
- Keyword
- Returning values from Functions
- Scope and lifetime of variable
- local scope
- global scope
Functions
Functions are the sub-programs that perform specific task.
OR
Functions are the small modules and in each module you can perform specific task.
*Module - Component/Part
Types of Functions
In Python there are mainly two type of functions -
- Built in Functions
Built in functions are those functions which loaded automatically with the python language.
for example - input() or len() or max()
- User Defined Functions
User defined functions are those functions which is defined by the user according to their desire.
Importance of User Defined Functions
- Re-usability
Once the function body is defined it can be use more than one time as per user choice.
- Modularity
In simple words modularity means a complex problem can be divided into small parts.
How to Define Functions
It is very simple we can define functions just by adding def keyword in the starting of the program.
for example -
def sum( ):
In the above example the def refers to define and the sum( ) is the function we want to define.
def sum( ):
a=int(input("Enter 1st Number"))
b=int(input("Enter 2nd Number"))
c=a+b
print(c)
sum( )
sum( )
sum( )
In the above example we see that we just defined a function named sum and after writing the function we write program to calculate the sum of two numbers as we write in our normal python IDLE.
But you notice that after writing the whole program we write three times sum( ) which is our function name.
So by writing three times sum( ) after the function when we run the program then the function will automatically runs three times because we write three times sum.
If we write two times then it runs automatically two times.
Parameters
Parameters are the variables used to pass values from one function to another function.
Type of Parameters
- Formal Parameters
Formal Parameters are written in the function prototype or with function header of the definition.
These variables will be treated as local variable of the function.
for example - def sum(x,y)
- Actual Parameters
These are the variables through which actual values will be passed, when we are calling the function.
for example - sum(5,4)
- Default Parameters
These are the parameters with default values.
for example - def sum(x,y=5)
User Defined Functions
Now there are two types of user defined functions -
- Functions Without Parameters
These are the functions without the value inside parenthesis.
for example -
def sum( ):
a=int(input("Enter 1st Number"))
b=int(input("Enter 2nd Number"))
c=a+b
print(c)
sum( )
sum( )
sum( )
In the above example after defining the function in the function sum( ) we did not give the value inside the function.
- Function Without Parameters
These are the functions with the value inside the function.
Example of Actual and Formal Parameters
formal parameters
⇲ ⭹
def sum(a,b):
c=a+b
print(c)
x=int(input("Enter 1st Number"))
y=int(input("Enter 2nd Number"))
sum(x,y)
⇱ ↗
Actual Parameters
Concept of Default Parameters
def sum(x,y=5):
z=x+y
print(z)
a=int(input("Enter 1st Number"))
b=int(input("Enter 2nd Number"))
sum(a,b)
sum(a)
sum( ) ❌ -this statement sum( ) is wrong statement in the default parameters because above we already give 4 variables x,y and a,b.
Passing Parameters
Python has three ways to pass the values through arguments.
- Positional Argument
It follows the sequence of variable declaration.
for example -
def sum(a,b,c):
d=a+b+c sum(a,b,c)
print(d) ↓ ↓ ↓
sum(5,6,7)
sum(5,6,7)
sum(7,6,5)
- Default Argument
In a function header any parameter cannot have a default value unless all parameters appearing on its right have their default values.
for example -
def simpleintrest(p,r,t=2):
si=(p*r*t)/100
print(si)
simpleintrest(1000,10)
we need to enter all the three values of p r t in the definition then only the program will run.
def si(p,r=10,t) ❌ - wrong Statement
def si(p=1000,r=10,t) ❌ - wrong Statement
def si(p=1000,r=10,t=2) ✔️ - correct Statement
- Keyword Argument
In this argument sequence does not matter and the values are received by the names.
for example -
def simple_interest(p,r,t):
si=p*r*t/100
print(si)
simple_interest(p=1000,r=10,t=2)
simple_interest(p=1000,t=2,r=10)
simple_interest(r=10,t=2,p=1000)
Returning Values from Function
Functions in python may or may not return value it can be classified in two ways -
- Function Returning Some Value -
def sum(a,b):
c=a+b
return c
result=sum(2,3)
print(result)
- Function not returning any value
def sum(a,b):
c=a+b
return c
result=sum(2,3)
print(result)
Scope and lifetime of variable
- Local Scope
Local Scope are the variables which are used inside the function and they are destroyed once.
Hence, the function does not remember the value of a variable from its previous call.
for example -
def square( ):
x=5
print("local variable",x)
x=10
square( )
print("variable outside function square")
here variable x declared inside square( ) will be treated as local variable and it cannot be accessed outside.
- Global Scope
Global Scope are the variables which can used outside the function.
In this scope, lifetime of a variable is the period through which the variable exists in the memory.
for example -
glv=10
def global_variable( ):
print("inside function",glv)
global_variable( )
print("outside function",glv)
here we can see the variable glv can be used anywhere either inside or outside the function.
IN THE NEXT BLOG WE LEARN MORE ABOUT MUTABLE OR IMMUTABLE TYPE AND ALSO LEARN SOME BASICS OF PYTHON LIBRARIES.
THANK YOU FOR VISITING , KEEP LEARNING STAY TUNED FOR NEXT BLOG.
FOLLOW OUR BLOG FOR REGULAR UPDATES.
VIEW OUR PREVIOUS BLOG FOR THE BASIC CONCEPTS OF PYTHON AND OTHER BASIC CONCEPTS OF COMPUTER.
VISIT OUR SECOND BLOG –incredible2facts.blogspot.com
Our YouTube channel will give you the short motivational speeches of the great personalities in the present there is only seven motivational videos on channel but in future I will upload the motivational videos soon .
VISIT OUR INSTAGRAM PAGE –instagram.com/learnpython16
Our Instagram page is also a new page i just created few days ago but stay tuned I will upload the post very soon.
VISIT OUR TWITTER PAGE -
Please support us by following our blog and by subscribing our YouTube channel and by following our Instagram page.
Python Functions
Reviewed by Learn Python
on
April 27, 2020
Rating:

No comments: