+ 1

I don't understand what is the meaning of this "return". They say it return the value but what value?.

The value of return

21st Jun 2019, 3:55 PM
The unknown 321
The unknown 321 - avatar
18 ответов
+ 3
//Javascript code function add(a, b) { return a + b; } var sum = add(2, 3); // returns 5 to sum //A simple illustration
21st Jun 2019, 3:59 PM
Yathin Babu
Yathin Babu - avatar
+ 3
Actually, return just defines that how the function will work. In Luk's answer, if you remove the return keyword, the result of function will be undefined.
21st Jun 2019, 4:05 PM
Ayan Fox
Ayan Fox - avatar
+ 3
Oh, so that's your real question !! Here you go : http://www.fredosaurus.com/notes-cpp/functions/return.html#targetText=Return%20statement,middle%20of%20a%20loop%2C%20etc.
21st Jun 2019, 4:16 PM
Ayan Fox
Ayan Fox - avatar
+ 3
By the way bro, I've seen that the code also works without that line 🤔
21st Jun 2019, 4:18 PM
Ayan Fox
Ayan Fox - avatar
+ 2
Function does stuff and sends the information back to were you called it //my function Function call(){do stuff and return the outcome} //Calling the call function⬇️ Call(go to that function up there ⬆️ take this this string "hello" and bring back a reversed version of it and use it);
21st Jun 2019, 7:13 PM
D_Stark
D_Stark - avatar
+ 1
Luk people say return means that your program ran successfully without errors that's what people say but programmers use it differently just the way you did. That's why I'm confused
21st Jun 2019, 4:05 PM
The unknown 321
The unknown 321 - avatar
+ 1
The unknown 321 I see, you're confused why programs end with "return 0"? Because usually when program ends successfully we make it return 0, in the same way as function does. When something bad/unexpected happens and it has to exit, you make it return other numbers, for example error code (404 etc.). Then something else can read it, for example program which tests for errors. There's nothing too complicated, it's just for readability and that's just the way programmers do it.
21st Jun 2019, 4:13 PM
Luk
Luk - avatar
+ 1
"Luk people say return means that your program ran successfully without errors..." In that case, they refer to the value being returned by the `main` function. The way every C/C++ program informs the environment (operating system) that the expected task(s) done without any problem or interruption. As a cleaner and more readable syntax, there's an implementation-defined macro constant called `EXIT_SUCCESS` available in <cstdlib> library that can be used instead of 0 like so int main(void) { //... return EXIT_SUCCESS; }
21st Jun 2019, 4:17 PM
To Seek Glory in Battle is Glorious
To Seek Glory in Battle is Glorious - avatar
+ 1
https://code.sololearn.com/c1j8eNZW4lwz/?ref=app This is a calculation code. I didn't use return here because I wanted to see if it will result in error but nothing happened
21st Jun 2019, 4:35 PM
The unknown 321
The unknown 321 - avatar
+ 1
"So the value that it should return should only be integer number ?" No, not necessarily. It depends on what you're really expecting to be the return. You can have a similar function to return a floating-point value like double sum(double a, double b) { return a + b; } int main(void) { double a = 4.0; double b = 5.0; cout << sum(a, b); return 0; } Output: 9 It can be a string like string sum(string a, string b) { return a + b; } int main(void) { string a = "Hello, "; string b = "World!"; cout << sum(a, b); return 0; } Output: Hello, World! Etc...
21st Jun 2019, 5:25 PM
To Seek Glory in Battle is Glorious
To Seek Glory in Battle is Glorious - avatar
+ 1
So return is similar to giving answer right ?
21st Jun 2019, 5:28 PM
The unknown 321
The unknown 321 - avatar
+ 1
"So return is similar to giving answer right ?" Yes. In fact, a tangible analogy that I can think of for describing a function is a black box (maybe a TV) which the "parameter list" is like a power cable or audio/video inputs and the "return statement" is like the screen itself that you can see the "result" of connecting the required inputs (power and data via cables).
21st Jun 2019, 5:40 PM
To Seek Glory in Battle is Glorious
To Seek Glory in Battle is Glorious - avatar
0
From your profile I assume you talk about C++. "return" returns whatever you want. You can make a function: int add(int x, int y) { return x+y; } Your question is not clear. Problem with something specific?
21st Jun 2019, 3:59 PM
Luk
Luk - avatar
0
Ayan this what confuses me too. I don't know why people use it if they don't need to.
21st Jun 2019, 4:26 PM
The unknown 321
The unknown 321 - avatar
0
The unknown 321 you don't have to use it. For small programs It's just about coding standards. In bigger projects like a full program or a game it may be important.
21st Jun 2019, 4:29 PM
Luk
Luk - avatar
0
"Ayan this what confuses me too. I don't know why people use it if they don't need to." Before C99 standard, if you had a simple C program like the following #include <stdio.h> int main(void) { printf("Hello, world!\n"); /* return 0; */ } where the return value had been commented out or omitted, the compiler did frown by issuing " warning: control reaches end of non-void function [-Wreturn-type] " which means there's a hefty chance that something very wrong would happen. Because of that, For being compatible with the previous standard (In C), it's considered good practice even people with a C++ background usually tend to use `return 0;` or the equivalent that I mentioned earlier extensively. Even though in C++, the compiler would implicitly include it at the end of the `main()` Live demo: https://rextester.com/FIV71343
21st Jun 2019, 4:48 PM
To Seek Glory in Battle is Glorious
To Seek Glory in Battle is Glorious - avatar
0
"I didn't use return here because I wanted to see if it will result in error but nothing happened" That's because main() is always "client" and as I mentioned it only returns value to the operating system and not to the screen or another function in the codebase. It clearly implies that you should define another function to meet your expectation. Something like int sum(int a, int b) { return a + b; } int main(void) { int a = 4; int b = 5; cout << sum(a, b); return 0; } Is it clear now?
21st Jun 2019, 5:06 PM
To Seek Glory in Battle is Glorious
To Seek Glory in Battle is Glorious - avatar
0
So the value that it should return should only be integer number ?
21st Jun 2019, 5:10 PM
The unknown 321
The unknown 321 - avatar