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 the code in the function // I'm back again - code continues here after the function ends
When we use a function we say that we call it. At the point where we call bombPlayer
, our program's execution branches to the code contained within...