Você está na página 1de 4

PARAMETER PASSING

CONTENTS:
Parameters can be classified into three groups or modes. They can be
used to:
1. Pass information to a sub-program.
2. Return information from a sub-program.
3. Pass information to a sub-program where it is updated and then returned.

CALL BY VALUE
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.
C Example:

#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("After 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);
}

CALL BY REFERENCE
The disadvantage of call by 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 by 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.

CALL BY REFERENCE
In the C programming language the effect of call by 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 by reference value.

C Example:

#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 reference value is used in C to pass arrays. This has the


efficiency advantage that we do not have make a copy of the array; of
course when doing this we must appreciate that we are working with
the actual data item. An example where we pass a string to a function
is given below. Remember that, in C, when we use an array name on
its own it acts as a "pointer" to the start of the array!

#include <stdio.h>

void toUpper(char *);


/* ------ MAIN ------ */

void main(void) {
char myString[32];

printf("Input a string of no more than 32 lower case


characters\n");
scanf("%s",myString);

printf("Echo myString = %s\n",myString);

/* Convert to Upper Casae */

toUpper(myString);

/* Output result */

printf("Upper case = %s\n",myString);


}

/* ------ TO UPPER CASE ------ */

void toUpper(char *text) {


int index=0;

while (text[index] != '\0') {


text[index] = text[index]-32;
index++;
}
}

Você também pode gostar