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.
Monday, March 21, 2016
Wednesday, March 9, 2016
Loops! Loops! Loops!
In this post we will discuss doing the various types of loops in the C language.
I want to start of with saying the these will look very much like loops in Java, so they should not be too difficult to understand.
The first loop we will go over is a "for" loop. It is put together in the following manner:
for (initialization; exit_condition; repeat){}
The main difference is that in C the initialization step, the variable must be initialized before the loop. In other words the variable is not a local variable for that loop alone, it's a global one for the entire class.
If we try and initialize the variable completely in the loop, the program will give us an error when we try to build it.
Another kind of loop is the "while" loop. While the for loop uses the increments to tell when the condition is reached, a "while" loop will just continue until the condition inside of it is reached.
This last example for loops will be doing a nested loop. For this I will use a nested "for" loop to create a variety of results. This is just a loop inside of another loop, and these can be done more than just once, and the more there are the more complex and confusing they become. These can be used for more complex mathematical functions, or running through multidimensional arrays.
I want to start of with saying the these will look very much like loops in Java, so they should not be too difficult to understand.
The first loop we will go over is a "for" loop. It is put together in the following manner:
for (initialization; exit_condition; repeat){}
The main difference is that in C the initialization step, the variable must be initialized before the loop. In other words the variable is not a local variable for that loop alone, it's a global one for the entire class.
If we try and initialize the variable completely in the loop, the program will give us an error when we try to build it.
Another kind of loop is the "while" loop. While the for loop uses the increments to tell when the condition is reached, a "while" loop will just continue until the condition inside of it is reached.
This last example for loops will be doing a nested loop. For this I will use a nested "for" loop to create a variety of results. This is just a loop inside of another loop, and these can be done more than just once, and the more there are the more complex and confusing they become. These can be used for more complex mathematical functions, or running through multidimensional arrays.
Subscribe to:
Posts (Atom)