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 will be the output of the following Python code?</p><pre><span style="font-size: 14px;">def foo():<br></span><span style="font-size: 14px;"> total += 1<br></span><span style="font-size: 14px;"> return total<br></span><span style="font-size: 14px;">total = 0<br></span><span style="font-size: 14px;">print(foo())</span></pre>
- 0
- 1
- error
- none of the mentioned
Qus. 2 : <p>What will be the output of the following Python code?</p><pre><span style="font-size: 14px;">def foo(x):<br></span><span style="font-size: 14px;"> x = ['def', 'abc']<br></span><span style="font-size: 14px;"> return id(x)<br></span><span style="font-size: 14px;">q = ['abc', 'def']<br></span><span style="font-size: 14px;">print(id(q) == foo(q))</span></pre>
- True
- False
- None
- Error
Qus. 3 : Given a function that does not return any value, what value is shown when executed at the shell ?
- int
- bool
- void
- None
Qus. 4 : <p>What is the return type of following function ?</p><pre>def func1():<br> return 'mnp',22<br>print(type(func1()))</pre>
- List
- Dictionary
- String
- Tuple
Qus. 5 : <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;"> for i in arg:<br></span><span style="font-size: 14px;"> print(i)<br></span><span style="font-size: 14px;">disp(name="Rajat", age="20")</span></pre>
- TypeError
- Rajat 20
- Name age
- None of these
Qus. 6 : <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;"> while n>0:<br></span><span style="font-size: 14px;"> print(b,end='')<br></span><span style="font-size: 14px;"> n=n-1<br></span><span style="font-size: 14px;">display('z',3)</span></pre>
- zzz
- zz
- Infinite loop
- An exception is thrown
Qus. 7 : If return statement is not used inside the function, the function will return:
- None
- 0
- Null
- Arbitary value
Qus. 8 : If a function doesn't have a return statement, which of the following does the function return ?
- int
- null
- None
- 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 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