- C++ First Program
- C++ Data Types
- C++ Conditions
- C++ Booleans
- C++ Switch Statements
- C++ Loops
- C++ Functions
- C++ Pointers
- C++ Arrays
- C++ Data Files

Functions are very handy tools in programming, they allow reusable code and the ability to build larger programs. Think about a program as a car. A car is not just one piece of hardware but pieces of hardware all put together to work with eachother. Each piece of hardware has it's own duties to be executed. Functions are like the pieces of hardware in a car. Functions can be written seperately to do certain tasks then put together to make a single more sophisticated program. Let us begin here is the syntax for a function.

return type name (data typevariable,data typevariable....)

So what do these mean?

**return type**-this what data type will return after the function is done so something like int,double, or even void

**name**-this is the name of your function choose whatever name you'd like, it will be the name you use to call it like your puppy! Ok maybe not

**data type** *variable*-this will be something like 'int x' these are the arguments that the function will take in

So a certain function that will take in two arguments x and y that are both integers and return a double that is named mult will look like this

double mult (int x, int y)

This is what's called a prototype, think of it like a pre processor directive in order to use the function (which you are about to write) the prototype must be declared. It's good practice to put the prototype before main like this.

#include <iostream> double (int x, int y); //notice the semicolon int main() { //main program return 0; }

Notice the semi colon, when prototyping a function before main you must put a semi colon after the prototype. So up until now that actual functions hasn't been written yet or in other words defined. The definition of the function is written completely outside of main like this.

#include <iostream> double mult (int x, int y); //notice the semicolon int main() { //main program return 0; } double mult (int x, int y) //notice no semicolon { //function will be written here }

When beginning to define the function notice how it is exactly the same as the prototype except the semicolon. The statement that go in betweem the curly brackets are the things that will be executed. So now let us write a full working program, this program will use a function to multiply two variables and return the sum, here we go!

//Full working program #include <iostream> double mult (int x, int y); //our functions prototype int main() { int a,b; //declare two variable a and b double c; //declare a variable 'c' that can hold decimal values cin>>a; //assign 'a' a value cin>>b; //assign 'b' a value c = mult(a,b); //call the function mult and send it the values of 'a' and 'b' cout<<"The function returned "<<c<<endl; return 0; } double mult (int x, int y) //defining the function { double z; //delcare a local variable that can hold decimal numbers //'x' holds the value of 'a' and 'y' holds the value of 'b' z = x*y; //z holds the value of 'x' and 'y' multiplied return z; //returns the value of 'z' to the variable 'c' in main }

The functions will take two values in this case 'a' and 'b' which will be held in 'x' and 'y'. Notice that 'x' and 'y' were not declared inside the curly brackets of mult this is because they were declared in the prototype double mult (int x, int y) and because both 'x' and 'y' are integers then the variables that are passed to it in this case 'a' and 'b' must also be integers. Also notice the variable 'z' declared inside the function mult, 'z' is what's called a local variable inside the function, it can only be used inside that function. If a programmer were to declare 'z' again inside the main function they would act as two different variables. This practice is frowned upon because it's very easy to mix the variables up.

Now you may be asking yourself, couldn't we of just put the same program into main and not have to write a function at all? Of course we could have but like many times the simplicity of the lesson helps in understanding but sometimes makes the actual lesson feel obsolete. Much more sophisticated programs are of course much larger and it would be tedious to have one huge main program. If something were to go wrong with a piece of the code then you would have a lot of searching ahead of you, but if the program was made up of a collection of functions then a programmer would have a much easier time finding the function that was giving trouble. Also if a program was all in main then it would be hard for multiple programmers to work on the same program but functions allow each programmer to work on a specific task of the program (a function) and then put them together when they are done making a single program.

Ok so let us take a look at a certain program that uses a function to calcualte the factorial of a natural number from 0 to 12. A factorial of a number is the series of multiplication less than that number. For example if we take the factorial of 4 it would be 4*3*2*1 which equals 24, so the factorial of 4 is 24. Here is the program take a look.

//Full working program #include <iostream> #include//needed to use system("pause") using namespace std; void fact (int x); //notice void, this means the function will not return a value int main() { int f; cout<<"Please input a number to be evaluated " "WARNING this program can't compute a value greater than 12"<<endl; cin>>f; cout<<"The factorial of "<<f<<" is "; fact(f); //sends the value of 'f' to the function fact return 0; } void fact (int x) //definition { //'x' now holds the value of 'f' int fact=1; //local variable fact must be assigned 1 to avoid garbabe if (x==0) //by definition the factorial of 0 is 1 { cout<<"1"<<endl; system("pause"); //pause program to see output return; //returns control to main } else if (x>12) //if 'x' is greater than 12 return control to main { return; //notice no value or variable just return, this is because of void } while (x>=1)//while 'x' is greater than or equal to 1 continue loop { fact=fact*x; //fact equals what is was times 'x' x--; //after every loop decrease 'x' by 1 } cout<<fact; //print fact and return control to main return; }

Notice how majority of the program takes place inside the function and all main really does is call the function fact. Now notice the void in front of the function name.

voidfact (int x)

The void function means that no value will be returned unlike the last function where the value of z was returned nothing is actually returned from the function hence the void. In these type of functions it's ok to simply put return when you want control to be passed back to main.

In the fact function there are three different scenarios. if 'x' equals 0 the program simply prints out 1 and returns control to main, there is nothing more to execute. The next scenario is if x is greater than 12, in the beginning of the program it warned the user that it will not calculate anything higher than 12. If the user enter a value more than 12 than the function will immediately pass control back to main. The program can generate a factorial higher than 12 but once you get to higher numbers like 100 the program can't compute a number that high and it will just print out 0. For accuracy we will keep it at 12. The last scenario is if 'x' is between 0 and 13 throws the program into a loop.

while (x>=1)//while 'x' is greater than or equal to 1 continue loop { fact=fact*x; //fact equals what is was times 'x' x--; //after every loop decrease 'x' by 1 }

The equation fact=fact*x can also can be written fact*=x but for simplicity I will continue doing it the other way. The equation is simple but lets walk through it in regards to the loop. Say if the user entered 4 factorial to be evaluated, remember fact was first assigned 1 so fact=1*4 which equals 4 so fact now equals 4 and 'x' is decremented by 1 so now it equals 3. So the new equation becomes fact=4*3 which equals 12 so now fact equals 12 and 'x' is now 2. The equation now becomes fact=12*2 which equal 24 and 'x' now equals 1 so the equation becomes fact=24*1 which is still 24 and 'x' is now 0 which doesn't pass the while loop test so the program breaks out of the loop, prints out fact, and returns control to main. So there you have it, happy coding!!