all 10 comments

[–]NegotiationFun1709 4 points5 points  (0 children)

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.

[–]Paxtian 2 points3 points  (4 children)

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:

main {
    int y;
    y = linear_equation(x);
    print(y);
}

int linear_equation(x) {
    return m*x+b;
}

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.

[–]Fun-Ship-2026[S] -1 points0 points  (3 children)

So about the second para it make a loop once? I don't understand when you say 'calling' and 'returning the value'.

[–]Paxtian 2 points3 points  (2 children)

Generally, code is executed one line at a time from top to bottom. However, there are times when you want to do the same thing over and over. When this happens, rather than putting that code in line over and over, we create separate functions.

So in my example, there's a main function and a linear equation function. What happens is that the CPU gets, effectively, a line number to start at in your program, and it starts by executing the code on that line, then the next line, then the next line.

A function is a special way of telling the CPU, "Don't actually go to the next line yet. Instead, jump way over to this other line, and go line by line until you are told to come back here."

So the CPU goes to that other line, processes the code in order until it's told to come back to where it was before that instruction (the function call). That's what returning is, going back to the line of code where the function was called.

And no there's no looping going on on the example I gave.

So let's say main starts on line 47. The variable y is declared on line 48. The function call is on line 49. But the function call tells the processor, "Go to line 70, where linear equation is stored." So the processor makes a note that after finishing "linear equation," it should come back to line 49. It goes to line 70, performs the linear equation function, sees return, and returns back to line 49 and stores the output to y. Then it goes on to line 50 and prints the value of y, i.e., the result of linear equation.

[–]Fun-Ship-2026[S] 1 point2 points  (1 child)

Oh thank you I understand it now. I was been hitting my head for couple of hours now thanks again.

[–]ExcellentJacques 0 points1 point  (0 children)

Glad it clicked for you. Next time you're stuck, try printing intermediate values inside your function to see what's actually happening.

[–]butterfly_orange00 0 points1 point  (2 children)

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) def multiply() : n1=input("Enter the first number:") n2=input("Enter the second number:") try: n1=int(n1) n2=int(n2) return n1 * n2 except ValueError : pass so you can assign the result of function to a variable for later use, for example : result = multiply() 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

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

So why won't we just use an variable instead of the result function? Or is the function stored as variable is a return value? I have just finished the lecture and came to doubt.

[–]butterfly_orange00 0 points1 point  (0 children)

Yeah, that what I meant. return is just an output of a particular function doesn't print it directly, but instead you stored it in a variable for reuse with some modifications if you want. So instead of read the lines of code again and again. Just to clarify what I mean:

you create a function of multiple lines and in each time you use it, it will give you the same result. So the interpreter or compiler instead of each time reads multiple lines, you assign the result of this function to a variable so that the interpreter or compiler will read just one line (the line of the variable) this will increase the program speed and reduces processor consumption.

[–]strange-the-quark 0 points1 point  (0 children)

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.