Using Functions in C++

Using Functions in C++

This article is about using functions in C++. Functions reuse code to help solve problems. It is described as doing a small part that can be used elsewhere in your program. Today I want to show you how to do that.
 
 

Definition of a Function

When you write a function you can save it and use it in another part of your code. This is called "calling your function". There are methods designed to do this that are built into C++ and most other languages. A function is made of a name, parameters, body, and return type.
 
The name should make sense and describe what the purpose of this function is.
Parameters will usually be a variable that the function use.
The body of a function is what it does. It is the statements that do something.
The return type is referring to the data type of the value that the function created.
 

Calling Your Function

After you write a function that does some task, you have to call it when you want to use it. This will be in your [main()] function.
 
 
 
I created the function HelloWorld() and then called it from the main() function. Now you can see at the top of the program is the function definition of HelloWorld(). That is where I described what it is and how it is to be used.
 
You can also loop a function. So we start by defining a loop and then include the function to be called. You can have lots of fun doing this. Math and many other tasks can do some interesting tasks when looped.
 
 

Function Prototypes

When using a function the compiler has to know about it before any calls to one of these functions are made. First, we do this by typing in the whole function definition at the top of the program. However, you can also use a prototype and finish the rest later in the program, after the main function. The main difference in the header is that a protype will use a [;] after the header. That means its a prototype. It will look something like this.
 

I will say its a matter of personal opinion whether you use a prototype or not. At this point it doesn't make any difference. I prefer not to use prototypes myself but again it doesn't matter.

Parameters

A parameter is a variable that holds some type of value that will go into a function.
 
You can see in this new function that there is an argument inside the function parentheses.
 
Using expressions is more useful. Calculations, for example, can be done. Parameters make these types of functions very useful.
 

Return Statement

This statement ends a function immediately. This would be used when you design a function to only end after a certain condition.
There are also functions that can return values. Many mathematical functions are like this. Values are given to the function and it returns a value. This is handy and is why everyone likes calculators.
 

Returning True/False Values

A true/false value is often called a boolean value. This lets the designer of the function test to see if a certain condition exists. It is very helpful to do this.
 
 
 

 Conclusion

So these are functions. I have gone over how they are created and slightly how they are used. There are infinite combinations. It may not seem very useful but as you get more proficient in using functions in C++ you will want to use them. I just wanted to explain their basic usage so you will be prepared. Have a great day and if you enjoyed this article please share it on social media and subscribe to my mailing list for more articles like this.