0

Read description about return in c++↓↓

I always see people use return in different ways as they say it gives us back a result in calculation but it behaves the same cout does because all the return does is return a result same as printing it. So why is it useful and how can I use it ? Because if I print the result it will give me the answer and if i returned the result it will also give me the result so why do we use it ?

31st Jul 2019, 9:10 PM
The unknown 321
The unknown 321 - avatar
10 Réponses
+ 2
Return is used within functions. Consider a simple function that adds two numbers: auto add( int a, int b) { // cout << a + b; or return a + b; } Of course you could print the value to the console, making it visible to the user, but then you lose the value internally, since you can't access it anymore once the function ends. But what if you want to use the value in another calculation afterwards? This is exactly where return kicks in, because now you can assign the addition result to a variable and use it further in your program. By only printing it, you lose the value. So as you can see, return is important for passing around values inside your program for further actions like calculations.
31st Jul 2019, 10:30 PM
Shadow
Shadow - avatar
+ 2
Why you lose the value internally? Operations like adding, e.g. 3 + 2, create a temporary value (also called an RValue, if you want to look it up). If that temporary value is not stored in memory, it is discarded once the line is executed, and you can't access it anymore, hence it is lost. How can this happen? A computer won't just store every single value you ever compute throughout your program, that is what variables are for in the first place: To store a value in order to use it later on. How can't I access it anymore? This is just like your brain works. You are flooded with countless information a day, too many for your brain to process and store. And if something is not stored in your brain, you can't remember that information. So the two of them might act similar if you only want to output the result, however, if you want to use the result again for new computations, they are not. Here is an example code for clarification: https://code.sololearn.com/c7c57TuQfVNW/?ref=app
1st Aug 2019, 7:41 AM
Shadow
Shadow - avatar
+ 2
If you create a variable, then yes. What I mean is that this 3 + 2; creates a value, but you don't create a variable, hence the value is not stored. For the second point, have you actually tried creating a variable inside a function and referencing that variable outside the function?
1st Aug 2019, 7:58 AM
Shadow
Shadow - avatar
+ 1
That something like the following won't work: void add( int a, int b ) { int c = a + b; } cout << c; And int d; void add( int a, int b ) { d = a + b; } would only work if 'd' was declared in the same scope as add(), however, it would have to be a global variable in this case, or add() would have to be declared inside main(), where both cases aren't considered good practice. The important thing here is scope. Variables only exist in their scope. A variable created inside a function doesn't exist on the outside, because functions have a local scope. If you are not familiar with scope, look it up. The course here sadly doesn't teach scope, although it is an important programming principle, not only for C++. The point of return is to pass a value from the inside of a scope to the outside, so it can be used there, without having to use global variables.
1st Aug 2019, 8:18 AM
Shadow
Shadow - avatar
+ 1
Okay, we are currently spinning around, lol. First of all, I didn't say return is used within functions that add two numbers, I just stated return is used within functions. My problem is I don't understand why you want to print everything to the console. As I tried to explain earlier, if you just want to output the result, both methods are fine and you can go for cout, but when you want to use the value later on again, use return, because it allows for writing int a = add( 3, 2 ); and then you can do more stuff with it. Like, that's it. If I confused you all the other stuff, then I'm sorry, that was background information I thought might be useful to have, but that is what it comes down to.
1st Aug 2019, 10:16 AM
Shadow
Shadow - avatar
0
Why you lose the value internally ? How can this happen ? And how can't I access it anymore ?
1st Aug 2019, 4:56 AM
The unknown 321
The unknown 321 - avatar
0
Because even if I returned the value outside the function I can do the same thing with printing it.
1st Aug 2019, 4:58 AM
The unknown 321
The unknown 321 - avatar
0
A computer won't just store every single value. Then why are they saying whenever I create a variable the computer finds a valid space in the memory to store its value ? To store a value in order to use it later on I can do this when I already have my variable declared why would I return it ?
1st Aug 2019, 7:51 AM
The unknown 321
The unknown 321 - avatar
0
Have you actually tried creating a variable inside a function and reference it outside the function. What do you mean by this ?
1st Aug 2019, 8:04 AM
The unknown 321
The unknown 321 - avatar
0
Of course it won't work because first you must write cout <<c inside the add function not outside. In main all you have to do is just write add(then your 2 numbers) and of course d must be inside the add function. But we didn't return the value here ? I know because of void but you said return is used within functions that adds 2 numbers. Why didn't we return the result. We print it out and the same result came.
1st Aug 2019, 8:36 AM
The unknown 321
The unknown 321 - avatar