Loading

Functions

Return Values


Example 1

// Return the value of Pi
// Function Declaration
double Pi();

int main() {
// Function Call
    double dPi = Pi();
    return 0;
}

// Function Definition
double Pi() {
    return 3.14159;
}

Passing Arguments by Value


Example 1

// Convert Celsius to Fahrenheit
// Function Declaration
double CelsiusToFahrenheit(double);

int main() {
    double dCelsius = 28.3;
    // Function Call
    double dFahrenheit = CelsiusToFahrenheit(dCelsius);
    return 0;
}

// Function Definition
double CelsiusToFahrenheit(double dCelsius) {
    return (9.0/5.0)*dCelsius + 32.0;
}

Example 2

// Convert Fahrenheit to Celsius
// Function Declaration
double FahrenheitToCelsius (double);

int main() {
    double dFahrenheit = 84.6;
    // Function Call
    double dCelsius = FahrenheitToCelsius (dFahrenheit);
    return 0;
}

// Function Definition
double FahrenheitToCelsius (double dFahrenheit) {
    return (5.0/9.0)*(dFahrenheit - 32.0);
}

Passing Arguments by Reference


Example 1

// Swap the values of two passed in arguments
// Function Declaration
void Swap(double&, double&);

int main() {
    double dFahrenheit = 84.6;
    // Function Call
    double dCelsius = FahrenheitToCelsius(dFahrenheit);
    return 0;
}

// Function Definition
void Swap(double& drD1, double& drD2) {
    double dTemp    = drD1;
    drD1        = drD2;
    drD2        = dTemp;
}

Passing Constant Arguments by Reference


Example 1

// To avoid allocations, objects should be passed as
// constant references, rather than by value
// Function Declaration
int MyFunction(const MyClass&);

int main() {
    MyClass qMyObject = new MyClass;
    // Function Call
    int iValueSum = MyFunction(qMyObject);
    return 0;
}

// Function Definition
int MyFunction(const MyClass& kqrObject) {
    // kqrObject can not be changed in here.
    // These member functions are constant
    return kqrObject.GetValue1() + kqrObject.GetValue2();
}

Passing Array Arguments


Example 1

// Return the smallest element of a one-dimensional array
// Function Declaration
double GetSmallest(double[], const int);

int main() {
    double daArray[] = {3.5, 2.4, 6.2};
    // Function Call
    double dSmallest = GetSmallest(daArray, 3);
    return 0;
}

// Function Definition
double GetSmallest(double daArray[], const int kiArraySize) {
    double dSmallest = daArray[0];
    for (int iIndex = 1; iIndex < kiArraySize; ++iIndex) {
        if (dSmallest > daArray[iIndex]) {
            dSmallest = daArray[iIndex];
        }
    }
    return dSmallest;
}

Example 2

// Return the average of a two-dimensional array
// Function Declaration
double GetAverage(double[][3], const int, const int);

int main() {
    double daaArray[][3] = {{3.5, 2.4, 6.2}, {1.8, 7.3, 1.4}};
    // Function Call
    double dAverage = GetAverage(daaArray, 2, 3);
    return 0;
}

// Function Definition
double GetAverage(double daaArray[][3], const int kiDimSize1,
            const int kiDimSize2) {
    double dAverage = 0.0;
    for (int iIndex1 = 0; iIndex1 < kiDimSize1; ++iIndex1) {
        for (int iIndex2 = 0; iIndex2 < kiDimSize2; ++iIndex2) {
            dAverage += daaArray[iIndex1][iIndex2];
        }
    }
    dAverage /= (kiDimSize1*kiDimSize2);
    return dAverage;
}

Passing Pointer Arguments


Example 1

// Set the variable pointed to by the pointer
// Function Declaration
void SetValue(int*, int);

int main() {
    int iX = 0;
    int* ipIntPtr = &iX;
    // Set the value of iX to 3
    // Function Call
    SetValue(ipIntPtr, 3);
    return 0;
}

// Function Definition
void SetValue(int* ipPointer, int iInteger) {
    *ipPointer = iInteger;
}

Passing Pointer Reference Arguments


Example 1

// Change what a pointer points to—useful for data structures.
// Function Declaration
void SetPointer(bool*&, bool*);

int main() {
    bool* bpPtr1 = 0;
    bool bMyBool = true;
    bool* bpPtr2 = &bMyBool;
    // Set bpPtr1 to point to bMyBool
    // Function Call
    SetPointer(bpPtr1, bpPtr2);
    return 0;
}

// Function Definition
void SetPointer(bool*& bprRefToPtr, bool* bpPtr) {
    bprRefToPtr = bpPtr;
}

Default Arguments


Example 1

// Simple sum function to illustrate default arguments
// Function Declaration
int Sum(int iInt1 = 2, int iInt2 = 1);

int main() {
    // Function call with no arguments—returns 3
    int iValue3 = Sum();
    // Function call with one argument—returns 11
    int iValueEquals11 = Sum(10);
    // Function call with two arguments—returns 7
    int iValueEquals7 = Sum(3,4);
    return 0;
}

// Function Definition
int Sum(int iInt1, int iInt2){
    return iInt1 + iInt2;
}

Variable Arguments


Example 1

#include <iostream>
#include <cstdarg>

// Function Declaration
int Sum(int ...);

int main() {
    // Sum the three integers 5, 1, and 2
    // The first argument tells how many arguments are passed in
    // Function Call
    int iSum = Sum(3, 5, 1, 2);
    std::cout << "Sum = " << iSum << std::endl;
    return 0;
}

// Function Definition
int Sum(int iArgCount ...) {
    int iSum = 0;
    va_list qList;
    va_start(qList, iArgCount);
    for (int iIndex = 0; iIndex < iArgCount; ++iIndex) {
        int iCurr = va_arg(qList, int);
        iSum += iCurr;
    }
    va_end(qList);
    return iSum;
}

Example 2

#include <iostream>
#include <cstdarg>

void Output(char* cpVarTypes, ...)
{
    using namespace std;
    va_list qVarList;
    va_start(qVarList, cpVarTypes);
    
    for (int iIndex = 0; cpVarTypes[iIndex] != ''; ++iIndex) {
        switch (cpVarTypes[iIndex]) {
        case 'c':
        {
            char cChar = va_arg(qVarList, char);
            cout << cChar << endl;
            break;
        }
        case 's':
        {
            char* cpString = va_arg(qVarList, char*);
            cout << cpString << endl;
            break;
        }
        case 'd':
        {
            double dDouble = va_arg(qVarList, double);
            cout << dDouble << endl;
            break;
        }
        case 'f':
        {
            float fFloat = (float)va_arg(qVarList, double);
            cout << fFloat << endl;
            break;
        }
        case 'i':
        {
            int iInt = va_arg(qVarList, int);
            cout << iInt << endl;
            break;
        }
        default:
        {
            cout << "Error: Unknown Type" << endl;
            return;
        }
        }
    }
}

int main() {
    using namespace std;
    
    Output("sdifc", "XoaX.net", 3.14159, 101, 2.7f, 'X');
    
    return 0;
}

Recursive Functions


Example 1

// Fibonacci Numbers
// Function Declaration
unsigned int Fibonacci(unsigned);

int main() {
    // Function Call
    unsigned int uiFib10 = Fibonacci(10);
    return 0;
}

// Function Definition
unsigned int Fibonacci(unsigned int uiN) {
    if (uiN < 2) {
        return uiN;
    }
    return Fibonacci(uiN - 1) + Fibonacci(uiN - 2);
}

Inline Functions


Example 1

// Short functions, like Max() or Min(), which calculate the
// maximum or minimum are ideal candidates for inlining
// Function Definition
inline int Min(int, int);

int main() {
    // Function Call
    int iX = 10;
    int iY = 5;
    int iMinimum = Min(iX, iY);
    return 0;
}

// Function Definition
inline int Min (int iInt1, int iInt2) {
    if (iInt1 > iInt2) {
        return iInt2;
    } else {
        return iInt1;
    }
}

Function Overloading


Example 1

// A simple exmaple of function overloading. These two functions
// both find the smallest element in an array. The function that
// called in each case depends on the type of the array that is
// passed in as the first argument.
#include <iostream>

int FindSmallest(int* ipArray, unsigned int uiLength)
{
    int iSmallest = ipArray[0];
    for (unsigned int uiIndex = 1; uiIndex < uiLength; ++uiIndex) {
        if (iSmallest > ipArray[uiIndex]) {
            iSmallest = ipArray[uiIndex];
        }
    }
    return iSmallest;
}

double FindSmallest(double* dpArray, unsigned int uiLength)
{
    double dSmallest = dpArray[0];
    for (unsigned int uiIndex = 1; uiIndex < uiLength; ++uiIndex) {
        if (dSmallest > dpArray[uiIndex]) {
            dSmallest = dpArray[uiIndex];
        }
    }
    return dSmallest;
}

int main() {
    using namespace std;
    int iaArray[] = {7, -3, 4, 9, -1};
    cout << FindSmallest(iaArray, 5) << endl;
    double daArray[] = {8.7, 2.4, 7.4, 1.9, 3.0, 9.3};
    cout << FindSmallest(daArray, 6) << endl;
    return 0;
}