Parameters can be classified into three groups or modes. They can be used to:
Ada identifies these as the in, out and in out parameter modes (note that in Ada, by default, a parameter is an in parameter). There are many mechanisms whereby the above can be implemented, these include:
Call by value is the most common and is used by languages such as
C,
Pascal, Modula-2 and
Algol-60.
Here the formal parameter acts as a local variable which is initialised with the value of the
actual parameter (which may be a variable, a constant or an expression) and may then be changed.
However, any changes made to the formal parameter will not effect the value of the actual parameter.
#include <stdio.h> void doit(int, int, int); void main(void) { int value1, value2, value3; scanf("%d %d %d",&value1,&value1,&value3); printf("Before doit: value1=%d, value2=%d, value3=%d\n",value1,value2,value3); doit(value1,value2,value3); printf("Afrer doit: value1=%d, value2=%d, value3=%d\n",value1,vlaue2,value3); } void doit(int num_a, int num_b, int num_c) { printf("Start of doit: num_a=%d, num_b=%d, num_c=%d\n",num_a,num_b,num_c); num_a = num_b*100; num_b = num_b+num_c; printf("End of doit: num_a=%d, num_b=%d, num_c=%d\n",num_a,num_b,num_c); }
In languages such as Ada (also Algol-68)
the formal parameter is a local constant rather than a local constant and thus can not be changed.
The latter mechanism is thus referred to as call by constant value.
with CS_IO; use CS_IO; procedure IN_MODE_EXAMPLE is VALUE_1, VALUE_2: float; procedure MEAN_VALUE (NUM_1, NUM_2: in float) is begin put_line("MEAN VALUE FUNCTION"); put_line("==================="); put("The mean is "); put((NUM_1+NUM_2)/2.0); new_line; end MEAN_VALUE; begin get(VALUE_1);get(VALUE_2); MEAN_VALUE(VALUE_1, VALUE_2); end IN_MODE_EXAMPLE;
The disadvantage of call by value (and call by constant value) is that a copy is always made of the actual parameter to obtain the formal parameter. The need to make a copy can be avoided using the call be reference mechanism. In call by reference the address of the actual parameter is passed, thus everything that happens to the formal parameter actually happens to the actual parameters. This is used by languages such as, Pascal, Modula-2 and Algol-68.
program PASSING (input, output); var VALUE_1, VALUE_2: real; procedure MEAN_VALUE (NUM_1: real; var NUM_2: real); begin NUM_2 := (NUM_1+NUM_2)/2; end; begin readln(VALUE_1, VALUE_2); write('VALUE_1 = '); write(VALUE_1); write(', VALUE_2 = '); write(VALUE_2); writeln; MEAN_VALUE(VALUE_1, VALUE_2); write('MEAN VALUE_2 = '); write(VALUE_2); writeln; end.
In the C programming language the effect of call be reference can be achieved by passing a reference as a value. Knowledge of this reference can then be used to alter the value held in it. This mechanism thus only simulates call by reference and should (more accurately) be referred to as call be reference value.
#include <stdio.h> void meanValue(float *, float); void main(void) { float value_1, value_2; scanf("%d %s\n",&value_1,&value_2): meanValue(&value_1, value_2); printf("Mean value_1 = %f\n",value_1); } void meanValue(float *num_1, float mum_2) { printf("num_1 = %f, num_2 = %f\n",*num_1,num_2); *num_1 = (*num_1+num_2)/2.0; }
Remember that the operator & is interpreted as "the address of ...". And the operator * when followed by a pointer is interpreted as "the variable pointed at by ...".
Call by result is used in Ada to implement out mode parameter passing. The formal parameter acts as an uninitialised local variable which is given a value during execution of the procedure. The value of the formal parameter is then assigned to the actual parameter on returning from the routine.
with CS_IO; use CS_IO; procedure OUT_MODE_EXAMPLE is VALUE_1, VALUE_2: float; MEAN: float; procedure MEAN_VALUE (NUM_1, NUM_2: in float; NUM_3: out float) is begin NUM_3:= (NUM_1+NUM_2)/2.0; end MEAN_VALUE; begin MEAN_VALUE(VALUE_1, VALUE_2, MEAN); put("The mean is "); put(MEAN);new_line; new_line; end OUT_MODE_EXAMPLE;
Call by copy restore (also known as call by value result) is an amalgamation of call by value and call by result. The formal parameter acts as a local variable which is initialised to the value of the actual parameter. Within the routine, changes to the formal parameter only affect the local copy. On returning from the routine the final value of the formal parameter is assigned to the actual parameter. Call by copy restore is supported by Ada to achieve "in-out" parameter operation. It has the same disadvantages as those associated with call by value.
with CS_IO; use CS_IO; procedure IN_OUT_MODE_EXAMPLE is VALUE_1, VALUE_2: float; procedure MEAN_VALUE (NUM_1: in out float; NUM_2: in float) is begin NUM_1:= (NUM_1+NUM_2)/2.0; end MEAN_VALUE; begin get(VALUE_1);get(VALUE_2); put("BEFORE MEAN VALUE: VALUE_1 = "); put(VALUE_1);put(", VALUE_2 = ");put(VALUE_2);new_line; MEAN_VALUE(VALUE_1, VALUE_2); put("The mean is "); put(VALUE_1);new_line; put("AFTER MEAN VALUE: VALUE_1 = "); put(VALUE_1);put(", VALUE_2 = ");put(VALUE_2);new_line; end IN_OUT_MODE_EXAMPLE;
The above all assumes that we wish to pass data items. Some languages support passing of procedures (e.g. Pascal). Example: in numerical analysis, where methods for finding (say) roots of functions etc. can be written to be independent of any particular routine, it useful to be able to pass appropriate functions to solving routine.
Remember to switch of the hot water: return to imperative home page.
Created and maintained by Frans Coenen. Last updated 03 July 2001