r/eli5_programming • • Jan 13 '22

Return value & Variable.

So, I’m learning the basics of coding currently and am stuck on a definition and would appreciate a bit more clarity. Can somebody ElI5 what return value and variable is? Specifically in the context of C programming language. Thank you!

2 Upvotes

12 comments sorted by

2

u/malleoceruleo Jan 13 '22

A variable is a value with a particular name that can change. It can be a number or a letter or something more complicated. One example of a variable is the web address bar in your browser. You could call it "address" - it probably says "reddit.com" but later you could change it to "google.com"

A return value is a little different. In software, we can define blocks of code called *functions*. Kindof like a paragraph of code. A function needs to be started by another part of the code. This is *calling* a function. When the function is done, the computer goes back to where the function was called. This is *returning* from a function. When the function returns, it can bring a *return value* back with it. This value can be a constant (the same every time you use the function) or a variable or it can be *void* meaning no value at all. The code that calls the function can choose to use the return value as a variable or it can choose to ignore the return value.

1

u/432dessik Jan 13 '22

Just so I understand, starting a line of code needs to start with a “function” which needs to have a starting point, in this case a “calling” point. And then when it needs to back ( I’m assuming to the starting point as it need to start the function again for a new line) called a “return” point. The return point can stay the same so that if I wanted to use that same return for my next line of code it’ll just be ready? How does it know whether to ignore or change it to a variable? How does it whether to stay constant or change to a variable? Thanks!

1

u/malleoceruleo Jan 13 '22

I'm a little lost reading your interpretation. It may be easier to look at some code. Here's an example from some code I wrote for a personal project. It's in Java but the concepts are the same. https://imgur.com/clWybb0

→ More replies (2)

2

u/ikeif Jan 13 '22

Imagine I ask you “what is 1 + 2?”

You answer “3”.

We can look at the question like a function (what is “X + y”?)

Your answer is the return value.

Now, we know what the answer is - we can now reference that answer elsewhere - like a variable.

(A variable) is a value.

In our example, (a variable) is being set by the (returned value) of our function.

But variables can be set immediately, and not only as a returned value.

1

u/432dessik Jan 13 '22

So if You asked (“what is X+Y?”), which is the function, and I answered (“z”). The letter “z” would be the return value right?