return statement in python example

In Programming, the return statement is used by lots of programmers but in few programs, we didn’t know how the return works. Now, we will learn some interesting about return statements.

Return Statement

  • The return is used to end the execution of the program and sends a result back to the caller.
  • Only one return statement is executed at run time even though the function contains multiple return statements.

Syntax :

def <name> (arg1, arg2, …, argn):
    <statement>
    return [Expression]

Coding 1:

All functions in Python have a return value even if no return line inside the code.

# return the asuume values x,y = 5,5

def multiply (x,y):
    z = x*y
    return z
print(multiply(5,5)

#Output
25

Coding 2:

Functions without a return value, then it will return the special keyword “None”.

# return without arguments 

def multiply (x,y):
    z = x*y
    return
print(multiply(5,5)

#Output
None

Python Program to find GCD of the two numbers using the return statement

# GCD using return

def gcd(a,b):
	if(b==0):
		return a
	else:
		return gcd(b,a%b)

a = 8
b= 2

print ("The gcd of 8 and 2 is : ",end="")
print (gcd(8,2))

#Output
The gcd of 8 and 2 is : 2

Related Blogs

python functions and their types: https://iterathon.tech//python-functions-and-their-types/

LEARN MORE ❣