|
|
|
| Kind of Parameter | Usage | | Actual parameter | Appears in a function call. The corresponding
formal parameter may be either a reference or a
value parameter. | | Formal value parameter | Appears in a function heading. Receives a copy of
the value stored in the corresponding actual
parameter. | | Formal reference parameter | Appears in a function heading. Receives the address of the corresponding actual parameter. |
|
|
|
|
|
|
Before we talk more about parameter passing, let's look at an analogy from daily life. You're at the local discount catalog showroom to buy a Father's Day present. To place your order, you fill out an order form. The form has places to write in the quantity of each item desired and its catalog number, and places where the order clerk will fill in the prices. You write down what you want and hand the form to the clerk. You wait for the clerk to check whether the items are available and calculate the cost. He returns the form, and you see that the items are in stock and the price is $48.50. You pay the clerk and go on about your business. |
|
|
|
|
|
|
|
|
This illustrates how function calls work. The clerk is like a void function. You, acting as the main function, ask him to do some work for you. You give him some information: the item numbers and quantities. These are his input parameters. You wait until he returns some information to you: the availability of the items and their prices. These are the clerk's output parameters. The clerk does this task all day long with different input values. Each order activates the same process. The shopper waits until the clerk returns information based on the specific input. |
|
|
|
|
|
|
|
|
The order form is analogous to the actual parameters of a function call. The spaces on the form represent variables in the main function. When you hand the form to the clerk, some of the places contain information and some are empty. The clerk holds the form while doing his job so he can write information in the blank spaces. These blank spaces correspond to reference parameters; you expect the clerk to return results to you in the spaces. |
|
|
|
|
|
|
|
|
When the main function calls another function, reference parameters allow the called function to access and change the variables in the actual parameter list. When the called function finishes, main continues, making use of whatever new information the called function left in the variables. |
|
|
|
|
|
|
|
|
The formal parameter list is like the set of shorthand or slang terms the clerk uses to describe the spaces on the order form. For example, he may think in terms of units, codes, and receipts. These are his terms (formal parameters) for what the order form calls quantity, catalog number, and price (the actual parameters). But he doesn't waste time reading the names on the form every time; he knows that the first item is the units |
|
|
|
|
|