0
What return statement does?what happens when we don't write return statement
5 Answers
+ 5
when you have a return statement it means that when you call that function from main somthing will be returned back to it
void just means that nothing will be returned to the function although theres a possibility you will print somthing to screen nothing is actually retuned to the calling function
+ 1
Using pseudo code
Sample 1:
age = 14;
CheckAge(age)
function checkAge(num) {
if(num < 18) {
print("Too young");
} else {
print("Old enough");
}
}
Sample 2:
age = 14;
text = CheckAge(age);
function checkAge(num) {
if(num < 18) {
criteria = "Too young";
} else {
criteria = "Old enough";
}
return criteria;
}
print(info);
If age was less than 18, info would get set to "Too young" etc.
+ 1
Another example or return statement:
value = multiplyTwice(4);
function multiplyTwice(num) {
return num*2;
}
value will be set to 8, because the function took the number (4), and returned a value based on a calculated value.