Functions
So far, we have written all of our code in the main function. This is fine if you are doing a single task, but once you start doing more with a program, the code will become bigger and over a period of time everything will be in the main function, which will look very confusing.
With functions, you can break your code up into smaller, manageable chunks. This will make you able to structure your program better.
A function has the following syntax:
type function name (parameter1, parameter2) {statements;}
Going from left to right, the type here is the return type. After performing a statement, a function is capable of returning a value. This value could be of any type, so we specify a type here. A function has only one variable at a time.
The function name is the name of the function itself.
Then, inside brackets, you will pass in parameters. These parameters are variables of a certain type that are passed into the function to perform a certain function.
Here as an example: two parameters are passed in but you can pass as many parameters you want. You can pass in more than one parameter per function and each parameter is separated by a comma.
Let's look at this example:
#include <iostream> #include <conio.h> // Program prints out values to screen void add(int a, int b) { int c = a + b; std::cout << "Sum of " << a << " and " << b << " is " << c <<
std::endl; } int main() { int x = 28; int y = 12; add(x, y); _getch(); return 0; }
Here, we create a new function called add. For now, make sure the functions are added before the main function, otherwise main will not know that the function exists.
The add function doesn't return anything so we use the void keyword at the start of the function. Not all functions have to return a value. Next, we name the function add and then pass in two parameters, which are a and b of type int.
In the function, we create a new variable called c of type int, add the values of the arguments passed in, and assign it to c. The new add function finally prints out the value of c.
Furthermore, in the main function, we create two variables called x and y of type int, call the add function, and pass in x and y as arguments.
When we call the function, we pass the value of x to a and the value of y to b, which is added and stored in c to get the following output:
When you create new functions, make sure they are written above the main function, otherwise it will not be able to see the functions and the compiler will throw errors.
Now let's write one more function. This time, we will make sure the function returns a value. Create a new function called multiply, as follows:
int multiply(int a, int b) { return a * b; }
In the main function, after we've called the add function, add the following lines:
add(x, y); int c = multiply(12, 32); std::cout << "Value returned by multiply function is: " << c <<
std::endl;
In the multiply function, we have a return type of int, so the function will expect a return value at the end of the function, which we return using the return keyword. The returned value is variable a multiplied by variable b.
In the main function, we create a new variable called c; call the multiply function and pass in 12 and 32. After being multiplied, the return value will be assigned to the value of c. After this, we print out the value of c in the main function.
The output of this is as follows:
We can have a function with the same name but we can pass in different variables or different numbers of them. This is called function overloading.
Create a new function called multiply, but this time, pass in floats and set the return value to a float as well:
float multiply(float a, float b) { return a * b; }
This is called function overloading, where the function name is the same but it takes different types of arguments.
In the main function, after we've printed the value of c, add the following code:
float d = multiply(8.352f, -12.365f); std::cout << "Value returned by multiply function is: " << d << std::endl;
So, what is this f after the float value? Well, f just converts the doubles to floats. If we don't add the f, then the value will be treated as a double by the compiler.
When you run the program, you'll get the value of d printed out: