Functions and Parameters in C++
Airfare Daily Deals eCigarettes Eyeglasses Hotels Jewelry Online Backup Online Dating Online Printing Online Tickets Skin Care Textbook Rentals Vitamins Web Hosting Weddings
Find thousands of shopping-related forums
SEARCH

Functions and Parameters in C++

If you have any prior knowledge of C++, or practically any other programming language, you probably already know what a function is. In this article, we’re going to talk about basic functions, and how to create your own custom functions with or without parameters in the C++ programming language.

What are functions?

Basically, functions are blocks of code that you’ll want to use multiple times in your program, but you don’t want to write them over and over again, because that would be a waste of time and code lines. The most important function of your program is the main function. This is where your program starts running, so if you don’t have this function, or you renamed it, your program won’t work properly. So, the main function is like a “home” for your program. But from that “home”, you’re going to want to use other functions. So how do you do that?

Creating your own function

We’ll start with creating a void function (I’ll explain what that means later on in this article.). Go ahead and insert this code after the closing brace of the main function:

void printSomething () {

cout << “Hello, I am text on your screen” << endl;

}

Let’s go over this code. First of all, you type void. What this means is, “don’t return a value”. Functions can return values, like if you would have a function to calculate your weight, that function would return an integer value. Then, you would type for example:

int calculateWeight () {

to start your function. But now, we don’t want our function to return any values, so we use void. next, we type the name of the function, followed by brackets. Inside these brackets go parameters, but since we don’t have any in this function, these are empty. then, you insert an opening brace and you start with the body of the function. So, whenever you call this function, it will execute anything that’s in the body of that function.

Now, if you try to run this program, it will give you an error message. Why is this? It’s simple, the compiler runs the program from top to bottom, so it starts with the standard include iostream, then using std, then it starts with the main and it stumbles upon that function. Since it wasn’t mentioned before in the code, the compiler doesn’t know what you’re talking about. There are two ways to solve this problem. One is to just move the function above the main function, so it will recognize it. But, if you would like to keep your function up top, you can also prototype your function. To do this, just type “void printSomething();”. This basically tells the compiler: “You are going to see something called printSomething in your code. Don’t freak out, it’s just a function. Whenever you see it, go look for the function.”. So then, when the compiler reaches the line where you call printSomething, it will know that it has to go look for it, and it will find it under the main function. If you now run your program, it will compile without any errors.

Parameters

Sometimes, your function is going to need one or more values before it can start. For example, if you’re going to add up two values, you first have to pass them to the function. This is easy. When you create the function, just add one or more variables in between the brackets, like this:

int addNumbers (int number1, int number2) {

Now, you can use these variables in your body, and when you call the function, you just enter the two numbers, for example like this:

addNumbers (12, 64);

Need an answer?
Get insightful answers from community-recommended
experts
in Computer Programming & Languages on Knoji.
Would you recommend this author as an expert in Computer Programming & Languages?
You have 0 recommendations remaining to grant today.
Comments (0)
ARTICLE DETAILS
RELATED ARTICLES
ARTICLE KEYWORDS