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 ...

Get Beginning C++ Game Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.