O'Reilly logo

Beginning C++ Game Programming by John Horton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Getting started with functions

So what exactly are C++ functions? A function is a collection of variables, expressions, and control flow statements (loops and branches). In fact, any of the code we have learnt about in the book so far can be used in a function. The first part of a function that we write is called the signature. Here is an example function signature:

public void bombPlayer(int power, int direction) 

If you add an opening and closing pair of curly braces {…} with some code that the function actually performs then we have a complete function, a definition:

void shootLazers(int power, int direction) 
{ 
    // ZAPP! 
} 

We could then use our new function in another part of our code, as follows:

// Attack the player bombPlayer(50, 180) // Run ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required