Functions in C++

Function Parameters

Parameter Type Function Does Not Need to Modify Actual Parameter Function Needs to Modify Actual Parameter
Basic Data Types Pass by Value Reference or Pointer
Arrays const Pointer Pointer
Structures const Pointer or const Reference Reference or Pointer
Class Objects const Reference or const Pointer Reference or Pointer

The above are just guidelines; actual situations may have different choices. For example, for basic types, cin uses references, so you can use <span>cin>>n</span>, instead of <span>cin>>&n</span>.

Returning Objects from Functions

Returning Objects

When a function returns an object that is a local variable, it should not return by reference because after the function execution, the local variable will call the destructor, and the object pointed to by the reference will no longer exist. Typically, overloaded operators return objects.

In this case, there is an overhead of calling the copy constructor to create the returned object, but this is unavoidable.

Returning const Objects

This is not very meaningful.

Returning const References

Returning a reference to a const object: To improve efficiency, the function returns a reference to the passed object. For example, comparing two objects:

const Person&amp; Max(const Person&amp; p1, const Person&amp; p2)
{
  if (p1.age &gt; p2.age)
    return p1;
  else
    return p2;
}

Returning Non-const References

Two common scenarios:

  1. 1. Overloading the assignment operator, aimed at improving efficiency. Generally, objects are modifiable, so returning will not add const.
  2. 2. Overloading the <span><<</span> operator used with cout. In this case, it can only return a non-const reference <span>ostream &</span>; if the return type is <span>ostream</span>, it would require calling the copy constructor of the ostream class, which does not have a public copy constructor.

Function Overloading

In the same scope, the function names are the same, but the function parameters differ in type, number, or order. Overloaded functions can have different return types, but merely differing return types does not constitute overloading.

Function Templates

Function templates use generics to define functions, allowing the compiler to generate functions of that type by passing the type as a parameter to the template.

If you want to use the same algorithm for different parameter types, you should use function templates. For example, if there is a function to swap two values, with parameter types like integers, floats, etc., defining a function for each type would lead to redundant code.

// Implicit instantiation: regular function template
template &lt;typename T&gt; void Swap(T &amp;a, T &amp;b);

// Explicit instantiation: generates a function definition for double type based on the above function template.
// This is not very meaningful, as the compiler will generate a function definition for double type when passing double type parameters.
template void Swap(double &amp;a, double &amp;b);

template &lt;typename T&gt;
void Swap(T &amp;a, T &amp;b) {
    T temp;
    temp = a;
    a = b;
    b = temp;
}

However, not all types use the same algorithm; in this case, explicit specialization can be used—defining a function specifically for that type without using the function template to generate the function definition.

struct job {
char name[40];
double salary;
int floor;
};
// Explicit specialization
template &lt;&gt; void Swap&lt;job&gt;(job&amp; j1, job&amp; j2);  // &lt;job&gt; can be omitted

template &lt;&gt; void Swap(job&amp; j1, job&amp; j2) {
    double t1;
    int t2;
    t1 = j1.salary;
    j1.salary = j2.salary;
j2.salary = t1;
t2 = j1.floor;
j1.floor = j2.floor;
j2.floor = t2;
}

If the parameters of the function template vary, you can overload the function template.

// Overloaded function template
template &lt;typename T&gt; void Swap(T a[], T b[], int n);

template &lt;typename T&gt;
void Swap(T a[], T b[], int n) {
    T temp;
    for (int i = 0; i &lt; n; i++) {
        temp = a[i];
        a[i] = b[i];
        b[i] = temp;
    }
}

Overloading occurs due to different function parameters, while explicit specialization occurs due to different algorithms.

Leave a Comment