- If a group of statements is repeatedly required then it is not recommended to write these statements every time separately. We have to define these statements as a single unit and we can call that unit any number of times based on our requirement without rewriting. This unit is nothing but function.
- The main advantage of functions is code reusability.
Note
- In other languages functions are known as methods procedures, subroutines …etc.
Python supports 2 types of functions
- Built in Functions
- User Defined Functions
Built in Functions
- The functions which are coming along with python software automatically, are called built in functions or pre defined functions.
- Example: id(), type(), input() …etc.
User Defined Functions
- The functions which are developed by programmer explicitly according to business requirements, are called user defined functions.
Syntax
Python
def function_name(parameter):
"""doc string"""
***
***
return value
Note
- While creating functions we can use 2 keywords
- def (mandatory)
- return (optional)
Question
Write a function to print Hello?
Python
def wish():
print("Hello Good Morning")
wish()
wish()
wish()
Output
PowerShell
Hello Good Morning
Hello Good Morning
Hello Good Morning
Parameters
- Parameters are inputs to the function. If a function contains parameters, then at the time of calling, compulsory we should provide values otherwise we will get error.
Question
Write a function to take name of the student as input and print wish message by name.
Python
def wish(name):
print("Hello",name," Good Morning")
wish("Ajay")
wish("Manish")
Output
PowerShell
Hello Ajay Good Morning
Hello Manish Good Morning
Example
Write a function to take number as input and print its square value?
Python
def squareIt(number):
print("The Square of",number,"is", number*number)
squareIt(4)
squareIt(5)
Output
PowerShell
The Square of 4 is 16
The Square of 5 is 25
Return Statement
- Function can take input values as parameters and executes business logic, and returns output to the caller with return statement.
Question
Write a function to accept 2 numbers as input and return sum?
Python
def add(x,y):
return x+y
result1=add(10,20)
result2=add(100,200)
print("The sum is",result1)
print("The sum is",result2)
Output
PowerShell
The sum is 30
The sum is 300
- If we are not writing return statement then default return value is None
Example
Python
def f1():
print("Hello")
f1()
print(f1())
Output
PowerShell
Hello
Hello
None
Question
Write a function to check whether the given number is even or odd?
Python
def even_odd(num):
if num%2==0:
print(num,"is Even Number")
else:
print(num,"is Odd Number")
even_odd(10)
even_odd(15)
Output
PowerShell
10 is Even Number
15 is Odd Number
Question
Write a function to find factorial of given number?
Python
def fact(num):
result=1
while num>=1:
result=result*num
num=num-1
return result
for i in range(1,5):
print("The Factorial of",i,"is :",fact(i))
Output
PowerShell
The Factorial of 1 is : 1
The Factorial of 2 is : 2
The Factorial of 3 is : 6
The Factorial of 4 is : 24
Returning multiple values from a function
- In other languages like C, C++ and Java, function can return maximum one value. But in python, a function can return any number of values.
Example
Python
def sum_sub(a,b):
sum=a+b
sub=a-b
return sum, sub
x, y=sum_sub(100,50)
print("The Sum is :",x)
print("The Subtraction is :",y)
Output
PowerShell
The Sum is : 150
The Subtraction is : 50
Example
Python
def calc(a,b):
sum=a+b
sub=a-b
mul=a*b
div=a/b
return sum,sub,mul,div
t=calc(100,50)
print("The Results are")
for i in t:
print(i)
Output
PowerShell
The Results are
150
50
5000
2.0
Ungraded Questions
Get ready for an exhilarating evaluation of your understanding! Brace yourself as we dive into the upcoming assessment. Your active participation is key, so make sure to attend and demonstrate your knowledge. Let’s embark on this exciting learning journey together!