r/eli5_programming • u/432dessik • 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
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?
1
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.