Introduction
Functions are a useful way of referring to blocks of code, allowing the code to be called many times. Functions take some input information (the input parameters), do something with that information and then return a result (the return value).Predefined functions, such as sin() and sqrt()have already been met in earlier sessions. It is possible to define your own functions in C. Being able to do this allows powerful programs to be developed in a modular manner that makes the development and debugging stages significantly easier than would otherwise be the case.
[edit]
Defining a function
A function is defined as follows: type function_name(parameter_list)
{
// code block here
}
[edit]
type
Functions return a value. The type of this value (int, float, char etc) has to be specified. All of the standard C types met so far may be used as a function type. Sometimes a return value is not required, for example if the function prints an error message and then returns, in which case a return type of void should be used.
[edit]
function_name
This is a label that is used to refer to the function from other code blocks. The rules for valid names are the same as those for variables.
[edit]
parameter_list
Values are passed to a function via its parameter list. This a comma separated list of the values that the function takes. Each item in the list has a type specification. The parameter list can be blank, i.e. the function takes no parameters, in this case the keyword void should be used instead of a list (as is the case with "int main(void)". The names used for the parameters can be different from those used in the block of code that calls the function.
[edit]
code block
The code block contains the operations that constitute the function. The parameters declared in the parameter list can be used as standard variables within this block. Temporary variables may also be declared for use within the block. Unless the function has a return type of void then the function needs to return a value of the correct type. This is done via the return command (see below).
[edit]
A simple example
The code example below shows a simple example of defining and using a function.
#include <stdio.h>
#include <stdlib.h>
float product(float x, float y) // function definition - see note 2
{
float result; // a local variable - see note 3
result = x*y;
return result; // returning the value - see note 4
}
int main(void)
{
float a;
float b;
float answer;
// prompt user for values
printf("Please input two values to be multiplied:");
scanf("%f %f",&a,&b);
// call the function product
answer=product(a,b); // calling the function - see note 5
printf("The product of %f and %f is %f\n",a,b,answer);
system("pause");
return 0;
}
[edit]
Notes
- This example is rather too simple to be useful, but does illustrate how functions are defined and used.
- The function product is defined here. It has two input parameters and returns a value of type float.
- result is a variable declared within the product code block. It can only be used in this block.
- The function returns the value stored in result (which is of type float).
- The values of a and b are passed to the function product, the result is stored in the variable answer. Notice that different parameter names are used in the function and main block.
[edit]
Declaring Versus Defining Functions
In the example above the code for the function product appeared before the main block. This is fine for such a simple program, but in a realistic situation where there may be many tens if not hundreds of functions a program would rapidly become too difficult to follow. In such a situation it would be much better if the main() block comes first, followed by the functions. This is achieved using function prototypes. These are essentially the first line of a function definition terminated with a semicolon. For the function product() the prototype would be:float product (float x, float y);A function prototype tells the compiler about the function parameters and the return type - it is said to declare the function. The compiler uses this information to leave placeholders in the compiled code which is then filled in once the code that defines the code has been compiled. This definition code could come after the main function block or even be in a separate file. A function prototype must appear before the code block in which the function is used.
The example above can be rewritten as:
#include <stdio.h>
#include <stdlib.h>
float product(float x, float y); // see note 1.
int main(void)
{
float a;
float b;
float answer;
// prompt user for values
printf("Please input two values to be multiplied:");
scanf("%f %f",&a,&b);
// call the function product
answer=product(a,b);
printf("The product of %f and %f is %f\n",a,b,answer);
system("pause");
return 0;
}
float product(float x, float y) // see note 2
{
float result; // Note: this is a local variable
result = x*y;
return result; // returns the value to the function call
}
[edit]
Notes
- The function is declared here.
- The function is defined here.
- The function type and parameter list types must be the same in the declaration and definitions.
No comments:
Post a Comment