Parameters
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.
Declaring Parameters
A function was defined by code which looks like this:
identifier (parameters...)
types of parameters
{
}
Parameters, like variables and functions, also have types which must be declared. For instance:
function1 (i,j,x,y)
int i,j;
float x,y;
{
}
or
char function2 (x,ch)
double x;
char ch;
{ char ch2 = '*';
return (ch2);
}
Notice that they are declared outside the block braces.
0 comments:
Post a Comment