Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "pass-by-name" and how does it work exactly?

I've checked Wikipedia and googled but I still can't wrap my mind around how pass-by-name works in ALGOL 60.

like image 203
bdd Avatar asked May 08 '09 03:05

bdd


People also ask

What is pass by name in programming?

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.

How do you pass an argument in a name?

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.

What are two arguments against the use of pass by name parameters?

Disadvantages: Repeated evaluation of arguments can be inefficient. Pass-by-name can have unsafe semantic effects. Pass-by-name is difficult to implement.

Does Python pass by name?

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 .


1 Answers

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.

like image 101
Greg Hewgill Avatar answered Oct 01 '22 04:10

Greg Hewgill