C++ Functions

Why we need them:

Functions are used to simplify repeated lines of code. For instance, instead of having to include lines to calculate the tangent of some angle, we can simply use the function tan(number). Functions also help break down a complicated program into smaller, fundamental building blocks and allows software to be developed by software teams. Functions can either return a single value of a defined data type, change the actual values of a set of data types, or both.

It is important to note that there is a one-to-one correspondence between the order in which variables are "sent" from the main when using the function, and the order in which they are "received" by the actual function definition. I highlight this point in the following examples of how programmers can define functions.

Return a single defined value:

A single value of a predefined data type can be returned by using the following format:

// headers
returnType functionName(argumentType1 variableName1, argumentType2 variableName2, ...); //define function here

int main()
{
//main program here, variables must be defined before being used in function!
returnType tempName=functionName(firstVariable, secondVariable, ...); //use function here
return 0;
}

returnType functionName(argumentType1 variableName1, argumentType2 variableName2, ...) //note NO semi-colon
{
returnType numberToBeReturned; // must declare variable type to be returned
// math occurs
return returnType;
}

**********************************

And an example which shows the entire code

#include <iostream>
#include <cmath>
using namespace std;

double functname(double var1, double var2, double var4, double var3);
/*note that I purposely put the var4 and var3 names "out of order." I am trying to reinforce the concept that name doesn't matter, what matters is the order the variables appear.*/

int main()
{
double var1 = 2.0;
double var2 = 3.0;
double var3 = 2.5;
double var4 = 3.5;
double resultr = functname(var2, var3, var1, var4);

cout << "The result of the function 'resultr' is: "
cout << resultr;
cout << endl;

return 0;
}
double functname(double var1, double var2, double var4, double var3)
{
double answerr = pow(var1, var2) + pow(var3, var4);
return answerr;
}

Change the values of some data:

// headers
void functionName(argumentType1 & variableName1, argumentType2 & variableName2, ...); //define function here - note the "&" symbol

int main()
{
//main program here, variables must be defined before being used in function!
functionName(firstVariable, secondVariable, ...); //use function here
return 0;
}

void functionName(argumentType1 & variableName1, argumentType2 & variableName2, ...) //note NO semi-colon
{
variableName = math or something; // must declare variable type to be returned
// math occurs
return // the return can be omitted
}

**********************************

And an example which shows the entire code

#include <iostream>
#include <cmath>
using namespace std;

void functname(double & var1, double var7, double var8);
/*note that I only put the ampersand in front of the first variable, I just want to change it, not all of the variables.*/

int main()
{
double var1 = 2.0;
double var2 = 3.0;
double var3 = 2.5;

cout << "The value of 'var2' starting out is: " << var2 << endl;

functname(var2, var3, var1);

cout << "The value of 'var2' is now: " << var2 << endl;

return 0;
}
void functname(double & var1, double var7, double var8)
{
var1 = pow(var7, var8);
return;
}