+ 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
18 ответов
+ 3
//Javascript code
function add(a, b) {
return a + b;
}
var sum = add(2, 3); // returns 5 to sum
//A simple illustration
+ 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.
+ 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.
+ 3
By the way bro, I've seen that the code also works without that line 🤔
+ 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);
+ 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
+ 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.
+ 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;
}
+ 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
+ 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...
+ 1
So return is similar to giving answer right ?
+ 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).
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?
0
Ayan this what confuses me too. I don't know why people use it if they don't need to.
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.
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
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?
0
So the value that it should return should only be integer number ?