What exactly is the point of the "return"? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What exactly is the point of the "return"?

19th Jan 2016, 6:52 AM
Noble S. Ant
Noble S. Ant - avatar
5 Answers
+ 2
Treat a function like a tunnel. Return is the exit. And parameters are Entry point. If a function is declared void than it will not have exit ie dead end. But if it has been defined with int then it will have an open exit. Return statement is a gate keeper , it determines who can exit from tunnel. :)
22nd Mar 2016, 1:35 AM
Abhijeet Verma
Abhijeet Verma - avatar
+ 2
function returns value and after running itself acts like the returned value. for example we do int a=3; here value of a is 3; now for. int b= a here value of b is 3; similarl to this if we write this int twice(x){ int y; y= 2*x; return y; } int a=twice(b); here value of a is 6. oh yes Hans said it right it specifies the variables to be used as returning output from. and the data types of returning variable and the function should be same. In some function we write commands to be run and nothing to be returned hence there is no need of return command
4th Nov 2016, 3:56 PM
Sandeep Chatterjee
0
It states which variable should be the output of the function. For example: int calculateApproxAge(int birthYear){ int approxAge = 2016 - birthYear; return approxAge; } will allow the function to know that it needs to output the approxAge variable. Also note that the approxAge variable should have the same data type as is defined at the start of the function. In this case, that data type is integer.
1st Feb 2016, 4:15 PM
Hans
0
Let's say you have function main, and function xyz. Function xyz is an int return, so when it is tested [if (xyz() == 1)] xyz will return the int you selected. It can be used in if statements
6th Apr 2016, 4:36 AM
Tyler Nijmeh
Tyler Nijmeh - avatar
- 1
it's like the sign to the compiler that It is the end of the program
10th Jul 2016, 9:30 AM
Chaitanya Naidu
Chaitanya Naidu - avatar