Introduction to Functions: C++ Crash Course (Free Tutorial)

Ready to learn some C++? Welcome to our introductory article on functions, the parts of code that carry out tasks. If you are a beginner and want to learn to code for FREE, check out our 30-minute beginners course here: training.mammothinteractive.com/p/learn-to-code-in-30-minutes

Functions consist of three main parts:

  1. return type: any type of variable that the function is returning, such as an integer or a string.
  2. function name: the name of the function. Unlike the name of an object, a function name is usually a verb.
  3. any parameters that the function takes in. A function usually modifies the parameters. For example, it might add or subtract one integer from another, or add one string onto another.

Let’s look at an example. If you want to follow along in real time, go on over to rextester.com. This site allows you to code in the main window, and test out your code instantly.

Let’s have a function return an integer and take in an integer a.

int return_int(int a){
return a;
}

Right now, our return type is int, function name is return_int, and parameter is integer a. This function is essentially taking in an integer and returning it.

Next we’ll call this function in main. Create an integer i and have it be the result of return_int. Tell it to take in a 5. In rextester.com, your main coding screen should look like this:

//g++ 4.9.3

#include <iostream>

int return_int(int a){
return a;
}
int main ()
{
int i = return_int(5);
std:cout << "Hello, world!\n";
}

In order to test the code, replace the "Hello, world!\n" with i. Scroll down to the bottom and press “Run It”. You should see a “5” appear.

Now why don’t we make a function that does something more useful? Let’s create a function called add_ints that takes in integers a and b and then returns the sum of the two, which will be another integer. Below the return_int function, add the following:

int add_ints(int a, int b){ 

Create another integer c, and have c be the sum of a and b:

int c = a + b;

As well, have the function return c so that it outputs the result.

return c; 
}

To test the code, create an integer j in the main that is the result of adding the integers 3 and 4.

int j = add_ints(3,4);

Replace std:cout << i; with std:cout << j; so that the compiler prints out j.

If all goes well, this function will add 3 and 4 to make 7, store 7 in the variable j, and print out the result j, which will be 7. If you run the code, you will see “7” appear on the screen.

 

Furthermore, let’s design a string function that takes in a couple of strings and combines them.

Call the function combine_strings, and have it combine strings a and b:

std:string combine_strings(std::string a, std::string b){

Like with the preceding integer function, create another string called combined, which will be a + b.

std::string combined = a + b;

Similar to adding integers, this is how you add strings. The function will take one string and attach a second one directly behind.

We want this function to return combined, which is the result of string a and string b being combined. In computer science, this is known as concatenation.

return combined;
}

Let’s see our string function in action. In the main, create a string called k that is the result of the function combine_strings. In parentheses, specify that string k that should be the result of the strings “mammoth” and “interactive”.

std::string k = combine_strings("mammoth", "interactive");

Replace std:cout << j; with std:cout << k; so that the compiler prints out k. You will see strings mammoth and interactive printed out in the correct order.

 

Lastly, let’s create a function that involves “if” statements. Have the function return an integer and call it greater. As well, have the function take in two integers a and b.

int greater(int a, int b){

Let’s have this function tell us which of these two integers is larger. To do so, create the following if statement: “If a is greater than b, return a.” This requires the following code:

if(a > b){
return a;

However, what if a is greater than or equal to b? To account for this, create the following clause:

} else {
return b;
}
}

This function takes two integers in its parameter and compares the two. If a is greater than b it returns a. If a isn’t greater than b, the function returns b.

To test this function, call the integer in our main function. Create another integer l and have it be the result of the greater of 10 and 8.

int l = greater(10,8)

Replace std:cout << k; with std:cout << l; so that the compiler prints out l.

The function will compare 10 and 8 and store the greater of the two in l. By running the code, you will get the output “10”.

Congratulations! You now know how to code functions in C++. To learn more, sign up for our FREE 30-minute introduction course here: training.mammothinteractive.com/p/learn-to-code-in-30-minutes.

Mammoth Interactive Favicon

Why you NEED to take this course :

Get in Touch.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

EMAIL US

support@mammothinteractive.com