Functions
A function is a module or block of program code which deals with a particular task. Making functions is a way of isolating one block of code from other independent blocks of code. Functions serve two purposes. They allow a programmer to say: `this piece of code does a specific job which stands by itself and should not be mixed up with anyting else', and they make a block of code reusable since a function can be reused in many different contexts without repeating parts of the program text.
Functions help us to organize a program in a simple way; in Kernighan & Ritchie C they are always written in the following form:
identifier (parameter1,parameter2,..)
types of parameters
{ variable declarations
statements..
......
....
}
For example
Pythagoras(x,y,z)
double x,y,z;
{ double d;
d = sqrt(x*x+y*y+z*z);
printf("The distance to your point was %f\n",d);
}
Functions with values
In other languages and in mathematics a function is understood to be something which produces a value or a number. That is, the whole function is thought of as having a value. In C it is possible to choose whether or not a function will have a value. It is possible to make a function hand back a value to the place at which it was called. Take the following example:
bill = CalculateBill(data...);
The variable bill is assigned to a function CalculateBill() and data are some data which are passed to the function. This statement makes it look as though CalculateBill() is a number. When this statement is executed in a program, control will be passed to the function CalculateBill() and, when it is done, this function will then hand control back. The value of the function is assigned to "bill" and the program continues. Functions which work in this way are said to return a value.
In C, returning a value is a simple matter. Consider the function CalculateBill() from the statement above:
CalculateBill(starter,main,dessert) /* Adds up values */
int starter,main,dessert;
{ int total;
total = starter + main + dessert;
return (total);
}
As soon as the return statement is met CalculateBill() stops executing and assigns the value total to the function. If there were no return statement the program could not know which value it should associate with the name CalculateBill and so it would not be meaningful to speak of the function as having one value. Forgetting a return statement can ruin a program. For instance if CalculateBill had just been:
CalculateBill (starter,main,dessert) /* WRONG! */
int starter,main,dessert;
{ int total;
total = starter + main + dessert;
}
then the value bill would just be garbage (no predictable value), presuming that the compiler allowed this to be written at all. On the other hand if the first version were used (the one which did use the return(total) statement) and furthermore no assignment were made:
main ()
{
CalculateBill (1,2,3);
}
then the value of the function would just be discarded, quite legitimately. This is usually what is done with the input output functions printf() and scanf() which actually return values. So a function in C can return a value but it does not have to be used; on the other hand, a value which has not been returned cannot be used safely.
0 comments:
Post a Comment