A function is a block of code that performs a specific task.
Types of Function-
- Built in Library Function: These are standard functions in python that are available to use. Python has many built in functions. Example: print(), input(), pow(), max(), min() etc.
- User-defined Function: We can create our own functions based on ourrequirements, which are called user-defined function. We can define a function in python using the def keyword.
(i) Create a Function: To create a function, we use the def keyword follows by a name.
Example:
def Hello():
print(“Hello Learners…”) #Function body
Here, def keyword is used to create a function. Hello() is name of the function.
(ii) Calling a Function: To use the function, we need to call the function. After creating a function, we can call it by using the name of the function followed by parenthesis.
def Hello():
print(“Hello Learners…”)
print(“Welcome to class”)
Hello()
We can make a function to put some commonly or repeatedly done tasks together and instead of writing the same code again and again for different inputs, we can do the function calls to reuse code contained in it over and over again.
Benefits of using Function:
- Increase code Readability
- Increase code Reuseability
Function Arguments:
Arguments are inputs given to the function.
Example:
def Hello(name):
print(“Welcome..”,name)
#pass argument
Hello(“Priya”)
Hello(“Raj”)
Parameters or Arguments:
The terms parameters and argument can be used for the same thing: information that are passed into a function.
Arguments are specified after the function name, inside the parenthesis. We can add as many arguments as we want, just separate them with comma.
From a Function’s perspective,
A parameter is the variable lists inside the parenthesis when a function is declared.
An argument is the value that is passed to the function when it is called.
Parameters are also known as Formal Parameters, and arguments are also known as Actual Parameters.
Example:
def sum(a,b):
#Here, a,b is parameter
return a+b
Result=sum(10,20)
#Here, 10,20 is argument
print(Result)
Return Statement:
We return a value from the function using the return statement.
def find_sq(n):
result=n*n
return result
sq=find_sq(4)
print(“Square :”,sq)
Specify Argument name with values:
def student(fname,lname):
print(fname,lname)
student (fname= “Eliza”, lname= “Begum”)
student (lname= “Begum”, fname= “Eliza”)
Positional Arguments:
def details(name,roll)
print(“Hi, I am ”, name)
print(“My rollno is: ”,roll)
details(“Eliza”,1) #Correct output
details(1, “Eliza”) #Incorrect Output
Number of Arguments:
A function must be called with correct number of arguments. If the function has 2 arguments, we have to call the function with 2 arguments, not more and not less.
def name(fname,lname):
print(fname, “ “ , lname)
name(“Eliza”, “Begum”)
Passing a list as an Argument
def List(food):
for x in food:
print(x)
fruits=[“mango”, “apple”, “orange”, “banana”]
List(fruits)
Programming example of Function:
1. Write a program to add two numbers using function.
def add(a,b):
sum=a+b
print(“Sum: ” , sum)
add(100,200)
Output:
Sum: 300
OR
def add(a,b):
return a+b
sum=add(100,200)
print(“Sum: ” , sum)
Output:
Sum: 300
2. Write a program to find the area of a circle.
def area(r):
pi=3.142
return pi*r*r
print(“Area is: “, area(5))
Output:
Area is: 78.55
3. Write a program to find maximum of two number.
def maximum(a,b):
if(a>b):
return a
else:
return b
print(“Maximum number: “, maximum(6,2))
Output:
Maximum number: 6
OR
a,b=6,2
maximum=max(a,b)
print(“Maximum Number: “,maximum)
Output:
Maximum Number: 6
4. Write a program to find the factorial of a number.
def factorial(n):
if(n<0):
return 0
elif(n==0 or n==1):
return 1
else:
fact=1
while(n>1):
fact*=n
n-=1
return fact
print(“Factorial : “,factorial(5))
Output:
Factorial : 120
5. Write a program in Python to check whether the number passed as an argument to the function is even or odd.
def evenOdd(x):
if(x%2==0):
print(“Even”)
else:
print(“Odd”)
evenOdd(8)
evenOdd(7)
Output:
Even
Odd
6. Write a python program to print Fibonacci Series.
def fib(n):
a,b=0,1
while(a<n):
print(a, end= ‘ ’)
a,b=b,a+b
fib(50)
Output:
0 1 1 2 3 5 8 13 21 34
Important points to remember:
- A function is a block of code which only runs when it is called.
- We can pass data(input), known as parameters into a function.
- A function can return data (value) as a result.