1 / 11

Week 12

Week 12. Methods for passing actual parameters to formal parameters .

hamal
Download Presentation

Week 12

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Week 12 Methods for passing actual parameters to formal parameters

  2. 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)

  3. Call by passing actual parameters to formal parameters

  4. Call by passing actual parameters to formal reference parameters

  5. 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.

  6. 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.

  7. Reference’s changes leading to the first variable’s change

  8. 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.

  9. 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.

  10. Call by passing actual parameter addresses to pointer variables of formal parameters

  11. Summary

More Related