Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Can somebody tell me what is the difference between "return" and "std::cout"?

+8 votes
asked May 3, 2023 by Eidnoxon (5,110 points)
Hi! I'm learning c++, and I'm kinda stuck. I'm learning c++ "return" keyword. It would be easy if in the youtube video the guy wouldn't do a 70 line project to explain it. I have visited several websites, but still, I couldn't understand. I watched several videos on youtube, still, I couldn't understand. So I was hoping that somebody could explain it to me. Actually, I have been testing, and I have just realized that the "return" and the "std::cout" function has nothing to do with each other. I have tried this:
myFunc(int a){

        std::cout << a << endl;

}

and

myFunc2(int a){

        return a;

}

"myFunc" worked, but myFunc2" didn't. Please explain it to me.

5 Answers

0 votes
answered May 4, 2023 by Teddy (330 points)

MyFunc2() returns A. when you return something you are basically giving MyFunc2() the value A, std::cout << MyFunc2() << endl; is going to print A, because MyFunc() is returning A.

example:

#include <iostream>

using namespace std;

int MyFunc();

int main(){
    cout << MyFunc();

    return 0;
}

int MyFunc(){
    return 21;
}

Output:

21

commented May 7, 2023 by Eidnoxon (5,110 points)
I feel so stupid right now lol thanks
0 votes
answered May 4, 2023 by Hardik (140 points)
when you tried to return it doesn't print the value and just return it where you called the function actually so its your wish that you can use it to print and also use it in some other operations but if you like using the syntax num=myFunc2(9);

then this will assign num the value of a that is 9 and you can simply print it like

std:cout<<num;

output will be 9 or you can instead just use some operations if you want now.
0 votes
answered May 5, 2023 by G-Loco22 (250 points)
Hello ! Fellow learner here!!

Ok so cout is basically having a look and reading what's there without doing anything else. returning is getting that value / data and bring back into another part of the programme.

It's a bit like you reading the menu of a restaurant, vs bringing home an item from the menu.
0 votes
answered May 5, 2023 by G-Loco22 (250 points)
If you want to return a value, you need something for it to ride with. These are the declarations before the function name. Int, string, etc etc . You are saying: This function is going to do some stuff, and return it in the value type I've added before the function name. You can't hitch a ride with no wheels.

Functions can do stuff without returning any data. EG. just print out a message. Or they can do stuff and return data and values back into the wider programme. EG. adding points to a game, maybe doing some math and returning the calculations.

I got confused with cout vs return because I didn't understand that cout was looking inside and reading what it saw. return was taking that data and dumping it into another part of the program.

with a function that returns a value, you need to express in this way:

int function_name(int a)

{

return a;

}

//int - This is the value type I want this function to grab and return back to the program. You choose which type to use.

// function_name -this is the name of the function - replace with your own

// stuff in brackets (this is the data type I will be adding into the function- numbers, words, characters, true/false etc)

// curly brackets - this is what my function will do.

//return ... (also inside the brackets: this is what data I want to transport back to the bigger programme with my value type I told you I wanted to use at the start of the function declaration.  

{

This is the stuff my function is going to do. Here is a template for the programme to follow.

Please take this value, and take its value back to the programme.

}

{

return a;

}
0 votes
answered May 5, 2023 by Fluffyfuffy (420 points)
When you use std::cout, you are printing someting to the terminal.

#include <iostream>

int main() {

     std::cout << "this line prints to the terminal!";

}

This program would print: this line prints to the terminal!

Return is a bit different. lets say you want to make a function that adds 2 numbers together and gives the result back. The function would look somewhat like this:

int addFunc(int a, int b) {

int c = a + b;

return c;

}

Once the function finishes, the return value is left. So if you do std::cout << addFunc(1, 2); It would print 3 because the function adds 1 + 2, then returns the answer back to where the function was called.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...