What is the difference between cout and return while defining a function?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What is the difference between cout and return while defining a function??

for example what is difference between int myFunc(int a){ return a ; } and int myFunc(int a){ cout << a ; }

27th Feb 2018, 3:21 PM
Yogesh Yadav
Yogesh Yadav - avatar
2 Answers
+ 9
cout has nothing to do with the return value of a function. put very simple cout is a command which prints something to the screen and that's all.. it doesn't affect your program, it is simply an output.. While, you can see the return as the task of your function inside the code.. What will your function do for your code? int getStrength ( ){ return player_strength ;} ....some code.. .. cout << "Level up! Now your strength is " << getStrength(); here getStrength function returns the current strength of a player. This is a very simple explanation.. functions can do a lot of things but this is a simple basic point to start.
27th Feb 2018, 4:07 PM
AZTECCO
AZTECCO - avatar
+ 4
Example #1: "return a;" is returning the value back to the calling function so its value can be used outside of its relative function. Example #2: The function is printing the value of 'a' to your console output (cout).
27th Feb 2018, 3:26 PM
Fata1 Err0r
Fata1 Err0r - avatar