C++: Variables and Functions

If you haven't read the previous tutorial, we highly recommend you do so. Otherwise, you might miss important concepts used here.

Go ahead and fire up a text editor, and begin reading and typing. The meat of this tutorial is in the comments. Remember, try typing it out instead of copying and pasting. This way you will be able to remember by doing instead of by reading.


/* First off: let's make a basic function that prints "Hello" on the screen.
* The syntax for a basic function is as follows:
** returnType functionName()
* Like with main(), you can return an int if you want, but with normal
* functions it is possible to return nothing (void).
void sayHello(){
std::cout << "Hello from a function!" << std::endl;
/* Here is something new. std::endl (or just endl if you used the namespace
* mentioned in the previous lesson) inserts a new line into the console.
* Get into the habit of adding it to cout statements for readibility.
} /* Notice how using void as a return type means we don't have to return
* anything. For a quick example, let's make a function that does return
* something.

int threeTimesThree(){
int result;
/* Woah, we haven't seen this before. This is simply known as a variable.
* Basically, it stores data. But before you can store anything, you need
* to tell the compiler what type of data you want to store. This is
* specified here with "int". So, we just made an integer named "result".

result = 3 * 3;
/* To set a variable to something, just use the syntax
** variable = value;
* Value can be a single value, a calculation, or the result of another
* function. But, it has to be of the same type that you defined it as
* above. result should equal 9 right now.

return result;
/* As with main(), if we set a return type we need to actually return
* something. Here, we are returning the variable result.

/* What if you want to pass a variable to your function? That is when arguments
* come in handy. They aren't much more difficult to use than a simple
* variable:
int multiply(int number1, int number2){
/* You can have as many arguments as you want, and name them whatever you want.
* They can be used in your function as a simple variable.

int result = number1 * number2;
// This is an example of defining and setting a variable at the same time.

return result;

// Of course, we need our usual main() function.
int main(){
/* To call a function, use the same syntax you did for defining it, except
* without the type before it. If a function returns a value, you can
* assign it to a variable.

// Print out hello

int answer = threeTimesThree();
std::cout << answer << std::endl;
// You can use variables in a cout statement as well.

// If you like, you can even use a function directly.
std::cout << multiply(4, 5) << std::endl;

// Finally, finish up our main() function with a return.
return 0;

Compile it (if you forget how, see the previous tutorial) and run. You should get something like:

Hello from a function!
9 20

If not, or you got an error, check your code and see if you mistyped anything. If you are still having trouble, post a comment and I'll try to help you out.

We hope you enjoyed this tutorial. Stay tuned for more!

Avatar for jacob Jacob Peddicord - http://jacob.peddicord.net/

Jacob is a web developer, student, and programmer from Ohio. He is a staff member at the Ubuntu Forums and is most likely a fanboy of the distribution. He loves to write in code and words, play video games, and rant about topics most would have abandoned long ago. Jacob uses GNOME and is never seen running stable software, much to the demise of his laptop.

Home » Articles »

Discussion: C++: Variables and Functions

Home » Articles » C++: Variables and Functions