next up previous contents
Next: Implicit Interfaces Up: The Building Blocks of Previous: External Functions   Contents

Actual arguments, formal (dummy) arguments and local variables

The function is invoked by its name, followed by a list of actual arguments. When the call is issued, in a special memory structure, called the program stack, 5 new locations are reserved; the addresses of the 3 arguments a,b,c are pushed, in this order, into the stack. The location contains the address of the real variable norm2, which was declared in the main program and is expected to hold the result of the function calculation. The location is dedicated to the local variable loc, about which we will talk later. The control is then transferred from the caller (here, the main program) to the called function norm2. The function ``sees'' the stack; from the function perspective, on the stack are (top to bottom): result variable address, argument z address, argument y address, and argument x address. The formal (dummy) names x,y,z are therefore just aliases for the actual variable names, to be used inside the function body (we say that, when the funcion is called, formal (dummy) variables are replaced by actual variables). It is therefore required that the order in which the actual arguments are supplied is precisely the order of formal (dummy) arguments in the function declaration. Moreover, the type of actual argument should coincide exactly with the type its omologue () formal (dummy) argument.

The norm calculation is performed and the result stored in norm2 (the function knows the address of this variable also!). At this point the control is returned to the main program. Here the function call norm2(a,b,c) is simply replaced by the value found in the result variable norm2, and the computations in the main program proceed further.

@25<@@25 setsize @25<@@25 setsizeSetFigFont>@ pt@@ pt xxxxxxsplain


As presented here, the argument passing is said to be done ``by reference'', since references to, rather than values of arguments are put on the stack. Note that, in practice, the situation is more complicated; the actual parameter-passing mechanism can be somewhat different than what we show here. This is the case, for example, when the function is invoked with literal constant arguments (perfectly legal in F95)

print*, norm2(1.0, 2.0, 3.0)
Nevertheless, the above discussion is useful to understand the ``inner logic'' of the F95 function declaration and function call syntax.

The variable loc is called a local variable. Its scope is the body of the function. When the function is called, space is allocated on the stack for loc; at this point the memory location does not carry any useful information. When the control returns to the main program (the caller), the stack pointer is reset to its previous value, wiping out all the information on stack; loc is therefore destroyed when the function is exited. loc does not exist between function calls. Obviously, a new function call means a new incarnation of loc, unrelated to the previous one; in particular, loc cannot remember its value from the previous call.

Note that IMPLICIT NONE should be used in any function declaration just as it is used in the main program.


Subsections
next up previous contents
Next: Implicit Interfaces Up: The Building Blocks of Previous: External Functions   Contents
Adrian Sandu 2001-08-26