Lecture No. 8
Lecture No. 8
LECTURE #8
COMPUTER PROGRAMMING (ELCP-206)
2
C++ FUNCTION: INTRODUCTION
As a fundamental feature of C++, functions allow programmers to encapsulate
code into reusable blocks, which can be called as needed throughout a program.
This introduction to C++ functions will cover their importance, types, and some
advanced features that leverage the power of this programming language.
3
FUNCTIONS
• A function groups a number of program statements into a unit and gives it a name.
• This unit can then be invoked from other parts of the program.
• The most important reason to use functions is to aid in the conceptual organization of a
program.
• Another reason to use functions is to reduce program size.
• Any sequence of instructions that appears in a program more than once is a candidate for
being made into a function.
• The function’s code is stored in only one place in memory, even though the function is
executed many times in the course of the program.
What Are Functions in C++?
➢ A function in C++ is a self-contained block of code that performs a specific
task. Once defined, a function can be executed whenever it is called from
other parts of the program or even from within itself, enabling code reuse
and better organization.
➢ Functions help to break down complex programming tasks into smaller,
more manageable components, thereby aiding in program maintenance
and understanding.
➢ Importance of Functions
▪ Modularity, Reusability, Scoping
➢ Types of Functions
▪ Standard Functions, Void Functions, Inline Functions, Recursive
Functions, Lambda Functions
6
C++ FUNCTION: INTRODUCTION
Definition and declaration
7
FUNCTIONS IN C++
8
DEFINITION AND DECLARATION
9
Components of Function Syntax
▪ Return Type: Specifies the type of value the
function returns (e.g., int, float, void).
▪ Function Name: The identifier used to call the
function.
▪ Parameter List: A comma-separated list of
parameters, each with a type and a name. It can be
empty if the function takes no arguments.
▪ Function Body: Enclosed in curly braces {},
containing statements that define what the function
does.
10
Code Explanation
▪ Function Definition: add
o This defines a function named add that takes two integer parameters a and b and returns their sum as an
integer.
▪ Function Logic
o The function computes the sum of a and b and returns the result.
▪ Calling the add Function
o The add function is called with arguments 5 and 7. The returned sum (which is 12) is then printed to the
console using cout. 11
Code Explanation
▪ Program Termination
o This line indicates that the program executed successfully and terminates the main
function.
12
C++ FUNCTION: INTRODUCTION
Definition and declaration
Function parameters
13
FUNCTION PARAMETERS
14
▪ Normal variables, i.e. no pointer variables, are passed to the function as function
parameters by value. This means that during the execution of a function a copy of the
variable is created on the stack of the processor.
▪ In C++ there is also the possibility that the parameter passing is done by pointer. In
this case the addresses of the variables, and not the variable itself, are passed to the
function.
▪ C++ has introduced another form of parameter passing, the passing by reference. This
is identified by the fact that the function parameter is preceded by a &, which must not be
confused with the address operator & in this context.
▪ Passing by reference is generally preferred over passing by pointer in C++ because:
1. Efficiency: Avoids unnecessary copying of large objects.
2. Simplicity: Easier to use and less prone to errors.
3. Safety: Reduces the risk of memory leaks and dangling pointers.
However, there are specific cases where passing by pointer might be more suitable, such
as when dealing with dynamic memory allocation or when null pointers need to be
handled explicitly.
15
Passing by Value…
Code Explanation
▪ This is a simple C++ program that defines a function
add to perform addition of two integers. The
function takes two arguments a and b, adds them,
and returns the result.
▪ In the main function, the add function is called
twice, passing the arguments 1 and 3 and then 5 and
6. The returned result from the function calls is then
printed to the console using the cout statement.
16
Passing by Pointer…
Code Explanation
▪ This is a similar C++ program as the previous one,
but the add function now takes two pointers a and b
as arguments instead of two integers. The add
function performs the addition of the values pointed
to by the two pointers and returns the result.
▪ In the main function, two variables a and b are
declared and initialized with values 1 and 3,
respectively. Another variable c is declared and
initialized with value 5. The add function is then
called twice, passing the addresses of a and b and
then a and c. The returned result from the function
calls is then printed to the console using the cout
statement.
17
Passing by Reference…
Code Explanation
▪ This is another similar C++ program as the previous
ones, but the add function now takes two references
a and b as arguments instead of two integers or
pointers. The add function performs the addition of
the values referred to by the two references and
returns the result.
▪ In the main function, two variables a and b are
declared and initialized with values 4 and 6,
respectively. Another variable c is declared and
initialized with value 12. The add function is then
called twice, passing the references of a and b and
then a and c. The returned result from the function
calls is then printed to the console using the cout
statement.
18
Difference between call by value and call by reference in C++
19
EXPLANATION
• The declaration tells the compiler that at some later point we plan to present a function
called starline.
• The keyword void specifies that the function has no return value, and the empty
parentheses indicate that it takes no arguments.
• Function declaration is terminated with a semicolon.
• Function declarations are also called prototypes.
• They tell the compiler, “a function that looks like this is coming up later in the program, so
it’s all right if you see references to it before you see the function itself.”
• The information in the declaration (the return type and the number and types of any
arguments) is also sometimes referred to as the function signature.
CALLING THE FUNCTION
• The syntax of the call is very similar to that of the declaration, except that the return type is
not used.
• The call is terminated by a semicolon.
• Executing the call statement causes the function to execute; that is, control is transferred to
the function, the statements in the function definition are executed, and then control returns
to the statement following the function call.
• In our example function is called (or invoked, or executed) three times from main().
• Each of the three calls looks like this:
• This is all we need to call the function: the function name, followed by parentheses
THE FUNCTION DEFINITION
• The definition consists of a line called the declarator, followed by the function body.
• The function body is composed of the statements that make up the function, delimited by
braces.
• The declarator must agree with the declaration: It must use the same function name, have
the same argument types in the same order (if there are arguments), and have the same
return type.
• When the function is called, control is transferred to the first statement in the function body.
• The other statements in the function body are then executed and when the closing brace is
encountered control returns to the calling program.
COMPARISON WITH LIBRARY FUNCTIONS
• Where are the declaration and definition for the library function?
E.g.,
• Where are the declaration and dedefinition for this library function?
• The declaration is in the header file specified at the beginning of the program
(CONIO.H, for getche()).
• The definition (compiled into executable code) is in a library file that’s linked
automatically to your program when you build it.
Eliminating the
Declaration
THANK YOU...
29