I've checked Wikipedia and googled but I still can't wrap my mind around how pass-by-name works in ALGOL 60.
Pass by name : This technique is used in programming language such as Algol. In this technique, symbolic “name” of a variable is passed, which allows it both to be accessed and update. Example: To double the value of C[j], you can pass its name (not its value) into the following procedure.
When you pass an argument by name, you specify the argument's declared name followed by a colon and an equal sign ( := ), followed by the argument value. You can supply named arguments in any order. When you call this procedure, you can supply the arguments by position, by name, or by using a mixture of both.
Disadvantages: Repeated evaluation of arguments can be inefficient. Pass-by-name can have unsafe semantic effects. Pass-by-name is difficult to implement.
In contrast to, e.g., C , where we have pass-by-value and pass-by-reference when we pass and assign variables, python uses what is called often referred to as pass-by-name .
I found a good explanation at Pass-By-Name Parameter Passing. Essentially, the body of a function is interpreted at call time after textually substituting the actual parameters into the function body. In this sense the evaluation method is similar to that of C preprocessor macros.
By substituting the actual parameters into the function body, the function body can both read and write the given parameters. In this sense the evaluation method is similar to pass-by-reference. The difference is that since with pass-by-name the parameter is evaluated inside the function, a parameter such as a[i]
depends on the current value of i
inside the function, rather than referring to the value at a[i]
before the function was called.
The page I linked above has some more examples of where pass-by-name is both useful, and dangerous. The techniques made possible by the pass-by-name are largely superseded today by other, safer techniques such as pass-by-reference and lambda functions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With