Computer Science
Parameter Passing
Parameter passing is the process of passing data or arguments to a function or subroutine during its invocation. There are different ways to pass parameters, including pass-by-value, pass-by-reference, and pass-by-name. The choice of parameter passing method can affect the performance and behavior of a program.
Written by Perlego with AI-assistance
Related key terms
1 of 5
3 Key excerpts on "Parameter Passing"
- eBook - PDF
- Arvind Kumar Bansal(Author)
- 2013(Publication Date)
- Chapman and Hall/CRC(Publisher)
4.4.7 Passing Subprograms as Parameters Languages supporting the functional programming paradigm such as Lisp, Scheme, Haskell, Ruby, and Scala, and many imperative programming languages, such as ALGOL and Pascal, support passing functions/subprograms as parameters. Passing functions as parameters involves passing the reference to a function’s first instruction and the reference to the corresponding environment of the function to check for the number and types of the argument at run time. This is quite difficult for statically typed languages—languages where type of the data entities is declared at compile time—because different subprograms can be invoked by different calls at run time, and the compile-time analysis has to make sure that the environment, including argument types received by the called function, is same as the environment and the argument types of the function passed as the parameter. In dynamically typed functional programming languages, this is not a problem, as type checking is done at run time. 4.4.8 Parameter Passing for Distributed Computing Distributed computing requires invocation of remote procedures on different processors. That means that different subprograms execute in different address spaces. Distributed computing uses three types of parameters passing: (1) call-by-moving, also called call-by-copy ; (2) call-by-reference ; and (3) call-by-visit, also called call-by-copy-restore . These Parameter Passing mechanisms are distributed address-space counterparts of call-by-value, Abstractions in Programs and Information Exchange ◾ 157 call-by-reference, and call-by-value-result in single-processor implementation of pro-gramming languages. The invocation of the remote procedure involves evaluation of the expression, relocating a copy of the object to the remote site and binding it to the formal parameter, executing the remote procedure, and returning the result or object back to the calling procedure. - R. Balakrishnan, Sriraman Sridharan(Authors)
- 2018(Publication Date)
- Chapman and Hall/CRC(Publisher)
in out parameter.A subroutine or procedure achieves its task either by changing one or more of its variable/reference parameters or by modifying some variables in the environment of the procedure or by modifying both.Name substitution : The actual parameter is substituted literally for every occurrence of the corresponding formal parameters. We don’t use this kind of Parameter Passing in our book.Table 5.9 illustrates the notations used in mathematics, in Pascal and in C .The following examples clarify the parameter-passing mechanism: Example 5.4.6 Value and variable substitution:Table 5.9 Notations in mathematics, Pascal, and CLet us write a subprogram computing the product of two positive integers m and n using only the operation of addition, and store the resulting product in the variable p .The algorithm behind this procedure is very simple: We have to decompose the product as a sequence of additions: Symbolically,Example 5.4.7 Variable substitution:m × n =.m + m + ⋯ + m⏞n t e r m sLet us write a procedure/function exchanging the contents of two integer variables, that is, ifandm = 5initially, then after executionn = 3andm = 3. What is the algorithm?n = 5Consider the problem exchanging the contents of two bottles of wine (one liter each) without mixing them. We need a third empty bottle of capacity one liter. First, we pour the contents of the first bottle into the third empty bottle, then pour the contents of the second bottle into the now empty first bottle, and finally pour the contents of the third into the now empty second bottle. This is the algorithm. A Pascal procedure to exchange two variables:- eBook - ePub
- Yuan Dong, Fang Yang, Li Zheng(Authors)
- 2019(Publication Date)
- De Gruyter(Publisher)
- Moving multiple plates from one pillar to another. This is a recursive operation.
- Moving one plate from one pillar to another.
The following program uses two functions to implement the two kinds of operations above – function hanoi for the first operation and function move for the second.Source code: Running result:Fig. 3.3: Hanoi Tower Game.3.1.3Passing Parameters Between Functions
Before a function is called, the formal parameters of this function neither take up any real memory space nor have real values. When a function call is made, the computer allocates memory for the formal parameters and assigns the actual parameters to the formal parameters. An actual parameter could be a constant, variable, or expression, and should match the type of the corresponding formal parameter (the parameter in the same position in its parameter list). Passing parameters between functions is the process of assigning formal parameters according to actual parameters. C++ has two ways to pass parameters: Call-by-Value and Call-by-Reference.1.Call-by-Value
The procedure of Call-by-Value consists of two steps: allocating memory space for a formal parameter, and using the actual parameter to initialize the formal parameter (assigning the actual parameter to the formal parameter). This procedure just passes the value of the actual parameter to the formal parameter. The formal parameter does not have any relation to the actual parameter once it has been initialized, and any change of the formal parameters afterwards cannot affect the actual parameter.Example 3.11: Swap and output two integers.Running result:Fig. 3.4: The status of variables when the program in Example 3.11 is running.Analysis: From the running result we can see that the values of variable x and y have not been swapped. It’s because in the function call above we use Call-by-Value to pass parameters, only where the values of the actual parameters are passed to the formal parameters. Thus the change of the formal parameters afterwards will not affect the actual parameters. Figure 3.4
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.


