Argument Passing by Position and by Name

The two-argument example of the Power subroutine passed the two arguments by position. The first value passed, 5, was assigned to the first argument, num, and the second value passed, 3, was assigned to the second argument, exponent. Thus, had the call of the subroutine been Power(3, 5) instead of Power(5, 3), the result would have been 35 instead of 53.

You also can pass arguments by name, without regard to position. 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. For example, if you call Power as follows

the resulting output ("5 to the power of 3 = 125") is the same as if you had called Power(5, 3).

Tip You can supply arguments both by position and by name in a single procedure call. However, as my mother told me when I was a child, "Just because you can do something doesn't mean you should do it." My advice is not to mix supplying arguments by position and by name in a single procedure call because it makes your code more difficult to read.

Was this article helpful?

0 0

Post a comment