Functions are the workhorse of the C language. They help perform many operations and to build programs up to something more usable than small programs with basic arithmetic or strings.
The construction of a function is as follows:
type function_name() { }
the type of a function refers to what is returned or generated by the function.
Some types are: char, int, float, double. Void is used if the function is not going to return a value.
One important thing to remember when using functions in C, all functions must be prototyped in the code. Otherwise the compiler will not understand the function and it will not be used properly. This is basically declaring the fucntion before the main class...I will show this in the following example(s).
The following is a simple (void) function contained within a loop.
The next program here will introduce parameters into the function. There just allow for some other variable or type to be introduced into the function from the outside; rather than instantiating it from inside the function.
The next example will help demonstrate how the return statements work in C for a function. For this example we will use the conversion between Fahrenheit to Celsius.
Those are all the different ways functions can be assembled and used in C. They are, of course, simple examples and functions can become more complex with more parameters of by calling more functions.
No comments:
Post a Comment