COLLEGE OF ENGINEERING AND TECHNOLOGY EDUCATION
Holy Trinity College of General Santos
Daproza Avenue, General Santos City
CP2 COMPUTER
PROGRAMMING 2
FUNCTIONS
Prof. Charisse S. Ronquillo
Instructor
2nd Semester 2nd Term | 2020-2021
FUNCTIONS IN C++
• Allow to structure programs in segments of code to perform
individual tasks.
• In C++, a function is a group of statements that is given a name,
and which can be called from some point of the program. The most
common syntax to define a function is:
type name ( parameter1, parameter2, ...) { statements }
• type is the type of the value returned by the function.
• name is the identifier by which the function can be called
• parameters Each parameter consists of a type followed by an identifier, with each parameter
being separated from the next by a comma. Each parameter looks very much like a regular
variable declaration (for example: int x), and in fact acts within the function as a regular variable
which is local to the function. The purpose of parameters is to allow passing arguments to the
function from the location where it is called from.
• statements is the function's body. It is a block of statements surrounded by braces { } that specify
what the function actually does.
19/04/2021 HTC_CETE S2021 V1 2
What is an Argument?
• An argument is referred to the values that are passed within a function
when the function is called. These values are generally the source of the
function that require the arguments during the process of execution. These
values are assigned to the variables in the definition of the function that is
called.
• Also called Actual parameters.
• Example:
• Suppose a sum() function is needed to be called with two numbers to
add. These two numbers are referred to as the arguments and are
passed to the sum() when it called from somewhere else.
19/04/2021 HTC_CETE S2021 V1 3
What is a Parameter?
• The parameter is referred to as the variables that are defined during a
function declaration or definition. These variables are used to receive the
arguments that are passed during a function call.
• These parameters within the function prototype are used during the
execution of the function for which it is defined. These are also called
Formal arguments or Formal Parameters.
• Example:
• Suppose a Mult() function is needed to be defined to multiply two
numbers. These two numbers are referred to as the parameters and
are defined while defining the function Mult().
19/04/2021 HTC_CETE S2021 V1 4
// C code to illustrate Parameters
#include <stdio.h>
// Mult: Function defintion
// a and b are the PARAMETERS
int Mult(int a, int b)
{
// returning the multiplication
return a * b;
}
// Driver code
int main()
{
int num1 = 10, num2 = 20, res;
// Mult() is called with
// num1 & num2 as ARGUMENTS.
res = Mult(num1, num2);
// Displaying the result
printf("The multiplication is %d", res);
return 0;
}
19/04/2021 HTC_CETE S2021 V1 5
Example Function Code (#1)
// function example
#include <iostream>
using namespace std;
int addition (int a, int b)
{
int r;
r=a+b;
The result is 8
return r;
}
int main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
}
19/04/2021 HTC_CETE S2021 V1 6
Explanation to #1
• This program is divided in two functions: addition and main.
• Remember that no matter the order in which they are defined, a C++ program
always starts by calling main. In fact, main is the only function called
automatically, and the code in any other function is only executed if its function is
called from main (directly or indirectly).
• In the example above, main begins by declaring the variable z of type int, and
right after that, it performs the first function call: it calls addition.
• The call to a function follows a structure very similar to its declaration. In the
example above, the call to addition can be compared to its definition just a few
lines earlier:
19/04/2021 HTC_CETE S2021 V1 7
• The parameters in the function declaration have a clear correspondence to the
arguments passed in the function call. The call passes two values, 5 and 3, to the
function; these correspond to the parameters a and b, declared for function
addition.
• At the point at which the function is called from within main, the control is passed
to function addition: here, execution of main is stopped, and will only resume
once the addition function ends. At the moment of the function call, the value of
both arguments (5 and 3) are copied to the local variables int a and int b within
the function.
• Then, inside addition, another local variable is declared (int r), and by means of
the expression r=a+b, the result of a plus b is assigned to r; which, for this case,
where a is 5 and b is 3, means that 8 is assigned to r.
19/04/2021 HTC_CETE S2021 V1 8
• Ends function addition, and returns the control back to the point where the
function was called; in this case: to function main.
• At this precise moment, the program resumes its course on main returning exactly
at the same point at which it was interrupted by the call to addition.
• But additionally, because addition has a return type, the call is evaluated as
having a value, and this value is the value specified in the return statement that
ended addition: in this particular case, the value of the local variable r, which at
the moment of the return statement had a value of 8.
19/04/2021 HTC_CETE S2021 V1 9
• Therefore, the call to addition is an expression with the value
returned by the function, and in this case, that value, 8, is assigned
to z. It is as if the entire function call (addition(5,3)) was replaced by
the value it returns (i.e., 8).
19/04/2021 HTC_CETE S2021 V1 10
19/04/2021 HTC_CETE S2021 V1 11
Functions with no type. The use of void
• void can also be used in the function's parameter list to explicitly specify that the
function takes no actual parameters when called. For example, printmessage
could have been declared as:
19/04/2021 HTC_CETE S2021 V1 12
Why do we need functions?
• Functions help us in reducing code redundancy.
• If functionality is performed at multiple places in software, then rather than
writing the same code, again and again, we create a function and call it
everywhere. This also helps in maintenance as we have to change at one
place if we make future changes to the functionality.
• Functions make code modular.
• Consider a big file having many lines of codes. It becomes really simple to
read and use the code if the code is divided into functions.
• Functions provide abstraction.
• For example, we can use library functions without worrying about their internal
working.
19/04/2021 HTC_CETE S2021 V1 13
Function Declaration
• A function declaration tells
the compiler about the
number of parameters
function takes, data-types of
parameters and return type of
function.
• Putting parameter names in
function declaration is
optional in the function
declaration, but it is necessary
to put them in the definition.
Below are an example of
function declarations.
(parameter names are not
there in below declarations)
19/04/2021 HTC_CETE S2021 V1 14
Types of Functions
• Built-in Functions
• Built-in functions are also known as library functions. We need not to
declare and define these functions as they are already written in the
C++ libraries such as iostream, cmath etc. We can directly call them
when we need.
• User-defined Functions
• The functions that we declare and write in our programs are user-
defined functions.
19/04/2021 HTC_CETE S2021 V1 15
Example of Built-in Function
19/04/2021 HTC_CETE S2021 V1 16
Practice #1
#include <iostream>
using namespace std;
int max(int x, int y)
{
if (x > y)
return x;
else
return y;
}
int main() {
int a = 10, b = 20;
// Calling above function to find max of 'a' and 'b'
int m = max(a, b);
cout << "Max value between a and b is " << m;
return 0;
}
19/04/2021 HTC_CETE S2021 V1 17
Practice #2
#include <iostream>
#include <cmath>
using namespace std;
//Declaring the function sum
int sum(int,int);
int main(){
int x, y;
cout<<"enter first number: ";
cin>> x;
cout<<"enter second number: ";
cin>>y;
cout<<"Sum of these two :"<<sum(x,y);
return 0;
}
//Defining the function sum
int sum(int a, int b) {
int c = a+b;
return c;
}
19/04/2021 HTC_CETE S2021 V1 18
Assignment:
Read and study more about functions in C++.
Search for the meaning of Pascal’s Triangle.
Write a C++ code that will display Pascal’s Triangle.
Submit your code and your output.
Submit your assignment in Schoology.
19/04/2021 HTC_CETE S2021 V1 19
Reference:
• Functions - C++ Tutorials (cplusplus.com)
• Difference between Argument and Parameter in C/C++ with
Examples – GeeksforGeeks
• Functions in C/C++ - GeeksforGeeks
• Functions in C++ with example (beginnersbook.com)
19/04/2021 HTC_CETE S2021 V1 20