Nested function call: Difference between revisions
(Created page with "A convention used in many programming languages (python, javascript, C, liquidsoap) When you see: foo(bar(baz())) In fact what happens is: * baz() gets c...") |
No edit summary |
||
(2 intermediate revisions by the same user not shown) | |||
Line 4: | Line 4: | ||
foo(bar(baz())) | foo(bar(baz())) | ||
In fact what happens is: | In fact what happens is: | ||
* baz() gets called, and it's output * | * baz() gets called, and it's output * is used as input to: | ||
* bar( * ) which gets call, and it's output ** sent as input to: | * bar( * ) which gets call, and it's output ** sent as input to: | ||
* foo ( ** ) | * foo ( ** ) | ||
Using temporary variables, the | Using temporary variables, the chain of events could also be written: | ||
x = baz() | x = baz() | ||
Line 18: | Line 17: | ||
z = foo(y) | z = foo(y) | ||
Note that although written differently (in a more linear fashion), the resulting program does exactly the same thing. Since it's more compact, and doesn't introduce temporary variables, programmers tend to write: | |||
result = foo(bar(baz())) | result = foo(bar(baz())) | ||
Latest revision as of 07:01, 15 May 2020
A convention used in many programming languages (python, javascript, C, liquidsoap)
When you see:
foo(bar(baz()))
In fact what happens is:
- baz() gets called, and it's output * is used as input to:
- bar( * ) which gets call, and it's output ** sent as input to:
- foo ( ** )
Using temporary variables, the chain of events could also be written:
x = baz() y = bar(x) z = foo(y)
Note that although written differently (in a more linear fashion), the resulting program does exactly the same thing. Since it's more compact, and doesn't introduce temporary variables, programmers tend to write:
result = foo(bar(baz()))