2️⃣ Custom Functions
Python allows users to create their own custom functions to perform required task.
Function is defined first and then it can be called.
We can call the same functions multiple times whenever its output required.
Lets see the syntax of defining and calling a custom function:
1 2 3 4 5 6 7 8 9 10 | # custom function definition
def my_function_name(param1, param2, param3):
# statement1
# statement2
# statement3
# statement4
return variable
# function call
returned_value = my_function_name(arg1, arg2, arg3)
|
Lets define simple function display_days(), which displays all days names
💻 Example:
1 2 3 4 5 6 7 8 | # function definition
def display_days():
days_list = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
for day in days_list:
print(day)
# function call
display_days()
|
✅ Output:
Mon
Tue
Wed
Thu
Fri
Sat
Sun
Lets define function greet(), which takes username as an input and greets the user.
💻 Example:
1 2 3 4 5 6 7 8 9 | # function definition
def greet():
print(f"Good Morning, {username} !")
# get username from user
username = input("Enter User Name:")
# function call
greet()
|
✅ Output:
Enter User Name:TechnoDexterous
Good Morning, TechnoDexterous !
3) Recursion
We can also call the same function multiple times while remaining inside it.
This process is called as recursion and the function is called as recursive function.
Lets understand the recursion consept by implementation of factorial calculation.
We know the factorial of a number is nothing but product of number with the number less than until it becomes 1.
💻 Example:
Factorial of 1 is 1.
Factorial of 2 is 2*1=2.
Factorial of 3 is 3*2*1=6.
Factorial of 4 is 4*3*2*1=24 and so on….
💻 Example 1:
Factorial of a number.
1 2 3 4 5 6 7 8 9 10 11 12 | # function defination
def facto(x):
if x==0:
return 1
else:
return(x*facto(x-1))
# lets calculate factorial of 4
four_fact=facto(4)
print(four_fact)
# lets calculate factorial of 3
three_fact=facto(3)
print(three_fact)
|
✅ Output:
24
6
💻 Example 2:
1 2 3 4 5 6 7 8 9 10 | num = int(input("Enter a number: "))
factorial = 1
if num < 0:
print(" Factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)
|
✅ Output:
Enter a number: 3
The factorial of 3 is 6
Return Keyword
We have defined various functions which perfroms desired tasks after calling them.
We can also return a particular value from function using return keyword.
Returned value can be stored in new variable and could be utilized for accomplishing further tasks.
1) Return single value:
Lets define a function which returns only single value or variable after calling it.
💻 Example:
Define two_nums_add function which returns addition of two variables (numbers): num1, num2.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | # function definition
def two_nums_add():
# calculate addition
addition=num1+num2
# return the addition
return(addition)
# lets define two variables
num1=10
num2=20
# function call and store the sum in result variable
result=two_nums_add()
print(f"Their addition(num1+num2): {result}")
|
✅ Output:
Their addition(num1+num2): 30
2) Return multiple values:
We can also return multiple values from a function.
The sequence of variables to which the returned values are stored should be same with the order of variables returned.
💻 Example:
Lets modify above function to return sum and product(multiplication) of num1 and num2.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | def calculate():
# calculate sum
addition=num1+num2
# calculate product
product=num1*num2
# return sum and product
return(addition,product)
# lets define two variables
num1=10
num2=20
# Lets store sum inside num_add variable
# and product inside num_prod
num_add,num_prod=calculate()
print(f'Their addition(num1+num2): {num_add}')
print(f'Their product(num1*num2): {num_prod}')
|
✅ Output:
Their addition(num1+num2): 30
Their product(num1*num2): 200
Be prepared with below questions prior to your interview !
Frequently asked Interview Questions
What are built-in functions in Python?
What is a custom function in Python?
How do you define a function in Python?
What are function parameters?
What are function arguments?
How do you return values from a function?
Can a function have multiple return statements?