Examples of Calling Functions


/************************************************************************
*

The following statement calls the function startup and passes no parameters:

startup();

The following function call causes copies of a and b to be stored in a local area for the function sum. The function sum runs using the copies of a and b.

sum(a, b);

The following function call passes the value 2 and the value of the expression a + b to sum:

sum(2, a + b);

The following statement calls the function printf, which receives a character string and the return value of the function sum, which receives the values of a and b:

printf("sum = %d\n", sum(a,b));

The following program passes the value of count to the function increment. increment increases the value of the parameter x by 1.

                                                                        *
************************************************************************/

/**
 ** This example shows how a parameter is passed to a function
 **/

#include <stdio.h>

void increment(int);

int main(void)
{
  int count = 5;

  /* value of count is passed to the function */
  increment(count);
  printf("count = %d\n", count);

  return(0);
}

void increment(int x)
{
  ++x;
  printf("x = %d\n", x);
}

/************************************************************************
*

The output illustrates that the value of count in main remains unchanged:


x = 6
count = 5

In the following program, main passes the address of count to increment. The function increment was changed to handle the pointer . The parameter x is declared as a pointer. The contents to which x points are then incremented.

                                                                        *
************************************************************************/

/**
 ** This example shows how an address is passed to a function
 **/

#include <stdio.h>

int main(void)
{
  void increment(int *x);
  int count = 5;

  /* address of count is passed to the function */
  increment(&count);
  printf("count = %d\n", count);

  return(0);
}

void increment(int *x)
{
  ++*x;
  printf("*x = %d\n", *x);
}

/************************************************************************
*

The output shows that the variable count is increased:

*x = 6
count = 6

                                                                        *
************************************************************************/