CITS2002 Systems Programming  
prev
next CITS2002 CITS2002 schedule  

Function Overloading

In C11, if we wish to perform very similar operations, but on different datatypes, we need to define multiple functions, with different (but similar) names.

In contrast, C++ supports function overloading which permits functions with the same name to support different parameter types and different return types.
This is often termed "a function with multiple type-signatures"

#include <iostream>

void outputWithNewline(int x)
{
    cout << "Integer value: " << x << endl;
}

void outputWithNewline(char *x)
{
    cout << "String value: " << x << endl;
}

The C++ compiler recognises that the multiple functions are strongly related, and calls the correct function (generates the correct instructions) depending on how the functions are called (i.e. with what parameter types).

 


CITS2002 Systems Programming, Lecture 23, p3, 17th October 2023.