Function

If you program many statements in main function, the source code will be difficult to understand and debug. On the other hand, it is often required to iterate same processing with different values of input. Function, which we study here, denotes a collective of processing. If we use a function, it will enables to iterate same processing with different values of input as well as make the source code look simple and easy to understand. If you can handle with functions, e.g. combining many functions, you can realize complex processing as you please.

Example of function

Take a look at an example of function. Example 1 defines and uses max_return, a function to return a maximum among five integers.

Example 1: function is defined before using

#include <stdio.h>

// Define of function
int max_return(int a, int b, int c, int d, int e)
{
  int max;

  max = a;

  if(max < b) max = b;
  if(max < c) max = c;
  if(max < d) max = d;
  if(max < e) max = e;

  return (max);

}

int main(void)
{
  int d0, d1, d2, d3, d4;
  int max;

  
  d0 = 2;  d1 = 3;  d2 = -1;  d3 = 8;  d4 = 5;
  // Recall function
  max = max_return(d0, d1, d2, d3, d4);
  printf("Maximum = %d\n", max);

  d0 = 3;  d1 = -2;  d2 = 5;  d3 = -6;  d4 = 0;
  // Recall function
  max = max_return(d0, d1, d2, d3, d4);
  printf("Maximum = %d\n", max);

  return (0);
}

In the above programming, a procedure is repeated twice to find a maximum among five integer variables, d0, d1, d2, d3, d4. After function max_return is recalled, processing is done in the function. And then, result is input into a variable, max.

If we do the same processing without function, it will be quite inefficient because we have to describe the same procedure twice. Example 1-2 is shown as such example.

Example 1-2: function is not used to achieve the same processing as Example 1

#include <stdio.h>

int main(void)
{
  int d0, d1, d2, d3, d4;
  int max;

  
  d0 = 2;  d1 = 3;  d2 = -1;  d3 = 8;  d4 = 5;
  // Processing without function
  max = d0;
  if(max < d1) max = d1;
  if(max < d2) max = d2;
  if(max < d3) max = d3;
  if(max < d4) max = d4;
  printf("Maximum = %d\n", max);

  d0 = 3;  d1 = -2;  d2 = 5;  d3 = -6;  d4 = 0;
  // Processing without function
  max = d0;
  if(max < d1) max = d1;
  if(max < d2) max = d2;
  if(max < d3) max = d3;
  if(max < d4) max = d4;
  printf("Maximum = %d\n", max);

  return (0);
}

Definition of function

Before we use a function, we have to define the function by ourselves. The definition is explained in the below from viewpoints of location and type.

Location of definition

We can define a function before it is used or after it is used. Example 1 defines a function before it is used, and Example 2 defines after used. In the latter case, we need prototype declaration.

In the case of prototype declaration, don't forget to end it with semicolon (;).

Example 2: function is defined after using

#include <stdio.h>

// Prototype declaration
int max_return(int a, int b, int c, int d, int e);

int main(void)
{
  int d0, d1, d2, d3, d4;
  int max;

  d0 = 2;  d1 = 3;  d2 = -1;  d3 = 8;  d4 = 5;
  // Recall function
  max = max_return(d0, d1, d2, d3, d4);
  printf("Maximum = %d\n", max);

  d0 = 3;  d1 = -2;  d2 = 5;  d3 = -6;  d4 = 0;
  // Recall function
  max = max_return(d0, d1, d2, d3, d4);
  printf("Maximum = %d\n", max);

  return (0);
}

// Define of function
int max_return(int a, int b, int c, int d, int e)
{
  int max;

  max = a;

  if(max < b) max = b;
  if(max < c) max = c;
  if(max < d) max = d;
  if(max < e) max = e;

  return (max);

}

Type of function

As variable has a type such as "int" or "double", function has the same type. For example, in "max_return" in Examples 1 and 2,

int max_return(int a, int b, int c, int d, int e)

interger type is defined.

Type of function is in accordance with type of variable which is returned by "return". In the Example 1,

return max;

This means the value of variable max is returned. As you see the type of variable max,

int max;

it has type of integer. Therefore, the type of "max_return" should be "int".

On the other hand, when a function has no return, the function has type of void in most of cases. An example of such function with void type is as follows.

/* 
    Calculation of BMI (Body Mass Index) and Standard weight
*/

#include <stdio.h>

void bmi_cal(double h, double w) {

  // BMI = weight(kg) / height(m) / height(m) 

  printf("height=%5.2f m, weight=%6.2f kg: BMI = %6.2f\n", h, w, w/h/h);  
}


void std_weight(double h) {

  // Standard weight (kg) = height(m) * height(m) * 22

  printf("height%5.2f m: Standard weight = %6.2f kg\n", h, h*h*22);  
}


int main(void) {

  double height, weight;

  printf("Input height(m) and weight(kg) ");
  scanf("%lf%lf", &height, &weight);

  // Calculation of BMI
  bmi_cal(height, weight);

  // Calculation of standard weight
  std_weight(***********);


  return (0);
}

Lessons for C programming
Junichi Susaki, Kyoto University