A function can perform calculations and adjust variables within itself and not accept any input or return any value such as in the first example.
A function can receive information, perform calculations on this input but still not return any information as in the second example.
A function can receive information, perform calculations on this information and also send information back when it is used as in the third example.
//======================================================================================================================
float finalValue = 0.0; //Global variable, some compilers may complain that this is actually a double since the 'f' is omitted, i.e. 0.0f
//A double takes up twice as much memory space but also has more precision.
void calculateValue(void) //This function definition has two 'voids' because nothing goes in and nothing comes out, it modifies the variable directly
{
finalValue = 10.0 * 2.0;
}
void RenderLoop(void)
{
if(20.0 == finalValue) //Here, finalValue is still 0.0 as defined at the beginning, this conditional will fail, you will not be able to eat pizza.
{
eatPizza = true;
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
calculateValue(); //Now when this function is called in the render loop, finalValue is modified to have a value of 20.0
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if(20.0 == finalValue) //Here, finalValue is now 20.0, you will finally be able to eat pizza
{
eatPizza = true;
}
}
//======================================================================================================================
//----------------------------------------------------------------------_PART_02_---------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------_PART_02_---------------------------------------------------------------------------------------------------
//======================================================================================================================
float numberOfBottlesOfRootbeer = 0.0;
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//The following function has two inputs where the previous one had void, this means we will be giving the function information necessary for its calculations
void HowMuchRootbeerToBuy(float costOfRootBeer, float amountOfMoneyAvailable)
{
//if we have $20.00, and the Root Beer costs $2.00 per bottle then numberOfBottlesOfRootbeer will be set to 10.0 when it's called in the RenderLoop() function
numberOfBottlesOfRootbeer = amountOfMoneyAvailable / costOfRootBeer;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void RenderLoop(void)
{
//Before we call the function, the value of numberOfBottlesOfRootbeer is still 0.0.
HowMuchRootbeerToBuy(2.0, 20.0); //Here we both call the function, and input the values for its calculations
//Now that we've called the function HowMuchRootbeerToBuy, the value of numberOfBottlesOfRootbeer is 10.0.
}
//======================================================================================================================
//----------------------------------------------------------------------_PART_03_---------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------_PART_03_---------------------------------------------------------------------------------------------------
//======================================================================================================================
int starTrekEpisodeSelection = 0;
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
int SelectEpisodeToWatch(int lastEpisodeWatched) //This function is set to return an integer.
{
int incrementEpisodeWatched;
incrementEpisodeWatched = lastEpisodeWatched + 1;
return incrementEpisodeWatched;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void RenderLoop(void)
{
//before the function SelectEpisodeToWatch() is called, the value of starTrekEpisodeSelection is 0.0.
starTrekEpisodeSelection = SelectEpisodeToWatch(15); //here the return value of the called function is being assigned to starTrekEpisodeSelection
//now starTrekEpisodeSelection integer has a value of 16.
}