What is Return Statement in Python : Syntax and Usage
Last updated: September 13, 2024 By Sunil Shaw
What is Return Statement in Python
In Python return
keyword
is used at the end of the function when there is a need to send back the result of the function back to the caller. Software programming is not about printing result all the time. Behind the scenes, perform calculations hidden from the users. Further utilize these values in calculations to obtain the final output desired by the users. You can assign the value obtained from the return statement to a variable and use it further for calculations. Let’s see what is return statement in Python: syntax and usage.
Syntax and Usage
Look at the code given below. The function adding_numbers()
adds three numbers and returns the result which is assigned to a variable X. Then, the program displays the value of X as output.
def adding_numbers(number1, number2, number3):
print('Have to add three numbers')
print('First Number = {}' .format(number1))
print('Second Number = {}'.format(number2))
print('Third Number = {}'.format(number3))
return number1 + number2 + number3
x = adding_numbers(10, 20, 30)
print('The function retured a value of {}'.format(x))
Output
Have to add three numbers
First Number = 10
Second Number = 20
Third Number = 30
The function retured a value of 60
[Finished in 14ms]
So you can say that:
- A return statement exists the function. The last statement of a function is it, and it won’t execute any statement coming after.
- When a function is not returning a value explicitly that means means indirectly or implicitly it is returning a value of None.
- If a function needs to return more than one value, it will return all the values as a tuple.
Example
Write a function that prompts the user to enter values for a list. The function should return back the length of the list.
def find_len(list1):
return len(list1)
x = input('Enter the values separated by single space :')
x_list = x.split()
print(find_len(x_list))
Output
Enter the values separated by single space :1 'Gmail' 'Google' 1.09 [2,3,45,9]
5
Explanation: When you execute this code program, it will prompt you to enter values separated by a single space. In this case, you will pass five values.
- 1
- ‘Gmail’
- ‘Google’
- 1.09
- [2,3,45,9]
Therefore, the program calculates the count of elements provided as inputs and shows the result, which is 5.
Example
The code below calculates and returns the sum and product of two numbers.
def sum_prod(num1, num2):
num_sum = num1 + num2
num_prod = num1 * num2
return num_sum, num_prod
x = int(input('Enter the first number :'))
y = int(input('Enter the second number :'))
print(sum_prod(x,y))
Output
Enter the first number :10
Enter the second number :20
(30, 200)
Example
Write code to find the HCF
of the two given numbers.
Solution
HCF, which stands for Highest Common Factor or Greatest Common Divisor for two numbers, denotes the largest number between 1 and the smaller of the two provided numbers that perfectly divides both numbers, resulting in a remainder of zero.
- Defining a function hcf() that takes two numbers as input.
def hcf(x,y):
2. Find out which of the two numbers is greatest, the other one will be the smallest.
small_num = 0
if x > y:
small_num = y
else:
small_num = x
Set a for loop for the range 1 to small_num+1. (We take the upper limit as small_num+1 beacuse the for loop operates for one number less than the upper limit of the range). In this for loop, divide both the numbers with each number in the range, and if any number divides both, perfectly assign that value to hcf as below in following code:
for i in range(1,small_num+1):
if (x % i == 0) and (y % i == 0):
hcf = i
return hcf
Suppose, two numbers are 6 and 24, first both numbers are divisible by 2. If hcf equals 2, then both numbers will be divisible by 3. So, assign the value of 3 to 3. Then, the loop will encounter 6, which will again divide both the numbers equally. So, assign 6 to HCF. Once the function reaches the upper limit of the range, it will ultimately yield an HCF value of 6.
3. Return the value of hcf:
return hcf
def hcf(x,y):
small_num = 0
if x > y:
small_num = y
else:
small_num = x
for i in range(1,small_num+1):
if (x % i == 0) and (y % i == 0):
hcf = i
return hcf
print(hcf(6,24))
OUTPUT
6
[Finished in 14ms]
About Author
I am a Web Developer, Love to write code and explain in brief. I Worked on several projects and completed in no time.
View all posts by Sunil Shaw