2. Functions and Function Overloading
2. Functions and Function Overloading
Function
Overloading
MARIYA TAUQEER
Components of a function
Function using
loop
Functions with parameters
Passing data by value
The return
statement
Causes a
function to end
immediately
Static local variables
If a function is called
more than once in a
program the values
stored in the function’s
local variables don't
persist between
function calls.
Local variables are
destroyed when the
function terminates
and then recreated
when the function
starts again.
Static variables
are not
destroyed
when the
function
returns
Using reference variables as
parameters
Reference variable allows a function to access the parameter’s original argument. Changes to
the parameter are also made to the argument.
The ampersand must
appear in both the
prototype and the
header of any function
that uses a reference
variable as a parameter.
It does not appear in
the function call
Overloading functions
Two or more functions may have the same name as long as their parameter
lists are different.
Different number of arguments
Inline Functions
To save execution time in short functions, you may elect to put the code in the function body
directly inline with the code in the calling program. That is, each time there’s a function call in
the source file, the actual code from the function is inserted, instead of a jump to the function.
C++ inline function is powerful concept that is commonly used with classes. If a function is
inline, the compiler places a copy of the code of that function at each point where the function
is called at compile time.
#include <iostream>
using namespace std;
inline int Max(int x, int y)
{
return (x > y)? x : y;
}
// Main function for the program
int main() { Output:
Max (20,10): 20
cout << "Max (20,10): " << Max(20,10) Max (0,200): 200
<< endl; cout << "Max (0,200): " << Max (100,1010): 1010
Max(0,200) << endl; cout << "Max
(100,1010): " << Max(100,1010) <<
endl;
return 0;
}