C++ Functions

Functions in programming are used to split up complex pieces of code and break them down into simple parts that complete a defined task. Using functions in programming is great for when you want to debug. It makes it easier to find where problems are being created and it is easier to step through a ten line function than a 400 line piece of code to find out where it is broken. Without functions large pieces of software could never be written. Theses pieces of software are created with hundreds of thousands of lines of code. It would be a debugging nightmare to have to fix all of the problems. Only when you can break down your code into simple, small independent parts that can work together and in a clearly defined way can you say that you know how to program. I think that functions are the most important thing that a programmer can learn to use.
The most simple type of function to write is called a user-defined function. A
user-defined function is the most simple to write because it written with the same format as main(). I doesn’t deal with parameters at all. It is a simple function that extends what main can do. To start out with a user-defined function you would first initialize what type of a function it is. In this case a user-defined function is always declared with a void statement. After void the name of the function is given with a start brace and an end brace. After the braces it is best to start a new line and write a comment of what the function is doing. After comments are given you will write the function definition. This function will not end with a return() statement because this function is void and does not return a value. The function

definition is basically the code in the function. The layout of a user defined function would look like this:

void FunctionName()

/* Describes what the function will do */

function definition

A sample user defined function that I made looks like this:

void Introduction()
/* prints who made the program when started */

cout << "This program was written by Brandon Bernien";

When making more advanced functions you are going to need a more powerful way using functions. The first step is to use function parameters. These parameters or arguments as some call it can be used to do something with variables in main(). You will be able to use function parameters by doing two things. First off you will put your parameters in the braces that come after the function name. An example would be: void FunctionName(int x, int y).
Now certain things must be done to correctly use a function parameter. As you can see I declared what type of variable the variable will be. I used int so that means that my variable will be a whole number -32,767 - 32,767. You can declare the function parameter to have any type of identifier supported by c++. The next thing that you must do is give the variable a name. I used x and y as my variables. Throughout the function to use the vales in the function parameter I must call the variable by the name identified in the actual parameter.

An example program that I created that uses function parameters is:

void AgeDescription(int Age)
/* Displays the appropriate message depending on Age’s value */

if (Age <= 25)
cout << "Wow a real youngster!n";
else if ((Age >= 26) && (Age <= 50)
cout << "Getting up there!n";
cout << "Have you made retirement plans??n";

This program simply illustrates how the function will take an integer from a user. It will then will display a message depending on the users input.
Function overloading is another important fundamental to learn to use in programming with functions. Function overloading may seem like a complex function but it is really very simple. To overload a function you will be making two or more functions with the same name. A good example to why this is done is if you want to make different calls to the function that alter only specific things in that function. Each function will be made with different arguments to make the overloading work. There is not much to explain about this topic except how to use it.