SLG 7.1 CS2 Introduction to Functions 7.1
SLG 7.1 CS2 Introduction to Functions 7.1
TARGET
Let us imagine an automobile factory. When an automobile is manufactured, it is not made from
basic raw materials, it is put together from previously manufactured parts. Some parts are maybe
made by more than one company or by itself.
https://www.vectorstock.com/royalty-free-vector/car-factory-robotic-automotive-assembly-line-vector-22266350
From the previous lessons, we have learned that c++ contain a collection of functions like main(). The
programming instructions are packed into one function. This technique is good for small or short
programs only. For a large-scale program, it is impractical to put all the instructions into one program.
This module will teach us how we can break problems into manageable pieces and solve it piece by
piece trough functions.
LG-JOAQUIN-CS2-7.1 CS 2 Page 1 of 6
Modular Programming
Modular programming is the process of dividing or breaking a large computer program into separate,
small parts, or functions. Modular programming increases the maintainability and readability of the
program. It makes the program easy to change or modify by the programmer itself or other
programmers.
Functions are also called modules. Instead of writing one long function that contains all necessary
statements to solve a problem, we can write several small functions that can solve specific parts of the
problem. These functions in turn can be executed in a desired order to solve the problem. This approach
is sometime called divide and conquer because we divide the problem into smaller problems that can
be easily be solved.
Figure 1
Figure 1 illustrates the idea of divide and conquer approach. Let us compare the two programs. One that
uses a long complex function containing all the necessary statements to solve the problem, and another
that uses the divide and conquer approach by dividing the problems into smaller parts, each part is
handled by a separate function.
LG-JOAQUIN-CS2-7.1 CS 2 Page 2 of 6
Standard library functions or simply C++ library functions are built-in functions in C++ programming.
The prototype and data definitions of these functions are present in their respective header files.
Programmers can use these functions by invoking/calling the functions directly; they do not need to
write the functions themselves.
In order to use these functions, we need to include the header file in which these library functions are
defined in our program. For instance, in order for us to use mathematical functions such as sqrt(), cos(),
fabs(), ceil(), we need to include the header file cmath in our program.
Example 1:
#include<iostream>
#include<cmath>
int main(){
double number, squareRoot, x;
number = 12.0;
cin >> x;
squareRoot = sqrt(number);
cout << "The square root of " << number << " is " << squareRoot <<
endl;
cout << "The square root of " << x << " is " << sqrt(x);
return 0;
}
Output:
25
LG-JOAQUIN-CS2-7.1 CS 2 Page 3 of 6
The square root of 12 is 3.4641
The square root of 25 is 5
In the sample program (Example 1), the sqrt() library function is used to calculate the square root of a
number. The function declaration is defined in the cmath header file. That is why, we need to use the
code #include <cmath> for us to be able to use the function sqrt(). Standard library functions will
be further discussed in the succeeding learning guides.
In the example above, it also illustrates that using a function in a program enhances the program’s
readability because it reduces the complexity of the function main. We can also reuse our function in
the program (or different programs) without rewriting the same code. For instance, we use sqrt function
more than once.
User-defined function
C++ allows the programmer to define their own function. Because C++ does not provide every function
that we may need and the designer cannot possibly know a user’s specific need. Unlike the built-in
functions that we can just call them to use, user-defined function must be created first so that we can
call them.
When creating a function, we must write its definition. all function definitions have the following parts:
Return type: A function can send a value to the part of the program that executed it. The return type
is the data type of the value that is sent from the function.
Name: You should give each function a descriptive name. In general, rules in naming variables
also apply to function names.
Parameter list: The program can send data into function. The parameter list is the list of variables that
holds the values being passed to the function.
Body: The body of a function is the set of statements that perform the function’s operation.
They are enclosed in a set of braces.
Figure 2
LG-JOAQUIN-CS2-7.1 CS 2 Page 4 of 6
Figure 2 shows the definition of a simple function. The line in the definition that reads int main()
is called the function header.
A function is executed when it is called/invoke. We should take note however that the function main is
called automatically when a program starts, and that all other functions must be executed by function
call statements. When a function is called, the program branches to that function and executes the
statements in its body. This means that the program pauses in executing the rest of the statements after
the function call and goes to the body of the called function to execute the statements there. See Figure
3.
Figure 3
Let us try to look the sample program below, which contain two functions; main and larger.
Example 2:
#include <iostream>
using namespace std;
int larger(int, int);
int main(){
int a, b;
cin >> a >> b;
cout << "The larger value between " <<a <<" and " << b <<" is:
"<<larger(a,b);
return 0;
}
Program output:
34 43
The larger value between 34 and 43 is: 43
In the example program above, we can say that before the program executes return 0 of main, the
program went to the larger function and executed all its statements and went back to main afterwards
and executed return 0.
LG-JOAQUIN-CS2-7.1 CS 2 Page 5 of 6
Further discussion of these topics will be presented in the succeeding modules.
KNOT
Functions allow us to modularize a program by separating its tasks into self-contained units. We
can use library function with our own functions in every program. The function we created are
referred to as user-defined function. There are advantages in modularizing a program with
functions. One is the divide and conquer approach, we can break down the problem into small
pieces and solve each part. Debugging and maintaining programs is easier when we divide the
program into meaningful functions. Another is software reuse.
REFERENCES
Malik, D. (2011). C++ Programming (5th ed., pp. 6-9). Boston, MA: Course Technology.
Gaddis, T. (2015). Starting out with C++: From control Structures through Objects. (8 th ed.). USA:
Pearson.
https://www.geeksforgeeks.org/
https://www.programiz.com/c-programming/c-functions
Prepared by: JOHN RAINER M. JOAQUIN Reviewed by: Trextan Thaddeus Sanchez
Position: SSTIII Position: SSTIII
Campus: PSHS-BRC Campus: PSHS-SMC
© 2020 Philippine Science High School System. All rights reserved. This document may contain proprietary information and may only be
released to third parties with approval of management. Document is uncontrolled unless otherwise marked; uncontrolled documents are
not subject to update notification.
LG-JOAQUIN-CS2-7.1 CS 2 Page 6 of 6