View on GitHub

Notes

reference notes

Functions in User-defined Header Files

WORKING WITH FUNCTIONS IN PROGRAMMER-DEFINED HEADER FILES

Writing styles:

Creating a header file

  1. Create a folder
  2. Create a header file with the extension .h
  3. Create a cpp file
  4. Include the header file in the cpp file using the #include "header.h" directive
  5. Write the function definition in the header file
  6. Write the function call in the cpp file

Advanced Function Calls

References and reference parameters

Call by value or Pass by value

void swap(int x, int y) { int temp; temp = x; /* save the value of x / x = y; / put y into x / y = temp; / put temp into y */

return; }

int main () { int a = 100; int b = 200;

cout << "Before swap, value of a :" << a << endl;
cout << "Before swap, value of b :" << b << endl;

/* calling a function to swap the values */
swap(a, b);

cout << "After swap, value of a :" << a << endl;
cout << "After swap, value of b :" << b << endl;

return 0; } ``` - Output: ```bash Before swap, value of a :100 Before swap, value of b :200 After swap, value of a :100 After swap, value of b :200 ``` No change in the values of a and b, but the values of x and y are swapped inside the function scope.

Call by reference or Pass by reference

void swap(int &x, int &y) { int temp; temp = x; /* save the value at address x / x = y; / put y into x / y = temp; / put temp into y */

return; }

int main () { int a = 100; int b = 200;

cout << "Before swap, value of a :" << a << endl;
cout << "Before swap, value of b :" << b << endl;

/* calling a function to swap the values using variable reference.*/
swap(a, b);

cout << "After swap, value of a :" << a << endl;
cout << "After swap, value of b :" << b << endl;

return 0; } ``` - Output: ```bash Before swap, value of a :100 Before swap, value of b :200 After swap, value of a :200 After swap, value of b :100 ``` The values of a and b are swapped. x and y are aliases of a and b respectively, so INSIDE the function a becomes x and b becomes y.

Call by address or Pass by pointer

void swap(int x, int *y) { int temp; temp = *x; / save the value at address x / *x = *y; / put y into x / *y = temp; / put temp into y */

return; }

int main () { int a = 100; int b = 200;

cout << "Before swap, value of a :" << a << endl;
cout << "Before swap, value of b :" << b << endl;

/* calling a function to swap the values using variable reference.*/
swap(&a, &b);

cout << "After swap, value of a :" << a << endl;
cout << "After swap, value of b :" << b << endl;

return 0; } ``` - Output: ```bash Before swap, value of a :100 Before swap, value of b :200 After swap, value of a :200 After swap, value of b :100 ``` Pointers store the address of normal variables. in this case, *x stores the address of a and *y stores the address of b.

Default arguments and empty perameter lists

If a function with default arguments is called without passing arguments, then the default parameters are used.

#include <iostream>
using namespace std;

int sum(int a = 20, int b = 30) {
    int result;
    result = a + b;

    return result;
}

int main () {
    // local variable declaration:
    int a = 100;
    int b = 200;
    int result;

    // calling a function to add the values.
    result = sum(a, b);
    cout << "Total value is :" << result << endl;

    // calling a function again as follows.
    result = sum(a);
    cout << "Total value is :" << result << endl;

    // calling a function again as follows.
    result = sum();
    cout << "Total value is :" << result << endl;

    return 0;
}

Another way, with prototype:

#include <iostream>
using namespace std;

void display(char = '*', int = 3);

int main () {
    int count = 5;

    cout << "no arguments passed:" << endl;
    // * , 3 will be used as default arguments
    display();

    cout << "one argument passed:" << endl;
    // # , 3 will be used
    display('#');

    cout << "two arguments passed:" << endl;
    // # , 5 will be used
    display('#', count);

    return 0;
}

void display(char c, int count) {
    for (int i = 0; i < count; i++) {
        cout << c;
    }
    cout << endl;
}

Things to remember:

Advanced Function Capabilities

Inline Functions

Normal function Inline function
When a function is called, control is transferred to the called function. causes an additional overheadleading to inefficiency When a function is called, the code of the function is copied at the point of call. so there is no control transfer.
The function call is a normal function call. The function call uses the keyword inline to make the function inline.

Pros and cons of inlining

Pros Cons
No function call overhead – obtain enhanced program SPEED. Consumes additional registers.
Save overheadof return call from a function. If used too many -the size of the binary executable file will be large.
Save overheadof variables push/pop on the stack. If used too many -reduce your instruction cache hit rate.
Increases the locality of reference - utilisation of the instruction cache. Might cause thrashing – decrease computer performance.
Less code -useful for the small embedded systems. Increase compile time overheadif someone changes the code.
#include <iostream>
using namespace std;

inline int Max(int x, int y) {
    return (x > y) ? x : y;
}

int main () {
    cout << "Max (20,10): " << Max(20,10) << endl;
    cout << "Max (0,200): " << Max(0,200) << endl;
    cout << "Max (100,1010): " << Max(100,1010) << endl;

    return 0;
}

Function Overloading

An overloaded function must have:

// function declaration int func(int); double func(double);

int main () { int i = 5; double d = 11.7;

cout << func(i) << endl; // calls int version
cout << func(d) << endl; // calls double version

return 0; }

// function definition int func(int x) { return (x * 2); }

double func(double x) { return (x * 3); }

- Output:
```bash
10
35.1

Overloading is called FUNCTION POLYMORPHISM in OOP

Things to remember:

Caution: 1) Do not overload unrelated tasks. 2) Sometime default arguments maybe used instead of overloading -reduce function definitions. 3) Overloading are extensively used for handling class OBJECTS. 4) Overloading AMBIGUITY-if the compiler can not choose a function amongst two or more overloaded functions.

Function Templates

Example, with one data type:

#include <iostream>
using namespace std;

// function template
template <typename T>
T add(T num1, T num2) {
    return num1 + num2;
}

int main () {
    cout << add<int>(10, 20) << endl;
    cout << add<double>(10.5, 20.5) << endl;

    return 0;
}

Example, with two data types:

#include <iostream>
using namespace std;

// function template
template <typename T, typename U>
U add(T num1, U num2) {
    return num1 + num2;
}

int main () {
    cout << add<int, int>(10, 20) << endl;
    cout << add<int, double>(10, 20.5) << endl;
    cout << add<double, double>(10.5, 20.5) << endl;

    return 0;
}