Parameters and Functions


C Programming Tutorial 

Go to the Table of Contents

Vist the Gifcom Corporation

Ways in and out of functions.

Not all functions will be as simple as the ones which have been given so far. Functions are most useful if they can be given information to work with and if they can reach variables and data which are defined outside of them. Examples of this have already been seen in a limited way. For instance the function CalculateBill accepted three values a,b and c.

CalculateBill (a,b,c)

int a,b,c;

{ int total;

total = a + b + c;
return total;
}

When variable values are handed to a function, by writing them inside a functions brackets like this, the function is said to accept parameters. In mathematics a parameter is a variable which controls the behaviour of something. In C it is a variable which carries some special information. In CalculateBill the "behaviour" is the addition process. In other words, the value of total depends upon the starting values of a,b and c.

Parameters are about communication between different functions in a program. They are like messengers which pass information to and from different places. They provide a way of getting information into a function, but they can also be used to hand information back. Parameters are usually split into two categories: value parameters and variable parameters. Value parameters are one-way communication carrying information into a function from somewhere outside. Variable parameters are two-way.