Saturday, March 16, 2013


Hello everybody and welcome to my blog. I didn't post anything for quite a while because i was busy. I'm sorry. These few chapters require special attention if you want to learn C++ programming. Today i will cover functions. Functions are very important because they simplify our program. If you understand how function works you don’t need to repeat the program code. You just have to define and declare the function and after that call function in program when needed. So far you have been using function main. Every program has that function in order to work properly.

1.1.How function works?

We can describe the function as a self-containing block of code that has specific purpose. When you define certain function that function must have a name. If you recall the standard function “function_main” have the name main. Why you need the name of a function? The name of a function is needed because it identifies the function and this name is used later in the program code for calling the function when it is necessary. So as a beginner I strongly suggest that when you are creating a function please give this function a meaningful name.
Generally the name of the function is governed by the same rules as those for variables. A function is, therefore a sequence of letters and digits, the first of which is a letters. The underscore (_) counts as a letter.
Let’s say that you need a simple function that will count carrots. The simple idea when you create a function is to name that function “count_carrots ( ).”
After a name you need to define arguments. The arguments need to correspond with parameters that appear in the definition of the function. The arguments that you specify replace the parameters used in definition of the function when function executes. The code in the function then executes as though it were written using your argument values.

In this example, the function returns the sum of two arguments passed to it. If we take a closer look we can see that a function returns either a single value to the point in the program where it was called or nothing at all, depending on how the function is defined. The function can return a pointer that might contain the address of an array.

1.2.Why do we need functions?

The answer to this question is yes and the reason for that is when you define a function it can be executed as many times as necessary from different points in a program. Without a function you would have to copy/paste the program code which is sometimes time-consuming. When you’re using functions you break your program code into small pieces that are easily manageable chunks for development and testing. In general it is easier to debug your code when you’re focusing on small parts of program code then a whole program.
Let’s say that you are writing very big program and this programs contain 10000 lines of a code. Without functions, this program would be impossible to debug and test. When you implement functions it is much easier to test and correct it.

1.2.1.      Structure of a function

To understand the structure of a function let’s take a closer look at the function main ( ). This function consists of a function header that identifies the function. The function is then followed by the body of the function between curly braces containing the executable code for the function. In the next picture you can see what every function need in order to work properly.

Now let’s examine the next example. This is an example of a function that raise a value to a given power; that is, to compute the result of multiplying the value x by itself n times, which is x^n:
// Function to calculate x to the power n, with n greater than or
// equal to 0
double power(double x, int n)                                   // Function header
{                                                                                // Function body starts here...
double result(1.0);                                                     // Result stored here
for(int i = 1; i < = n; i++)
result *= x;
return result;
}                                                                                // ...and ends here

As I mentoned earlier the function has the function header and function body.

1.2.2.      Function header

In a previous example the function header looks like this:
double power(double x, int n)                                   // Function header
The function header consists of three parts. The first part of a function is the type of the return value in this case double. The second part is a name of the function (POWER) and the third part is the parameters of the function which are located inside of brackets. In this case the parameters are variables x and n, of types double and int.
The return value is returned to the calling function when the function is executed, so when the function is called, it results in a value of type double in the expression in which it appears. Our function has two parameters: x, the value to be raised to a given power, which is of type double, and the value of the power, n , which is of type int . The computation that the function performs is written using these parameter variables together with another variable, result, declared in the body of the function. The parameter names and any variables defined in the body of the function are local to the function.

1.2.3.      The general form of a Function Header

The general form of a function header can be written as follows:
Return_type function_name(parameter_list)
Ther return_type can be any legal type. If the function doesn’t return a value, the return type is specified by the keyword void. The keyword void is also used to indicate the absence of parameters, so a function that has no parameters and doesn’t return a value would have the following function header.
Void function_name(void)
If you type void in the parameters list that means that a function takes no arguments, so you could, so you could omit the keyword void between the parentheses like:
Void function_name( )

1.2.4.      Function body

After the function header you have to write the function body. Function body is formed inside the curly brackets after the function header. The desired computation in a function is performed by the statements in the function body following the function header.