0
Function in c++
why we need to write "return" in the end of function? example: int addNumbers(int x, int y) { int result = x + y; return result; } int main() { cout<<addNumders(1,2)<<endl; } if I will delete "return result" and run, then I will get equal answer
7 odpowiedzi
+ 3
You need to write the "return" keyword to tell the function what to return.
Here, as you pointed out, it returns the right value if you don't put the keyword but it's only because you have only one integer inside your function. With more complex functions, you can manipulate multiple objects of the type of your function, hence the need of this keyword.
Also, code written after the function returned something is not executed, so you can check some condition in your function and tell it to return something in some case or do some other things after that, in which way you could have multiple return in a single function.
For example:
int highest(int a, int b) {
if(a > b)
return a;
return b;
}
here, the function will return a if it is higher that b and ignore the next line.
+ 2
No. Return is like turning in homework. Say, for instance, that you're in school and you've been given an assignment. If you don't turn it in, how will the teacher give you a grade?
Likewise, when you're calling a function, you're essentially giving the function a task to handle. The function uses the return statement to give you, the Webmaster, the completed assignment.
int doStuff(int x) {
return x -= 1;
}
int doThings(int x) {
x -= 1;
}
int main() {
cout << doStuff(5);
// Prints 4.
cout << doThings(5);
// Prints 0, or nil. No return statement.
return 0;
}
+ 2
if you delete "return result" and run, you will not get equal answer, we write "return" to give function result value .
+ 2
ype in the missing keyword to declare a generic type of data
+ 1
It shouldn't compile, much less run.
If it actually does compile and run, then you have your compiler to thank. It understood what you meant to do (return the value) and did it for you. I don't recommend letting your compiler guess what your code should be doing though. Rather, make sure your code has no such ambiguities.
If you set your compiler to be picky about C/C++ syntax then your code would not compile. You would get an error "Reached end of non-void function" or something like that.
If you want your code to be correct, which means that it follows the C/C++ standard, then you need a return statement. If you don't have a return statement when you need one, your code should not compile. However, if it does compile you could end up with a bug that didn't come from your own code but from how your compiler understood an ambiguity in your code. Be careful.
0
I'm not and expert so I'm going to keep it simple.
You declared 'int' as the return type in your 'addNumbers' function. This means you actually want your program to take the number that is assigned to a variable named 'result' and return it.
If you had declared the return type as 'void' instead then the program wouldn't expect a return.
Jacques
- 1