0% found this document useful (0 votes)
17 views10 pages

CSC 202 Session 4

Uploaded by

Abimbade Jamiu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views10 pages

CSC 202 Session 4

Uploaded by

Abimbade Jamiu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Ladoke Akintola University of Technology

Open and Distance Learning Centre


Computer Science Department
CSC 202- Computer Programming II
(Object Oriented Programming using C++)
FUNCTIONS IN C ++
1. INTRODUCTION
• A function is a named sequence of code that performs a specific task.
• It reduces the size of a program by grouping a number of program statements into a
unit and assigns a name to it. This unit can then be invoked from other parts of the
program by calling this name.

• Any sequence of instructions that appears in a program more than once is perfect to be
made into a function

• Function also facilitates code reusability whereby a function defined in a program can
be imported into another program.

• Program becomes easier to debug when you use functions


• Program becomes easier to extend when you use functions (Code Extensibility)
• Function maximizes the use of system resources especially memory as a function’s code
is stored in only one place in memory, even though a function could be executed many
times in the course of the program

• With inbuilt functions, programs become easier to write and faster to compile as you
are not interested in how your compiler does the calculation but the result of the
calculation e.g. computing a square root would have entail:

#include <iostream>
int main() {

1|Page
double input;
std::cout << "Enter number: ";
std::cin >> input;
double diff;
// Compute a provisional square root
double root = 1.0;
do { // Loop until the provisional root is close enough to the actual root
root = (root + input/root) / 2.0;
std::cout << "root is " << root << '\n';
// How bad is the approximation?
diff = root * root - input;
}
while (diff > 0.0001 || diff < -0.0001);
// Report approximate square root
std::cout << "Square root of " << input << " = " << root << '\n';
}

2|Page
• To use any inbuilt function, you must include the header file that contains the function
needed.
• Max and min function max (value1, value2) and min (value1, value2) can be used to
compute the maximum or minimum between two values. It could be found in
algorithm header file

• Every program that employs a user-defined function must have three features:
i.Function declaration: the process of informing your compiler that function will be
used is called function declaration. It entails specifying its return value, function
name and its argument type. Its syntax is:
Return_Data_Type function_name (list_of_parameters);
Function declaration must be terminated with a semicolon.
ii.Function definition: this entails making the function active by embedding
the required statements. This can be included before or after the main
function.

3|Page
After main function : Before main function:
void function_name(); return_type Function_name(optional_parameters)
main() {
{ //lines of code
….. }
Function call()
} main()
Function_name() {
{ Function call()
//lines of code }
}

iii.Function calls: this entails invoking the function to act at a certain point in our
program by specifying the function name followed by a parenthesis. i.e.

function_name(optional_parameters);
The data types of values at the point of function call must be the same with those
at the declaration point.
Function calls could be achieved in two ways; they are:
• Call by value: here, the actual values are sent to the function. The original values
cannot be modified in this approach
• Call by reference: here, the actual values are not passed into the function but
the memory address of the variable is passed. The original values cannot be
modified in this approach. In addition, this provides a mechanism for passing
more than one value from the function back to the calling program.

4|Page
5|Page
#include <iostream>
using namespace std;
int main()
{ // converts a floating point number into integer and fractional part
void intfrac (float, float&, float&); //function declaration
float number, intpart, fracpart; // normal variables declaration
do {
cout << “\nEnter a real number: “;
cin >> number;
intfrac (number, intpart, fracpart); // function call
cout << “Integer part is “<< intpart<< “, fraction part is “<< fracpart << endl;
}
while (number! = 0.0);
return 0;
} //function definition
// finds integer and fractional parts of real number
void intfrac (float n, float& intp, float& fracp)
{
long temp = static_cast<long>(n); //convert the real number to long integer,
intp = static_cast<float>(temp); //converts the number back to float
fracp = n - intp; //subtract integer part from the real number
}
Note:
• The & indicates that intp is an alias—another name—for whatever variable is passed
as an argument
• While intpart and fracpart are passed by reference, the variable number is passed
by value.

6|Page
7|Page
Programming Task
• Using any control structure and function where applicable; write a C++
program to generate a multiplication table. Such that:
1 2 3 4 5
1 1 2 3 4 5
2 2 4 6 8 10
3 3 6 9 12 15
4 4 8 12 16 20
5 5 10 15 20 25

employ setw function of the iomanip header file where applicable.

• Write a C++program to compute and display the Greatest Common


Divisor (GCF) between two numbers as well as the Lowest Common
Multiple (LCM) between two numbers. The relationship between LCM and
GCF is:
LCM (a,b) = (a.b ) / GCD(a,b)

8|Page
READING ASSIGNMENT II
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.
xiii.

9|Page
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.

SOLUTION
#include <iostream>
using namespace std;
int main()

int FirstNum, SecNum, m = 60, n = 7;

cout << "Supply your first number " << endl;

cin >> FirstNum;

cout << "Supply your second number " << endl;

cin >> SecNum;

cout << "Their sum is " << (FirstNum + SecNum) << endl;

cout << "Their difference is " << (FirstNum - SecNum) << endl;

cout << "Their product is " << (FirstNum * SecNum) << endl;

cout << "Their quotient is " << (FirstNum / SecNum) << endl;

cout << "Their remainder is " << (FirstNum % SecNum) << endl;

10 | P a g e

You might also like