In: Computer Science
T/F: A function may not execute every line of code in the function if it returns a value to the user before getting to the remaining lines.
|
Greetings!!!!!!!!!
The given statement is A.True. The reason behind it is as follows.
When a return statement is executed inside a function, the function will eventually terminate and return that value, and then the remaining lines of code in the function will not be executed. Because of this, during coding of functions generally have the return statement at the very last line of function's body, so that all the other remaining lines of code in the function will execute first before the function terminates and returns a value.
Example to support above explanation is as below:-
In below example of two function scripts with same name of function exampleReturnValue in python are shared with two different cases,
First script of function is printing the value of variable 'value' as 100, because before the return statement at last line, there is no return statement. So All statements are executed and final value of variable 'value' is return as 100.
Second script of function is printing the value of variable 'value' as 50, because before the return statement at last line, there is one more return statement .Which cause value of variable 'value' is return as 50, because statement "value =value + 50" was not executed as execution of first return statement causes control to terminate the execution of the function So All statements are executed and final value of variable 'value' is return as 50 by the function in second case.
Scripts and screen shots are shared for both cases.
Case 1.
def exampleReturnValue():
value = 50
# Below lines will not execute despite part of function.
# As they are below the return statement.
value =value + 50
return value
print(exampleReturnValue())
Case 2.
def exampleReturnValue():
value = 50
return value
# Below lines will not execute despite part of function.
# As they are below the return statement.
value =value + 50
return value
print(exampleReturnValue())
Thank You