Hi.
Can anyone tell me some good websites that I can go to to find out more about functions in C++?
Thanks
I apologize. I was trying to find out what websites I can go to find out how to create user-defined functions, like void displayMessage() for example.
Thanks for the sites, though
So every functoin has 4 parts. The return type, the name, the parameters, and the body/definition
The first 3 make up the declaration / header / prototype. The last makes up the definition.
So lets examine each of these individually....
First you have the return type.
The return type in your first post "void" is a common on that basically means the function will be returning NOTHING. Functions that have a void return type, usually take in certain parameters and modify them in the function definition. Since it isn't returning something, the variables being taken in are usually members of something else in code that are just being modified in this void function.
Some common return types are
void
int
bool
char *
char
There are plenty other return types, including classes/structs/user defined variables/etc...
The next function part is the name. This is quite self explanatory... it's just the name of the function.
So from what you've learned so far, a return type and a name.. you can do:
void ChangeScreenResolution
// or
bool IsPlayerHealthBelowZero
Next, is the parameters. This is the last part which makes up the Function header/prototype. The parameters are basically just variables being sent into the function, and modified within the function. Parameters can take basically any type of variable that a function can return... For example:
void ChangeScreenResolution(int ScreenWidth, int ScreenHeight)
// or
bool IsPlayerHealthBelowZero(int playerHealth)
Last, is the function definition... This is the body of the function. This is where the magic happens. The function definition takes in the parameters (if any), and modifies them, then it returns something (if any are needed).
So, for example:
void ChangeScreenResolution(int screenWidth, int screenHeight)
{
currentScreenWidth = screenWidth;
currentScreenHeight = screenHeight;
}
// or
bool IsPlayerHealthBelowZero(int playerHealth)
{
if(playerHealth <= 0)
{
playerHealth = 0;
return true;
}
return false;
}
So in the first example above, the function takes in a new screen width and height, then sets the current screen width and height to the new ones that were passed in.
The second example, takes in the current player health, checks to see if it's zero (or below)... then sets it to 0 just incase it is below, and returns "true" if it is below or equal to 0. Otherwise, it returns false. I have to return a bool value since the function returns a bool. In this case, if the player health is <= 0, i return true and as soon as the compiler hits that return, it exits that function immediately with that return type. If the playerHealth is greater than 0, then that if statement fails, and it goes down to the next statement, which is a return false, which leaves the function immediately with that return type.
Hope this all helps. I'm sure theres better tutorials on the net :)
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.