0% found this document useful (0 votes)
25 views60 pages

CSC 126 CH5

Chapter 5 discusses functions in programming, detailing their definition, types (library and user-defined), and how they can be invoked. It explains the structure of functions, including function prototypes, definitions, and calls, as well as parameter passing methods (by value and by reference). The chapter emphasizes the benefits of using functions, such as code reusability and easier maintenance.

Uploaded by

2024223538
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)
25 views60 pages

CSC 126 CH5

Chapter 5 discusses functions in programming, detailing their definition, types (library and user-defined), and how they can be invoked. It explains the structure of functions, including function prototypes, definitions, and calls, as well as parameter passing methods (by value and by reference). The chapter emphasizes the benefits of using functions, such as code reusability and easier maintenance.

Uploaded by

2024223538
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/ 60

Chapter 5: Functions

Contents
• Introduction
• Library functions
• User-defined functions
• Functions with parameter
– Value
– Reference
What is function
• A mini-program containing algorithms to do a
specific task
• A function could be invoked (called) many
times through its name (to execute its task),
by the main function or other functions
• When a function is invoked, the computer will
execute all the instructions in the function
from start to end, and then it goes to the
instruction after the function call
Function classification
• 2 types of functions :-
1. Predefined function or built-in or library function
2. Independent function or user-defined function
Library Function
• Functions that are ready-made by the
producer of a compiler and stored in the
header files (.h) called libraries
• To use the predefined function, the
appropriate library file must be included in the
preprocessor directive (# include)
• Common header files are :-
a) iostream.h c) string.h e) math.h
b) iomanip.h d) char.h f) stdlib.h
Library Function
Library Function
Library Function

Function call
Library Function
Library Function

Function call
Library Function
User-defined Function
• A function is a mini‐program that performs a
particular task written by the programmer.
• Function is a sequence of statements that can be
called multiple times, with different values for its
parameters if any.
• The way the functions are used (called) is the
same as library functions
• A function is called by other program- usually the
main() program when the function is needed by
the main() program.
User-defined Function
• Benefits:
– EASE OF UNDERSTANDING
• Each module should perform just one function, so it is easier to
identify a structure of a module.
– REUSABLE CODE
• A well-written of code in modules can be used more than once within
a program or in other programs. It is more reliable and save time.
– ELIMINATION OF REDUNDANCY
• Using modules can help to avoid the repetition of writing out the
same segment of code more than one.
– EFFICIENCY OF MAINTENANCE
• Each module should be self-contained and have little or not effect on
other modules within the algorithm. The modules can be divided
among multiple programmers to work on a problem.
Example: calculating sum of two
numbers
Simple design Modular design
Begin Begin
Input num1, num2 call sum()
sum = num1 + num2 End
Display sum
End sum module
sum()
Input num1, num2
sum = num1 + num2
Display sum
Return
Simple design Modular design
Begin
Begin

Call
Input num1, sum()
num2

End
sum = num1 + sum()
num2
Input num1,
num2
Display sum

sum = num1 +
End num2

Display sum

Return
Simple design Modular design
int main() int main()
{ {
int num1, num2, sum=0; sum();
cout<<“please enter 2 numbers”; }
cin>> num1>> num2;
//function sum
sum = num1 + num2 void sum()
{
cout<<“answer is :”<<sum; int num1, num2, sum=0;
} cout<<“please enter 2 numbers”;
cin>> num1>> num2;

sum = num1 + num2

cout<<“answer is :”<<sum;
}
User-defined Function
When using functions, we can have four types of
functions.
– Function without value returned and without
parameter
– Function without value returned but with
parameter
– Function with value returned but without
parameter
– Function with value returned and with parameter
User-defined Function
• 3 requirements to use the user-
defined/independent function in C++ :-
– Function prototype declaration
– Function call
– Function definition
User-defined Function
1

Click here

3
Click here

Click here

Function Structure in C++


User-defined Function – Function
Prototype
• It is a function declaration.
• Placed just before the main program and sometimes at the beginning of the main program, a
function declaration specifies:
– The return type
– The name of the function
– The order and type of parameters

• The purpose of function prototype allows the compiler to check for the existence of the
function, the parameter list and the return type is correct.
User-defined Function – Function
Prototype
User-defined Function – Function
Prototype
• The syntax for the independent function prototype declaration is given
below:
User-defined Function – Function
Prototype
• The following are examples of the function prototype:
User-defined Function – Function
definition
• A function must be defined before it can carry out the task assigned to it.
• Tasks are statements written inside the function definition.
• A function is written once in the program and can be used by any other function in
the program.
• The function definition is placed either before the main() or after the main().
User-defined Function – Function
definition
• A function definition consist of a function header and function body as
shown below:
User-defined Function – Function
definition
returnType functionName (type parameter_list)
{
declaration(s);
statement(s);
return expression;
}
User-defined Function – Function
definition
returnType functionName (type parameter_list)
{
declaration(s);
statement(s);
return expression;
}
User-defined Function – Function
definition
• An example of function definition that accepts three values, calculates the total
and display the total.

void sum3Num(int a, int b, int c)


{
int total = a + b + c;
cout<<total;
}

Explanation
• The keyword void in the function header indicated the function that does not return
a value after completing its assigned task (called void function)
• The parameter-list int a, int b and int c indicated that three integer values will be
sent into (accept/ receive) and used in the function sum3Num
User-defined Function – Function
definition
• An example of function definition that accepts three values, calculates the total
and return the total to the main function.

int sum3Num(int a, int b, int c)


{
int total = a + b + c;
return total;
}

Explanation
• The keyword int in the function header indicated that the function will return an
integer value.
• The parameter-list int a, int b and int c indicated that three integer values will be
sent into (accept/ receive) and used in the function sum3Num
• return total indicated that the value of total will be returned to the calling
function
User-defined Function – Function call
• A function is made to perform its designated task by a
function call in main function or in other function.
• When a function is called, the program control is passed to
the called function (function definition) and the statements
inside the function will be executed to the end until control is
passed back the function call (function invocation).
• To call a function, specify the function name and the values of
the parameters that the function needs to do it job.
• The following syntax is used when calling a function:
Function_name(actual parameters);
User-defined Function – Function call
• Example of function calls:
void main()
{
findMin(num1, num2);
}
User-defined Function – Function call
• Completing a function call
– Function call that returns no value (void function)
– Function call that returns a value
User-defined Function – Function call
• Function call without value returned (void
function)
– This type of function does not return any value to
the calling function (function call).
– Upon completing the last statement in the
function definition, the control is passed back to
the statement that calls the function in the calling
function (function call).
– Thus, the next statement in the calling function
will be executed.
User-defined Function – Function call
Explanation
• The statement calcSum(num1, num2, num3);
causes the function calcSum() to be executed.
• Upon completing the execution in the calcSum(),
control passed back to the calling statement in the main().
• The next statement, cout << “Next process…”;
will get executed.

Sample output:
User-defined Function – Function call
• Function call with value returned
– A function may also return a value. In this type of
function, when the execution in the called function
(function definition) is complete, the control is passed
back to the calling function with a value. This value
will be used in the calling function.
– There are four ways how a returned value can be used
in the calling function.
• In an arithmetic expression
• In a logical expression
• In an assignment statement
• In an output statement
User-defined Function – Function call
User-defined Function – parameter
• There are two types of parameters:
– Formal parameter
– Actual parameter
User-defined Function – parameter
• Formal parameter
– Formal parameter is arguments in the header of function
definition.
– Formal parameter contains the values that being passed by
actual parameter.
– The number of the formal parameters must agree with the
number of actual parameters passed from the function
call.
– Example:
User-defined Function – parameter
• Actual parameter
– Actual parameter could be a constant, variable or
expression in a function call that corresponds to the formal
parameter.
– Actual parameters contain values that will be passed to the
function definition
– Example:
User-defined Function
• In summary, when using functions, we can
have four types of functions.
– Function without value returned and without
parameter
– Function without value returned but with
parameter
– Function with value returned but without
parameter
– Function with value returned and with parameter
User-defined Function
• Function without value returned and without
parameter
User-defined Function
• Function without value returned but with parameter
User-defined Function
• Function with value returned but without parameter
User-defined Function
• Function with value returned and with parameter
User-defined Function- how values are
pass between function?
• A function communicates with another
function by passing values between them.
• A function can send data to other function,
and it can also accept values sent by other
functions.
• Data can be passed between function through
– Global variable Not advisable
– Parameter passing
– A return value
User-defined Function- how values are
pass between function?
• Parameter passing
– Parameters can be passed from the calling
function (function call) to the called function
(function definition) using two type of passing
which are:
• Parameter passing by value
• Parameter passing by reference
User-defined Function- how values are
pass between function?
• Parameter passing – pass by value
– When passing by value, a copy of that value is
passed from the calling function to the called
function through the parameters.
– The changes made to the copied value do not
change the original value.
User-defined Function- how values are
pass between function?

Calling function Called function


(function
definition)
main() num 4

Displayvalue() value 4 12
User-defined Function- how values are
pass between function?
User-defined Function- how values are
pass between function?
• Parameter passing – pass by reference
– When there is more than one value to be returned to the calling function or
changed during the program execution, reference parameter is used.
– When a parameter is sent by reference, the function actually gets the memory
location of the actual parameter it can directly access the data.
– In other words, the address of the variable in the computer’s memory is
actually being passed. As a result, the changes made to the variable will affect
the original value.
– When passing by reference, the actual parameter in the calling function must
be variable and the formal parameter must use the reference operator, which
is referred by the symbol ampersand (&). The reference operator forces the
corresponding actual formal parameters to refer to the same location.
– In addition, the returned-type for passing parameters by reference is typically
given the type void.
Independent Function- how values are
pass between function?
Example - Pass by value
#include <iostream.h> void addFour(int a, int b)
{
void addFour(int a, int b); a = a + 4; Value parameter
b++;
void main() cout<<a<<b<<endl;
{ }
int num1,num2;
num1 = 3;
num2 = 1;
cout<< num1<<num2<<endl;
addFour(num1,num2);
cout<<num1<<num2<endl;
}
In main BEFORE calling In main AFTER calling
addFour function In addFour function
addFour function
num1 3 a 7 num1 3

num2 1 b 2 num2 1
Example - Pass by reference
#include <iostream.h> void addFour(int &a, int b)
{
void addFour(int& a, int b); a = a + 4; //line 1
b++; //line 2
void main() cout<<a<<b<<endl;
{ }
Reference parameter
Reference parameter
int num1,num2; When the value of a changes,
num1 = 3; value num1 changes as well
num2 = 1;
cout<< num1<<num2<<endl;
addFour(num1,num2);
cout<<num1<<num2<endl;
}
In main BEFORE calling In main AFTER calling
addFour function In addFour function
addFour function
num1 3 a 7 num1 7

num2 1 b 2 num2 1
Example - Pass by reference
• How it works?
In main BEFORE calling In addFour function
addFour function

num1 3 a

num2 1 b 1

Before statement in line 1 execute!!

In main BEFORE In addFour function


calling addFour function
num1 7 a

num2 1 b 2

In function addFour, after statement in line 1 and line 2 execute!!


User-defined Function- how values are
pass between function?
Independent Function- how values are
pass between function?
Summary
• Void function: a function that does not have a
data type
• A return statement without any value can be
used in a void function to exit function early
• The heading of a void function starts with the
word void
• To call a void function, you use the function
name together with the actual parameters in a
stand-alone statement
Summary
• Two types of formal parameters: value
parameters and reference parameters

• A value parameter receives a copy of its


corresponding actual parameter

• A reference parameter receives the address


(memory location) of its corresponding actual
parameter
Summary
• If a formal parameter needs to change the value
of an actual parameter, in the function heading
you must declare this formal parameter as a
reference parameter
• Variables declared within a function (or block) are
called local variables
• Variables declared outside of every function
definition (and block) are called global variables
The end…

You might also like