the Return statement

The Return statement


The return statement causes your function to exit and returns a value to its caller. The point of functions in general is to take inputs and return something.

The return statement is used when a function is ready to return a value to its caller. So, only one return statement is executed at run time even though the function contains multiple return statements.

Any number of 'return' statements are allowed in a function definition but only one of them is executed at run time.

Syntax of return

return [expression list ]

This statement can contain expression which gets evaluated and the value is returned. If there is no expression in the statement or the return statement itself is not present inside a function, then the function will return the None object.

Example :

# return statment
def usr_abs (n):
if n>=0:
return n
else:
return –n
# Now invoking the function
x=int (input(“Enter a number :”)
print (usr_abs (x))

Output 1:

Enter a number : 25

25

Output 2:

Enter a number : -25

25

Qus. 1 : <p>What is the return type of following function ?</p><pre>def func1():<br>&nbsp; &nbsp; &nbsp; &nbsp;return 'mnp',22<br>print(type(func1()))</pre>

  1. List
  2. Dictionary
  3. String
  4. Tuple
Qus. 2 : <p>What is the output of the following code ?</p><pre><span style="font-size: 14px;">def disp(*arg):<br></span><span style="font-size: 14px;">&nbsp; &nbsp; for i in arg:<br></span><span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; print(i)<br></span><span style="font-size: 14px;">disp(name="Rajat", age="20")</span></pre>

  1. TypeError
  2. Rajat 20
  3. Name age
  4. None of these
Qus. 3 : <p>What will be the output of the following Python code ?</p><pre><span style="font-size: 14px;">def display(b,n):<br></span><span style="font-size: 14px;">&nbsp; &nbsp; while n&gt;0:<br></span><span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; print(b,end='')<br></span><span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; n=n-1<br></span><span style="font-size: 14px;">display('z',3)</span></pre>

  1. zzz
  2. zz
  3. Infinite loop
  4. An exception is thrown
Qus. 4 : If return statement is not used inside the function, the function will return:

  1. None
  2. 0
  3. Null
  4. Arbitary value
Qus. 5 : If a function doesn't have a return statement, which of the following does the function return ?

  1. int
  2. null
  3. None
  4. An exception is thrown without the return statement

Programs

Python program to convert decimal into other number systems

View Solution


python program that generates six random numbers in a sequence created with start stop step

View Solution


Python program to illustrate the global variable

View Solution


python program to create a function to calculate factorial value

View Solution


Python program to find the sum of factorial upto n terms using function

View Solution


python program to create a function for reverse the number

View Solution


Python program to check number is Palindrome or not using function

View Solution


Python program to create a function for calculating sum of digit

View Solution


Python program to create recursive function to find factorial

View Solution


python function to accept 2 number and return addition subtraction multiplication and division

View Solution


python function to accept number of days and return week and days to its equivalent

View Solution


python function to extract the day month year from given date

View Solution


Python function to find the sum of all numbers between 100 and 500 which are divisible by 2

View Solution


Python function to get two matrices and multiply them

View Solution


python recursive function to find the sum of digits

View Solution


python recursive function to print the reverse of number

View Solution


Write a function with name dividebyfive which generate and prints a random integer number from the range 0 to 100 and then return True if the randomly generated number is divisible by 5, and False otherwise.

View Solution


CCC Online Test Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Online Exam Quiz O Level NIELIT Study material and Quiz Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Best Website and Software Company in Allahabad Website development Company in Allahabad