C++ Tutorial #12 – More on Functions

Facebooktwittergoogle_plusredditpinterestlinkedinmail

In the last tutorial, we discussed about the syntax of a basic function, how to create one, and what they are used for. Today we will be continuing on functions. Before we get started, let me explain first what we’ll be doing. In a previous tutorial, we learned about jump statements and the different types of jump statements. We did that by writing separate programs using the different types. We will do the same today, but with different types and examples of functions.

 

First, we will discuss about declaring a function. Second, we will learn how to write a void function, also called a function with no type. Third, we will learn how to create a function with default values in the parameters. Fourth, we will learn how to create a function with arguments passed by reference. Like always, I will explain how these types of functions work. Lets get started!

 

DECLARING A FUNCTION

 

We don’t need to declare a functions when it is written at the top of our code (above the main function).  We do have to declare it when it’s written somewhere else for example, below the main function, or a different file. Today we will declare a function that is located below the main function. We will declare function in a separate file in a different tutorial. Lets begin coding!

 

 

The program should look like this:

 

The sum is: 17

 

As you can see, our addition function, is located at the bottom of our code.

 

 

On line 6, we declared our function. All you have to do is write the function type, name, parameters and end the line with a semicolon.

 

 

Some of you may be asking “Why do we have to declare a function when it’s anywhere but above our main function?”. The answer is because in c++, the computer reads the code from top to bottom. If the function is below the main function, the computer will read the main function first. It will see the function in the main code, and get confused, because the computer doesn’t know how to use the function. When you declare the function, the computer reads the declaration first, and then jumps to the function. After the computer understands how to use the function, it continues to the code that is written after the declaration.

 

NO TYPE FUNCTIONS or VOID FUNCTIONS

 

It is what it sounds, a No-Type Function is a function that has no type, meaning that your function does not need to return a value. Writing a void function is the same as writing a regular function, but instead of writing a type, we just place void. You can also place parameters in your no-type function. Here is an example of two void functions; one function with no parameters, and another one with two parameters.

 

 

The program should look like this:

 

This function prints stuff on the screen! =)

First Name: John
Last Name: Smith

 

Easier than your regular type function! You can write a regular function with a type, and have no parameters, although I don’t know why you would need to.

 

 

 

FUNCTIONS WITH DEFAULT VALUE

 

In your function, you can set a default value in your parameters. That way, when we use that function, and  don’t choose a value, the program will use the default value. Below, is an example of different ways we can use this same function printName.

 

 

The program should look like this:

 

First Name: John
Last Name: Smith
Age: 18

 

First Name: Ron
Last Name: Smith
Age: 18

 

First Name: Kyle
Last Name: NotSmith
Age: 50

 

If you look at line 13, you’ll notice that we didn’t place anything in the function. Our program chose the default value John, Smith and 18. In line 14, you’ll notice that we just placed Ron in parameter 1, and nothing else, the program chose the remaining default values.

 

 

FUNCTIONS WITH REFERENCE

 

This one is a little bit tricky. To use a function with a reference, we place an ampersand (&) after the type in our parameters. To use the function, we create a variable and set a value to it first. Then when we use the function, we place the variable type we created in the parameter instead of placing a value. This allows us to make changes to a variable with several functions.

 

 

The program should look like this:

 

4
12

 

As you can see, we have two functions, one multiplies the reference variable by two, and the other by three.

 

 

In our main function, we created a variable int x, and we set the value to 2. Our first function multiplied x by 2, making it equal to 4. Our second function multiplied x by 3, making equal to 12.

 

 

I’ll end this tutorial here, but now that you know more about functions, programming will be a whole lot funner. Congratulations! You passed the boring stuff in c++ programming! There’s still a lot to learn, but I’ll try to get more tutorials up soon.

 

Facebooktwittergoogle_plusredditpinterestlinkedinmail

About Justin

Computer programmer and owner of kauergames.com.
Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *