Skip over navigation

Functions in C++


Function Overloading and Function Templates

How to Cite This SparkNote

Problem : What are the requirements for function overloading?

A function name can only be overloaded if one or more of the following is true: 1) the versions have different return types, 2) the versions have a different number of arguments, or 3) the versions have arguments of different data types.

Problem : How many function arguments can have default values?

As many as you want. Keep in mind that parameters with default arguments must be listed after those that do not.

Problem : What restrictions are involved when using default values?

The main restriction with default values is that in calling the function you cannot skip over parameters. If a function declaration looks like void f(int a=1, float b=2.7, char c='z');, then you cannot call f() by specifying only a and c. You must specify either none, a, a and b, or all three arguments.

Problem : Does every variable in a function template have to be of the same type?

No. Function templates simply give you the option of not specifying one or more of the data types used in a function.

Problem : Which data types will work with function templates?

A function template can accept any data type. Keep in mind, however, that depending on what your function does, it may not make sense to call some data types. For instance, if << is not defined for a class, then there will be an error in trying to print out an object.

Follow Us