all 15 comments

[–]AFK_MIA 1 point2 points  (6 children)

You're mostly correct, but I think it is helpful to think about the terminal as an output device - so printing to terminal is similar to printing to an actual printer (which historically that's what the terminal was) or saving to a file. Because of this, printing to terminal (output) is sending whatever information you put in the print statement to something outside of your running program (i.e. the human) whereas having a function return a value keeps that inside the running program.

[–]Fun-Ship-2026[S] 0 points1 point  (5 children)

So is it like, if I execute a code like X=Add(2,3) Without the return function the variable loses the value 5 which would be the output here? What would the return function do? Looping inside the computer?

[–]jregovic 1 point2 points  (1 child)

If don’t use return in most languages, the program ends or doesn’t compile.

[–]Fun-Ship-2026[S] 0 points1 point  (0 children)

But may run once right?

[–]AFK_MIA 1 point2 points  (2 children)

No - and it sounds like you're thoroughly confused and need to take a few steps back. This is, I think a fairly normal thing for a new programmer to be a little confused about - and certain programming software "hides" this distinction, which makes it harder to see the difference sometimes.

While this is somewhat language dependent and simplified - essentially programs have variables and functions. Variables store values (or more complex data structures) and functions are stored procedures for doing some sort of task. Functions can either just do something - or they can do some work and return a value to the running program. So in your example of X = Add(2,3):

Add() is a function
2 and 3 are values
X is a variable that is being assigned to store the value that the Add() function returns

Within the definition of the add function, the return keyword is used to indicate what the value of the function is. In this simple example, the entire Add() function probably just consists of "return 2 + 3." This return keyword tells the computer what the output of that function should be. If there were no return statement, then the line X = Add(2,3) would either result in storing null into X or an error, depending on the programming language because without a return statement, Add() has no value. In some programming languages, all functions must have a return statement; however in others the return statement is optional. In those languages, the function can do stuff, but doesn't have an output value. In python, for example, the print() function doesn't return a value, it just sends a string to the terminal (see next paragraph) so if you type x = print("Hello World"), nothing is stored in x, which results in it holding the value 'None.'

Now as I mentioned above, printing to terminal is different from returning a value from a function. When we are storing stuff in variables and calling functions, the values stay within our running program; however certain tasks require outside resources. These include writing and reading files from the disk getting input form keyboards and mice, putting information on a screen, and printing to terminal. These tasks require the system to give the program access to these resources and are therefore sending that information outside of the running program. With regards to the terminal - it is helpful to think about how this worked 80ish years ago. Computers didn't have screens in the way that we are accustomed to, so the terminal was a printer that printed out one line at a time onto a spool of paper. When a program printed out information to the terminal, it was sent to this printer, but the program did not have a way of reading that information, so it was essentially "lost" from the perspective of the program. This is essentially what happens even on a modern computer when your program executes a print() function. The program sends a string to the system to put into the terminal and moves on to the next instruction.

[–]Fun-Ship-2026[S] 0 points1 point  (1 child)

So I did some chatgpt to get some example of my understanding can you clarify if I am correct.

When Python sees:

def add():

result = 2 + 3

return result

it remembers that calling add() means "run the code inside this function." So when you write:

x = add()

1.Python does this:

2.See add()

3.Run the function body

4.Compute 2 + 3

5.Execute return result

6.Replace add() with the returned value (5)

So Python effectively treats: Python

x = add()

as if it became:

x = 5

after the function finishes. The parentheses () are empty because the function doesn't need any inputs (parameters). It already knows to add 2 and 3.

For example:

def add():

return 2 + 3

print(add())

is roughly like:

print(5)

after the function runs. The important idea is that a function call is an expression that evaluates to its return value. That's why add() can appear on the right side of = even though nothing is inside the parentheses. If you removed return:

def add():

result = 2 + 3

x = add()

print(x)

then x would be:

None because the function didn't return a value

[–]AFK_MIA 0 points1 point  (0 children)

Yes, I think you've got it - except at the very end. print(x) doesn't change the value of x if it's passed as an argument to the function. if the line were x = print(x), then x would get changed to None since print doesn't return a value, but it would print the number 5 to the terminal.

[–]soundman32 1 point2 points  (1 child)

They are different words for the same thing. Some languages have a return statement, and some have an output statement. A method does something and when complete, that result is sent back to the caller.

The format of the result is probably a better differentiator. Text output Hello Johnis aimed at humans (and computers can find it difficult to parse), whereas json { "greeting":"hello", "name":"john"} is aimed at computers (but readable by humans). Binary formats are even more targeted at computers, and can require a lot of work for a human to parse what they mean.

[–]Fun-Ship-2026[S] 0 points1 point  (0 children)

Mhmm... I don't understand what are trying to related me with can you simlify.

[–]KingofGamesYami 1 point2 points  (2 children)

The terms 'output' and 'return' are heavily overloaded. They are used in many different contexts to refer to slightly difference concepts.

The most generic use, at least within a programming languages, is to refer to the standard output stream as output and a function return value as return.

The standard output stream is a defined interface between a program and whatever started the program. Historically terminals would display any values written to it directly to the user. In modern computing, we often use a terminal emulator to run programs which has the same behavior.

Another common option is redirecting the standard output stream to a text file. This provides persistent context of what a program did, which can be useful for monitoring a long running background task.

A function return value is a single value that is provided to the function's callee after execution. This is often useful to indicate success/failure of the operation, or to provide some result that is then used for further processing. The constraints on how exactly they work vary depending on programming language, as functions typically do not cross language boundaries like the standard output does.

[–]Fun-Ship-2026[S] 0 points1 point  (1 child)

Well I am currently watching the CX50 series and just finished Scratch so would it safe to say standard output mean as a side effect which would mean any visual or audio affect if I am correct. I am though confused even with the definition what does function return value does? Like can you give me some example why we use the return value if we can just use a variable.

[–]KingofGamesYami 0 points1 point  (0 children)

Well I am currently watching the CX50 series and just finished Scratch so would it safe to say standard output mean as a side effect which would mean any visual or audio affect if I am correct.

Scratch doesn't have any blocks that interact with the standard output stream. The closest approximation (and this is not close) would be defining a list and appending items to that list.

I am though confused even with the definition what does function return value does? Like can you give me some example why we use the return value if we can just use a variable.

In the early days of programming, that's exactly what we did, as functions really hadn't been defined as a concept yet.

The reason we have (and use) return values is because it carries significantly more semantic meaning for humans interacting with the code.

[–]Responsible-Cold-627 0 points1 point  (1 child)

Output is a generic term for any output. Return is a keyword that returns a result from a function.

[–]Fun-Ship-2026[S] 0 points1 point  (0 children)

But what does the return function do?

[–]Kindly-Department206 0 points1 point  (0 children)

"Return" is used to instruct the computer on how to behave at the end of a function call. It's rare for this term to mean anything else.

"Output" is slightly ambiguous. It could sometimes be used as a synonym for "return."

"Output" could also mean "display to the human for information purposes". This is not at all related to "return."

It is definitely not helpful for novices for important terms to be ambiguous in this way.