CSC 202 Session 5
CSC 202 Session 5
1|Page
• However, C++ allows you to overload functions, that is assign the same name to
different functions. E.g.:
• int max ( int x, int y);
• double max (double x, double y);
• The compiler uses a function’s signature to differentiate between overloaded functions.
• A function signature calculates the number and type of parameters used in a function.
• When a function is called, the compiler compares the arguments to the signature of the
overloaded functions and simply calls the appropriate function. E.g.:
• void sum();
• void sum(int x);
• void sum(double x);
• void sum(int x, double y);
• void sum(double x, int y);
• In overloaded functions, the compiler is not interested in the name of the function but
the number, order and type of parameters.
• If overloaded functions were not allowed in C++, new function names must be created
for different functions that perform basically the same task but accept different
parameter types.
• Overloading functions advocates that it is better for the programmer to choose the
same name for similar functions and let the compiler properly resolve the differences.
3. DEFAULT PARAMETERS
• C++ permits you to assign default values to the formal parameters in your function
definition.
• Also, you are allowed to mix non-default and default parameters in the parameter lists
of a function declaration, but all default parameters within the parameter list must
appear after all the non-default parameters
2|Page
int sum_range (int n, int m=100) {// OK, default follows non-default
int sum = 0;
for (int val = n; val <= m; val++)
sum += val;
return val; }
int sum_range (int n=0, int m=100) {// OK, both default
int sum = 0;
for (int val = n; val <= m; val++)
sum += val;
return val; }
int sum_range (int =0, int =0) { // ok, both are default
int sum = 0;
for (int val = n; val <= m; val++)
sum += val;
return val; }
3|Page
• Mixing overloading and default arguments can produce ambiguities that the compiler
will not allow; consider the following group of overloaded functions:
GROUP 1: VALID; overloaded functions are acceptable in C++
void sum ()
void sum (int n)
GROUP 2: INVALID; the compiler cannot determine if the call sum() means the first
overloaded version or the second with the parameter defaulting to zero.
void sum ()
void sum (int n=0)
GROUP 3: INVALID; the functions have the same signature, sum(int). C++ does not allow
a program to contain multiple function definitions with the same signature
void sum (int m)
void sum (int n=0)
4. RECURSIVE FUNCTIONS
• Functions are also allowed to call themselves in C++. This is called RECURSION.
int factorial (int n) { int factorial(int n) {
if (n == 0) int product = 1;
return 1; for (int i = n; i > 0; i--)
else product *= i;
return n * factorial (n - 1) ; return product;
} }
Which factorial function is better, the recursive or non-recursive version?
When executed, it takes a recursive function 0.925s to compile while a non-recursive
function uses 0.685s to compile.
• Every recursive function must be provided with a way to end the recursion.
Otherwise it will call itself forever and crash the program.
4|Page
5. PASSING ARRAYS TO FUNCTIONS
• The concept behind arrays in C still holds for C++.
• An array is also a variable but it can hold multiple values of the same data type.
• An array has a name, and the values it contains are accessed via their position within
the block of memory designated for the array.
• E.g. Int age [25]; declares an array name age that could store a maximum of 25 integer
values.
•
#include <iostream>
using namespace std;
// A function to display the content of an array
void print (int a[], int n) {
for (int i = 0; i < n; i++)
cout << a[i] << " ";
cout << '\n'; }
// a function to add up the contents of an array
int sum(int a[], int n) {
int result = 0;
for (int i = 0; i < n; i++)
result += a[i];
return result; }
int main() {
int list[] = { 2, 4, 6, 8 };
// Print the contents of the array
print(list, 4);
// Compute and display sum
cout << sum (list, 4) << '\n'; }
5|Page
Programming Task VI
Using functions, write a computer program that accepts the lengths of the two
sides of a right angle triangle (A and B) and computes the length of the
hypotenuse (C) of the triangle.
A C
Write a C++ program that uses function to add up all the positive values in
an array of integers. For example, if array arr contains the elements 3, -3, 5,
2,-1, and 2, the call sum_positive(arr) would evaluate to 12, since 3 + 5 + 2
+ 2 = 12.
6|Page
READING ASSIGNMENT V
A. Textbooks to Consult:
i. Chapter Two of Object-Oriented Programming in C++
ii. Chapter Two of Shaum’s Outline Series
B. Instructions:
i. You are expected to answer all these in your own words.
ii. You are strongly advised against copying words for words from the textbook.
iii. Discuss among yourselves but don’t be tempted to allow your friend to copy your
solutions
C. Study Questions
i. Write exhaustively on why you need to employ functions in your programs?
ii. Itemize the three elements of a function in a program.
iii. Discuss the two ways of defining a function? What are the observed advantages or
disadvantages of these two ways?
iv. Differentiate between a function definition and a function declaration.
v. Explain the process behind passing arguments by value.
vi. In a function declaration line, can you include the data type of the parameters alone
or you must also include the name of the parameters.
vii. Of what importance is the function return type in a function declaration?
viii. Of what importance is the return statement in a function definition?
ix. State the difference between the float data type in the following function
declaration: float area (float);
x. How do you handle instances when you need to return more than one value at the
same time from a function?
xi. What is the default return type of all compilers?
xii. Itemize the difference between passing arguments by value and passing arguments
by reference.
7|Page
xiii. Explain the principles behind the operation of an inline function. How does it
differentiate functions of the same name but with different types of parameters or
different numbers of parameters?
xiv. Itemize 3 advantages of using default parameters.
xv. Of what importance are static local variable?
References
1. Robert Lafore (2002), “Object-Oriented Programming in C++”, Sams Publishing
Fourth Edition
2. John R. Hubbard (2000), “Schaum’s Outline of Theory and Problems of
Programming with C++”, McGraw-Hill Publishers, Second Edition.
8|Page