D
D
Denis Karakchiev2017-02-20 16:33:09
Programming
Denis Karakchiev, 2017-02-20 16:33:09

What is the difference between returning a value and assigning?

An example from a popular JS tutorial:

For example:
var i = 1;
var a = ++i; // (*)
alert(a); // 2

On the line (*), calling ++i will increment the variable and then return its value to a. So a will get the value of i after the increment.
The postfix form i++ differs from the prefix ++i in that it returns the old value before the increment.

1. Where is the line between returning a value and assigning? Why was it necessary to create these two terms returnand =?
2. If I understand correctly, then in essence, returning a value is assigning it to a variable from which it is taken?
3. Is it always returned to a variable?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
S
Saboteur, 2017-02-20
@uokersam

in "i = 1" you just assign the value to the variable i.
in "a = i++" you call a separate subroutine that will find the value in the variable i, add one to it, save the changed i, which will now be one more, and then return the value, and this returned value will be assigned to the variable a
Feel the difference ?
Well, yes, finish reading the textbook, the question is elementary.

A
Abdula Magomedov, 2017-02-20
@Avarskiy

var a = 1;
var b = 1;
alert(++a); // 2
alert(b++); // 1

Run the code and feel the difference. And for what it may be needed, you will find out later.

N
nirvimel, 2017-02-20
@nirvimel

Faced with similar questions, the creators of one of the programming languages ​​decided to completely exclude from the language such a thing as assigning values ​​to variables. At the same time liquidating as such the concept of a variable.
As a result, the language became cleaner and easier. And the number of questions that arise when studying it has noticeably decreased.

N
nrv, 2017-02-21
@nrv

1) Assigning and returning a value are two different things.
An assignment is an assignment to a variable of the value returned by the expressions to the right of =.
That is, there is an expression that returns a value as a result of evaluating this very expression. For example, (a + b + c) / d. Unfortunately, an expression can not only return a value, but also change the value of the variables that participate in it. Like i++ and ++i. But this is bad coding style (my personal opinion). Return is no longer the return of a value, which was discussed above. This is an operator that specifies what value will be the result of executing the function. Just when it says return 0; then this is called returning the value of the function.
2) Crap, read the answer to paragraph 1
3) Return of value by expression can be made in anywhere. For example, it just says i++. But if this example makes sense, then if you just write a + b, then at least it’s possible, there’s no point.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question