110 likes | 209 Views
Week 12. Methods for passing actual parameters to formal parameters .
E N D
Week 12 Methods for passing actual parameters to formal parameters
People are more concerned about the value status of actual parameters before calling and after calling than other issues. In some situations we wish no changes taking place with the actual parameters after the calling manipulations; in other situations we wish the changes taking place after the calling manipulations, or the original values of the actual parameters would be substituted by the new values. In general there are three methods to pass actual parameters to formal parameters in the calling process, which have different effects on the value status of actual parameters. These methods are • call by passing values to formal variables. (values = actual parameters) • call by passing values to reference variables. (values = actual parameters) • call by passing addresses to pointer variables. (addresses = addresses of actual parameters)
Call by passing actual parameters to formal reference parameters
Reference variables introduced in C++ only • A reference variable is a new type of variable that is declared as a second variable relative to the first variable. The reference variable has the different name from the first variable but shares the same memory address with the first variable. The reference variable need to be declared, so the compiler knows which is the first variable and which the second (reference) variable. For example, int a=5; int &m=a; • m is declared as a reference variable to the first variable a. Both m and a share the same address despite the different names. In nature a reference variable is the first variable except the different name. So m is 5 when a is assigned with 5.
Reference variables • Any other declared variables, e.g., n, can be made “equal ” to a but n and a have different addresses. The following program illustrates the difference.
Mechanism of formal reference parameters’ change leading to actual parameters’ change • From the discussion of the last subsection, we may see that if people want to swap two given values by calling a function with the swapping functionality, they may treat the given values as the first variables while the arguments as the second variables to accomplish this job. • In the parameter passing process, both the first and second will be equalled and share the same address. As a result of this, when the defined function swaps the formal parameters (reference), the actual parameters (first variables) will be updated as well because they share the same memory addresses.
Call by passing actual parameter addresses to pointer variables of formal parameters • the addresses are always with pointer variables. If we take the addresses of the given values as the actual parameters while the pointer variables as the formal parameters. In the calling process, let the pointer variables of the formal parameters point to the actual parameters. Now loot at what is happening. • The addresses are used to hold the variable values. If the addresses are called, then the values in the addresses will be accessed. Therefore if the addresses (&a, &b) of the actual parameters are passed to the formal parameters (pointer variables) int *x, int *y, the stored values will be passed to *x, *y. The function will swap *x, *y; but the addresses &a, &b remain unchanged. The mechanism is illustrated in the following program.
Call by passing actual parameter addresses to pointer variables of formal parameters