Formal and Actual Parameter Example

Formal and Actual Parameter Example

Example: Main Program
#include <stdio.h>

/** prototypes go here **/

int square(int);         /* or int square(int value); */
void get_int(int *);     /* or void get_int(int *value); */
void double_it(int *);   /* or void double_it(int *value); */


int main(void)
{
  int num1, num2, num3; 
  
  num1 = square(2);
  num2 = square(num1);
  get_int(&num2);
  get_int(&num3);
  double_it(&num2);
  double_it(&num3);


  return(0);
}



/*===========================================================================
square: integer function
  parameter
    value: in only integer (a copy of the actual parameter is stored in the
                            formal parameter)
  return value
     integer that is the square of the formal parameter
  variable
     answer : integer
  description: squares the value of the formal parameter and returns the 
               answer using a function return statement

============================================================================*/
int square(int value)
{
   int answer;

   answer = value * value;
   return (answer);
}



/*===========================================================================
get_int: procedure (called a void function in C)
  parameter
    value: out only integer (the address of the actual parameter is used
                             stored in the formal parameter so that changes
                             can be made to the value stored at that location)
  return value
    none
  variable
    none
  description: a procedure which gets a single integer from the user and
              places that value in the formal parameter which is the storage 
              location associated with the actual parameter
============================================================================*/
void get_int(int *value)
{
  scanf("%d", value);
}


/*===========================================================================
double_it: procedure
  parameter
    value: in out integer (the address of the actual parameter is stored in
                           the formal parameter so that changes can be made
                           to the value stored at that location.)
  return value 
    none
  variable
    none
  description
     a procedure that doubles the value stored in the location associated 
     with the actual parameter and puts the result in the location associated 
     with the actual parameter
============================================================================*/
void double_it(int *value)
{
   *value = *value * 2;
}