Functions


Lessons

Credits for Lecturing and Authoring

Authored by: Lex Jacobson

Faculty Supervisor: Al Day

Some material derived from lessons authored by Prof. Martha Selby

HTML documentation by: Larry Genalo Jr.

Date Last Updated: 7/24/96


Functions

A C program can consist of several different functions, only one of which is main. Functions are separate program units (or modules).

The use of functions has some of the following advantages:

  1. A function can be referenced several times by the same program.
  2. Functions can be used by many different programs.
  3. Individual parts of a program become shorter and more easily understood with functions.
  4. Several programmers can work independently on different functions that will be used by one program.


Two types of functions will be discussed:

  1. Functions that return one value.

  2. Functions that return several values.

There can also be functions that don't return any values, which were discussed in the lesson on Intrinsic and Simple User-Defined Functions.


Functions that return a single value:

   General Form:
      return-value-type Function_Name(argument list)
      {
             	   declarations
                      .
              statements
                      .
              variable=(a C expression for value to be returned);
              return(variable);
      }

/*Program to calculate surface area and volume for a rectangular box*/

#include 

double box_area(double length, double width, double height){
  double area;
  area=2.0*(length*width + length*height+width*height);
}

double box_volume(double length, double width, double height){
  double volume;
  volume=length*width*height
  return volume;
}

int main(void) {
  double length, width, height, area, volume;
  FILE *inp;
  inp=fopen("IN.DAT","r");
  fscanf(inp, "%lf%lf%lf",&length,&width,&height);
  area=box_volume(length, width, height);
  volume=box_volume(length, width, height);
  printf("Volume=%f]include 

double box_area(double length, double width, double height){
  double area;
  area=2.0*(length*width + length*height+width*height);
}

double box_volume(double length, double width, double height){
  double volume;
  volume=length*width*height
  return volume;
}

int main(void) {
  double length, width, height, area, volume;
  FILE *inp;
  inp=fopen("IN.DAT","r");
  fscanf(inp, "%lf%lf%lf",&length,&width,&height);
  area=box_volume(length, width, height);
  volume=box_volume(length, width, height);
  printf("Volume=%f\n, Area=%f\n", volume, area);
  return 0;
}

Function Rules

  1. Each function is complete (like main) with its body contained in left and right braces.

  2. When a function returns a single value to the function that referenced it, a return statement is used with an expression for the value to be returned in parentheses.

  3. The function arguments in the reference statement must match in TYPE, NUMBER AND ORDER with the arguments used in the function referenced.

  4. Functions should be listed before the main function.


Functions that return several values:

General Form:

Void Function_Name (arguments received, *arguments returned) { declarations . statements . *variable_1=(a C expression for 1st value to be returned); *variable_2=(a C expression for 2nd value to be returned); }


Function Rules

  1. A function which returns more than one value has NO VALUE associated with it (It is not double or integer). It is of type void.

  2. The arguments in the statement which calls the function and in the function must match in TYPE, NUMBER AND ORDER (also, the argument list can be empty).

  3. Asterisks point to values to be returned to the main function. Ampersands point to values to be received from the function.