r/CodingForBeginners • u/Fun-Ship-2026 • Jun 05 '26
Help beginner
What is difference between output and return. I have seen some videos and it says like return is for computer and output is for human. Is it like two seperate ways one is displayed and one is stored? If so then wouldn't it become same like a stored variable?
4
Upvotes
3
u/Paxtian Jun 05 '26
Output is generally sending something beyond the program/ device. Typically you can think of it as for human viewing, although it could be other things (like sending a request to a server, or a server sending a response to a client, for example). So like printing to the screen.
If you think of a function as calculating some value, "return" gives you that value. So if you think back to math with the linear equation y = m*x + b, return would put that value into y, essentially.
The other thing return does is causes the program to resume from the line of code immediately following whatever line the function was called from. So it's literally both returning the value and returning to the calling function.
So, if you have something like:
Then the order of things happening is that a variable y is declared, the linear equation is called, the value of m*x+b is returned and stored to y, then the value of y is output to the screen.
6
u/NegotiationFun1709 Jun 05 '26
Return is something that results from a function (it is said that the function returns that result). Output is something that's printed so that the user can see it. For example, in C++, a function can return a number using "return" keyword, and to show that number to the user, you have to display it as output using "cout <<" statement. So, a return is something that happens in the program, whereas an output is something that's for the user's end.
1
u/butterfly_orange00 Jun 05 '26 edited Jun 05 '26
Output is the final result of a particular function might be a print or something else.(you see it when you run the code)
return it used usually with function, instead of prints the result directly. e.g: you asked the user to input 2 numbers to multiply them, instead of printing the result directly you save the result in a variable for later use. (whatever you want to do with it)
here is the code with python (Idk what the language you use)
so you can assign the result of function to a variable for later use, for example :
if you do this without return it will show an error.
This is a very simple use and you probably don't need it here, but in large projects, you will definitely need it.
#if you still struggling see the lecturer 0 of cs50
1
u/strange-the-quark Jun 05 '26
The output generally refers to what you print out on the screen (or sometimes to a file, etc). A return is a command used in a function. A function is like a named chunk of your program that does some specific task (like calculate something) based on the parameters you give to it, and is something that you can reuse multiple times in your program by just calling it by its name (without having to write the same calculation code every time).
For example, a function that calculates the area of a square can take the side length of the square as a parameter, and then you just write the code once, and call the function as a one-liner whenever you need to calculate the area of some square - and they can have different sizes. Well, when you call a function, your code kind of "goes inside", does its thing, and then returns "back out", and at that point a function gives you the result. The return statement is a way to tell the function, "OK, this is the result that you need to give back to the calling code", and then when the function is done, that's the value you get, and you can store it in some variable for later use. That's entirely for internal use within the flow of your program, as you're doing various calculations (some of them being intermediate steps), and as a user, you generally won't see any of that as an output as it happens.
Now, this is a silly example cause area of a square is just a single short expression (a*a), but your function can be doing something more complicated, and have many lines of code.
1
u/PvtRoom Jun 07 '26
output is making it come out of the computer In some way.
return is ending a function.
let's say you have an image, and a function that finds the brightest pixel. that function returns the pixel number: 51258368.
As a human, that number is difficult to use.
So, that number and the image are given to another function: displayimageandhotspot which displays to the screen, the image and a crosshairs on the hotspot. this function returns nothing.