By Mr. Sokunbi, M.A.
Computer Tech Dept, YabaTech
COM313
INTRODUCTION TO C++
By
Mr. Sokunbi, M.A.
By Mr. Sokunbi, M.A. Computer Tech Dept, YabaTech
Parameter Passing To Functions
Parameters are arguments passed to Functions.
They can be passed in the following three ways:
1. Passing by value
2. Passing by Reference
3. Passing by Constant reference
Passing by value
• When parameters are passed by value, the expression used in the
function call is evaluated first, then the resulting value is assigned to the
corresponding parameter in the function parameter list before the
function begins execution.
• For example, in the following function calls, the parameters are
evaluated first before the values are sent to the function: j=9; cube(j),
cube(2*j-3), cube(2*sqrt(j)-cube(3)). In each case, the expression within
the parenthesis are evaluated to a single value, then the value is passed
to the function. This type of argument is also called read-only method.
The value cannot be changed by the function.
By Mr. Sokunbi, M.A. Computer Tech Dept, YabaTech
Parameter Passing by value
Example: In our previous example, whenever function cube is
called by the main() method, the value of variable n is first
evaluated, after which the resulting value is passed to the function
and store in parameter x as shown below:
#include <iostream>
#include <stdio>
int cube(int x)
{
return x*x*x;
}
int main()
{
int n=1;
while (n != 0)
Cout<<“\n Enter an integer value: “;
{ cin >> n;
cout << "\tcube(" << n << ") = " << cube(n) << endl;
}
} //This program continues until you enter 0 as input
By Mr. Sokunbi, M.A. Computer Tech Dept, YabaTech
Parameter Passing By Reference
• Passing by Reference
• There are some situations where a function needs to change
the value of the parameter passed to it or when the parameter
will consume too much memory if passed by value. That can be
done by simply passing the memory address of the parameter.
This passing by reference.
• To pass a parameter by reference instead of by value, simply
append an ampersand &, to the type specifier in the functions
parameter list. This makes the local variable a reference to the
argument passed to it. So the argument is read-write instead of
read-only. Then any change to the local variable inside the
function will cause the same change to the argument that was
passed to it.
• Note that parameters that are passed by value are called value
parameters, and parameters that are passed by reference are called
reference parameters.
By Mr. Sokunbi, M.A. Computer Tech Dept, YabaTech
Parameter Passing By Reference
Passing by reference example: The swap() Function
• This little function is widely used in sorting data:
void swap(float& x, float& y)
{ // exchanges the values of x and y:
float temp = x;
x = y;
y = temp;
} // This has replaced the original values of a and b back in the main method
int main()
{ // tests the swap() function:
float a = 40.54, b = 28.5;
cout << "a = " << a << ", b = " << b << endl;
swap(a,b);
cout << "a = " << a << ", b = " << b << endl;
}
By Mr. Sokunbi, M.A. Computer Tech Dept, YabaTech
Parameter Passing By Constant Reference
Passing by Constant reference
• If you do not want the function to change its contents, then passing by reference can
be risky.
• Fortunately, C++ provides a third alternative: passing by constant reference.
• It works the same way as passing by reference, except that the function is prevented
from changing the value of the parameter.
• The effect is that the function has access to the argument by means of its formal
parameter alias, but the value of that formal parameter may not be changed during the
execution of the function.
• This example illustrates the three ways to pass a parameter to a function:
void f(int x, int& y, const int& z)
{ x += z;
y += z;
cout << "x = " << x << ", y = " << y << ", z = " << z << endl;
}
• The first parameter a is passed by value, the second parameter b is passed by
reference, and the third parameter c is passed by constant reference:
By Mr. Sokunbi, M.A. Computer Tech Dept, YabaTech
Inline Function
A function call involves great overhead.
Extra time and space have to be used to invoke the
function, pass parameters to it, allocate storage for its
local variables, store the current variables and the
location of execution in the main program, etc.
In some cases, it is better to avoid all this by specifying
the function to be inline.
This tells the compiler to replace each call to the
function with explicit code for the function.
To the programmer, an inline function appears the
same as an ordinary function, except for the use of the
inline specifier.
By Mr. Sokunbi, M.A. Computer Tech Dept, YabaTech
Inline Function
Inlining the Cube Function
We now make the cube() function an inline function
by introducing the keyword inline:
inline int cube(int x)
{
return x*x*x;
}
The only difference is that the inline keyword has
been added as a prefix to the function’s head.
This tells the compiler to replace the expression
cube(n) in the main program with the actual code
(n)*(n)*(n).
By Mr. Sokunbi, M.A. Computer Tech Dept, YabaTech
Inline Function
• When the compiler replaces the inline function
call with the function’s actual code, we say that it
expands the inline function.
• Warning: Use of inlined function can cause
negative side effects. For example, inlining a 20-
line function that is called in 26 different locations
would add at least 500 lines of unnoticed source
code to your program.
• Inlined functions can also limit the portability of
your code across platforms.
By Mr. Sokunbi, M.A. Computer Tech Dept, YabaTech
Overloading Functions
C++ allows you to use the same name for different
functions.
This is called function overloading
But the functions must have different parameter type
lists;
The compiler will regard them as different functions.
To be distinguished, the parameter lists must either
contain a different number of parameters, or there
must be at least one position in their parameter lists
where the types are different.
Consider the following example
By Mr. Sokunbi, M.A. Computer Tech Dept, YabaTech
Overloading Functions
Example: overload function sum to compute the sum of two integers, three double values, and
four integers
sum(int x1,int x2)
{
long sum1 =0;
sum1 = x1 + x2;
cout<<"\n\t THE SUM = " <<sum1<<endl;
getchar();
}
void sum(double x1,double x2,double x3)
{
long double sum2 =0;
sum2 = x1 + x2 + x3;
cout<<"\n\t THE SUM = " <<sum2<<endl;
getchar();
}
Class Work:
void sum(int x1,int x2,int x3,int x4) Write a C++ function and test driver that
{ receives an integer from the keyboard,
long sum3 =0; the computes the factorial of the number.
sum3 = x1 + x2 + x3 + x4;
Remember 6! = 6x5x4x3x2x1 (6x5!)
cout<<"\n\t THE SUM = " <<sum3<<endl;
getchar();
}
By Mr. Sokunbi, M.A. Computer Tech Dept, YabaTech
Sample program
• The following program classifies the 128 ASCII characters (see Appendix A):
• #include <cctype> // defines the functions isdigit(), islower(), etc.
• #include <iostream> // defines the cout object
• using namespace std;
• void printCharCategory(char c);
• // prints the category to which the given character belongs;
• int main()
• { // tests the printCharCategory() function:
• for (int c=0; c < 128; c++)
• printCharCategory(c);
• }
• void printCharCategory(char c)
• { // prints the category to which the given character belongs:
• cout << "The character [" << c << "] is a ";
• if (isdigit(c)) cout << "digit.\n";
• else if (islower(c)) cout << "lower-case letter.\n";
• else if (isupper(c)) cout << "capital letter.\n";
• else if (isspace(c)) cout << "white space character.\n";
• else if (iscntrl(c)) cout << "control character.\n";
• else if (ispunct(c)) cout << "punctuation mark.\n";
• else cout << "Error.\n";
• }
By Mr. Sokunbi, M.A. Computer Tech Dept, YabaTech
PRACTICAL
• Debug and test run the sample programs. Add “using
namespace std;” to any of the program where the
compiler complains about cout
• You can know how to program only by writing programs
and running them – Practice makes perfect.
• Be sincere to yourself!
For your practical, you can download cxxdroid app
from play store into your mobile phone
or Borland C++ compiler into your laptop
Check the Assignment on Google classroom