In the last session, we discussed the Python Loops concept. Now, we will look at Python functions and examples. The functions are the most important topic in Python.
Let’s start.
Functions
The function is a block of code and it is reusable.
We have two types of functions:
- User-defined functions - defined by the user to perform the specific task
- Build-in-functions - pre-defined in Python
Simple example
Input:
def myfuc():
print("Welcome")
myfuc()
Output:
Welcome
Add a string with function
Input:
def myfuc(name):
print("Welcome "+name)
myfuc("Python")
Output:
Welcome Python
Return function
Input:
def myfun(x):
y = x+5
return y
a = myfun(10)
print(a)
Output:
15
Multiple arguments passing in a function
Input:
def myfun(a,b,c,d,e):
x=a+b+c+d+e
print(x)
myfun(2,3,4,5,6)
Output:
20
Without Arguments
Input:
def add():
a = int(input("Enter a Number :"))
b = int(input("Enter a Number :"))
c = a+b
print("c =",c)
add()
Output:
Enter a Number : 30
Enter a Number : 25
c = 55
With Arguments
Input:
a = int(input("Enter a Number : "))
b = int(input("Enter a Number : "))
def add(a,b):
c = a+b
print("c =",c)
add(a,b)
Output:
Enter a Number : 15
Enter a Number : 25
c = 40
Multiple Function
Input:
def my_function(fname):
print(fname + " language")
my_function("C")
my_function("C++")
my_function("Java")
my_function("Python")
Output:
C language
C++ language
Java language
Python language
Food items to the amount using function and if, else statements,
Input:
A = int(input("Enter your Amount : "))
def Food(A):
if A<=100:
print("You have 'Fried Chicken' for Rs.",A)
elif A<=200:
print("You have 'Chicken Briyani' for Rs.",A)
elif A<=300:
print("You have 'Mutton Briyani' for Rs.",A)
else:
print("You have 'Dinner Combo' for Rs.",A)
Food(A)
Output:
1st Run:
Enter your Amount : 98
You have 'Fried Chicken' for Rs. 98
2nd Run:
Enter your Amount : 450
You have 'Dinner Combo' for Rs. 450
3rd Run:
Enter your Amount : 154
You have 'Chicken Briyani' for Rs. 154
4th Run:
Enter your Amount : 270
You have 'Mutton Briyani' for Rs. 270
Selecting Items for food order,
Input:
print(
'''
***Welcome***
Here is the Menu
Chicken lolipop
Fried chicken
Chicken briyani
Mutton briyani
Sunday combo
Fresh juice
'''
)
A = str(input("Order Please : "))
a = 'Fried chicken'
b = 'Chicken lolipop'
c = 'Chicken briyani'
d = 'Mutton briyani'
e = 'Sunday combo'
f = 'Fresh juice'
def Food():
if A == a:
print("Rs.100")
elif A == b:
print("Rs.150")
elif A == c:
print("Rs.200")
elif A == d:
print("Rs.300")
elif A == e:
print("Rs.500")
else:
print("Rs.75")
Food()
Output:
1st Run:
***Welcome***
Here is the Menu
Chicken lolipop
Fried chicken
Chicken briyani
Mutton briyani
Sunday combo
Fresh juice
Order Please : Chicken briyani
Rs.200
2nd Run:
***Welcome***
Here is the Menu
Chicken lolipop
Fried chicken
Chicken briyani
Mutton briyani
Sunday combo
Fresh juice
Order Please : Sunday combo
Rs.500
Now, you can clearly understand functions. If you have any doubts please ask me anything in the comments. In our next session, we will discuss more interesting examples of functions.