0% found this document useful (0 votes)
29 views

Chapter 5 - Modular Programming-2

inline keyword is a request to compiler to expand the body of the function at the point of call instead of generating call to actual function. ▪ Example: Chapter 5 33 10. Recursive function ▪ A recursive function is a function that calls itself during its execution. ▪ Example: Factorial function using recursion: Chapter 5 34 Cont’d . . . ▪ Base case: When n = 0, return 1 ▪ Recursive case: For n > 0, return n * factorial(n-1) ▪ Recursive calls: - factorial(3) calls factorial(2) - factorial(2)

Uploaded by

Yohannes Tibebu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Chapter 5 - Modular Programming-2

inline keyword is a request to compiler to expand the body of the function at the point of call instead of generating call to actual function. ▪ Example: Chapter 5 33 10. Recursive function ▪ A recursive function is a function that calls itself during its execution. ▪ Example: Factorial function using recursion: Chapter 5 34 Cont’d . . . ▪ Base case: When n = 0, return 1 ▪ Recursive case: For n > 0, return n * factorial(n-1) ▪ Recursive calls: - factorial(3) calls factorial(2) - factorial(2)

Uploaded by

Yohannes Tibebu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Fundamentals of

Computer Programming

Chapter 5
Modular Programming
(Function in C++)

Chere L. (M.Tech)
Lecturer, SWEG, AASTU
1
Outline
▪ Introduction to modular programming
▪ Function declaration and definition
▪ Calling a function and Return types
▪ Function parameters (Value Vs. Reference)
▪ Parameter Passing
✔ by value
✔ by reference
▪ Default arguments
▪ Function Overloading
▪ Scope of variables (revision)
▪ Special functions (recursive functions, inline functions)
▪ Array in function (array as parameter, return array, call with array)
Chapter 5 2
1. Introduction Modular Programming
Modular programming
 Programming approach in a complex problem breaking down in
to smaller manageable pieces
 The design of a program into individual components (modules)
that can be programmed and tested independently.
 It is a requirement for effective development and maintenance
of large programs and projects
 Procedures of a common functionality are grouped together
into separate modules.
 A program therefore no longer consists of only one single part
 It is now divided into several smaller parts which interact and
which form the whole program.
Chapter 5 3
Cont’d . . .
Modular program
 A program consisting of interrelated segments (or modules)
arranged in a logical and understandable form
 Modules in C++ can be classes or functions

Why Modular programming?


 Easy to write a correct small function

 Code Re-usability – Write once and use multiple times

 Code optimization – No need to write lot of code

 Maintainability – Easily to debug, maintain/modify the program

 Understandability – Easy to read, write and debug a function

Chapter 5 4
2. The concepts of Function

Functions
 A function is a block of code (subprogram) that performs a
specific task.
 Complicated tasks should be broken down into multiple
functions.
 Each can be called in turn when it needed

 Note:
– Every C++ program has at least one function, main().

Chapter 5 5
Cont’d . . .

 A function can Accepts


an input, act on data
(process the input) and
return a value (produces
an output).

 A function’s processing is
encapsulated and hidden
within the function

Chapter 5 6
Cont’d . . .

Types of Functions

Chapter 5 7
3. Function declaration and definition
Components of a function
 Function name
 Function arguments (parameters)
 Function body
 Return type

Creation of a function
▪ Function declaration
 The process of tells the compiler about a function name.
 Also called function prototype creation
▪ Function definition
 Give body of function (i.e. write logic inside function body).
Chapter 5 8
Cont’d . . .
There are three ways to declare a function:
 Write your prototype into a separate file, and then use the
#include directive to include it in your program.
 Write the prototype in side your program before the main()
function.
 Define the function before it is called by any other function.
 The definition act as its own declaration.

▪ Declaration syntax:

▪ Definition syntax:

Chapter 5 9
Cont’d . . .
▪ A function may ▪ The name of function it is decided by
return a value. programmer
▪ It refers to the data ▪ Should be meaningful valid identifier
type of the value the
function returns.
▪ It is optional (void). ▪ A value which is pass in
function at the time of
calling of function
▪ It is like a placeholder.
▪ It is optional.
▪ Parameter identifier is
also optional

▪ The collection
of statements ▪ Value returned by the function
Chapter 5 ▪ Single literal or expression 10
Cont’d . . .
Function Header
 First line of a function, which contains:
 The type of data returned by the function (if any)
 The name of the function
 The type of data that must be passed into the function
when it is invoked (if any)

Chapter 5 11
Cont’d . . .
Examples

Chapter 5 12
4. Function calling, execution and return
Function calling Function return
 Syntax :  Syntax :
func_name(parameters ); return value/variable;
or or return expression;
Variable = func_name(parameters);

Chapter 5 13
Cont’d . . .
Function execution

Chapter 5 14
5. Parameter passing
▪ Parameter is means by which functions are communicating and
passing data
▪ Parameters are either Actual parameter or Formal Parameters

The variable or expression corresponding


to a formal parameter that appears in
the function or method call in the calling
environ

A variable and its type as they


appear in the prototype of the
Chapter 5 15
function or method.
Cont’d . . .

Chapter 5 16
Cont’d . . .
Value Type Vs. Reference Type

Chapter 5 17
Cont’d . . .

Parameter passing
by Value Vs. by Reference

Chapter 5 18
Cont’d . . .
Parameter passing by Value Vs. by Reference

call by value call by reference


This method copy original This method copy address of
1 value into function as a arguments into function as a
arguments. arguments.
Changes made to the
Changes made to the parameter
parameter inside the function
2 affect the argument. Because address
have no effect on the
is used to access the actual argument.
argument.
Actual and formal arguments
Actual and formal arguments will be
3 will be created in different
created in same memory location
memory location

Note: By default, C++ uses call by value to pass arguments.


Chapter 5 19
Cont’d . . .

Example 1:
swapping two numbers

Chapter 5 20
Cont’d . . .

Chapter 5 21
Cont’d . . .
Example 2

Chapter 5 22
Cont’d . . .
Example 2 . . . .

Chapter 5 23
6. Default Arguments

 In C++ programming, we can provide default values for


function parameters.

 If a function with default arguments is called without passing


arguments, then the default parameters are used.

 However, if arguments are passed while calling the function,


the default arguments are ignored.

Chapter 5 24
Cont’d . . .
▪ Example 1

Chapter 5 25
Cont’d . . .
▪ Example 2

Chapter 5 26
Cont’d . . .
Things to Remember
 Once we provide a default value for a parameter, all
subsequent parameters must also have default values.
 For example:

 If the default arguments are provided in the function


definition instead of the function prototype, then the
function must be defined before the function call
Chapter 5 27
7. Function Overloading
• In C++, two functions can have the same name if the number
and/or type of arguments passed is different.
• These functions having the same name but different
arguments are known as overloaded functions.
• For example:

Chapter 5 28
Cont’d . . .
Example

Chapter 5 29
Cont’d . . .
▪ Example

Chapter 5 30
8. Revision on variable scope
▪ The scope of a variable is the portion of the program where the
variable is valid or "known".

Chapter 5 31
Cont’d . . .
▪ What is the output of the following code fragment?

Output:

Chapter 5 32
9. Inline function
▪ If a function is inline, the compiler places a copy of the code
of that function at each point where the function is called.
▪ To make any function inline function just preceded that
function with inline keyword.

33
Chapter 5
Cont’d . . .
Why use Inline function
▪ Whenever we call any function many time then, it take a lot of
extra time in execution of series of instructions such as saving
the register, pushing arguments, returning to calling function.
▪ For solve this problem in C++ introduce inline function.
▪ The main advantage of inline function is it make the program
faster.

▪ Example

34
Chapter 5
10. Recursive function

▪ A function that calls itself is


known as a recursive Function call
function.

▪ The technique is known as


recursion.

Recursive call

Chapter 5 35
Cont’d . . .

▪ Example:
factorial finder
function

Chapter 5 36
10. Function with Array
(a) Calling function with array element
▪ Indexed variables can be arguments to functions
▪ Example: If a program contains these declaration
int i, n, a[10];
void my_function(int n);
▪ An array elements a[0] through a[9] are of type int, and
calling the function as follow is legal:
my_function( a[ 0 ] );
my_function( a[ 3 ] );
my_function( a[ i ] );
Note:
 In the same it works for 2D array and string
Chapter 5 37
Cont’d . . .
(b) Array as formal parameter
▪ An entire array can be used as a formal parameter
▪ Such a parameter is called an array parameter
▪ It is neither a call-by-value nor a call-by-reference parameter
▪ However, behave much like call-by-reference parameters
▪ An array parameter is indicated using empty brackets or with
array size in the parameter list
void fill_up(int a[ ], int size); or
void fill_up(int a[5 ], int size);
• Note:
 Because a function does not know the size of an array argument, the
programmer should include a formal parameter that specifies the size
of the array as shown in the example above
Chapter 5 38
Cont’d . . .
Example 1: passing 1D arrays to function

Function declaration:
To receive an array of int, arr[]
as argument

Chapter 5 39
Cont’d . . .
Passing arrays to function ……

Chapter 5 40
Cont’d . . .
(c) Returning an Array (is it possible?)
▪ Recall that functions can return a value (single data
element) of type int, double, char, . . .
▪ Because array consist a set of the same type data
elements, functions cannot return array.
▪ However, an individual array element (single array
element specified by index) can be returned.
▪ For example: int myFunc(){
int myArray[10]; //instead this is valid
- - - - -- return myArray[1];
return myArray; //invalid
}
Chapter 5 41
Cont’d . . .
Example 2: Passing 2D array to function
Note:
 With 2D arrays, You can
specify both dimensions

 However, the first


dimension (number of
rows) may be omitted, like
you see on the
Read2Array() function

 But the second dimension


(number of columns)
cannot be omitted.

Chapter 5 42
Cont’d . . .

2D array as
parameter
example …

Chapter 5 43
Cont’d . . .

Example 3:
 Passing string to
function as
argument

Chapter 5 44
Summary of Function

Chapter 5 45
Reading Resources/Materials
Chapter 9 & 10:
✔ Diane Zak; An Introduction to Programming with C++ (8th
Edition), 2016 Cengage Learning

Chapter 5:
✔ Walter Savitch; Problem Solving With C++ [10th edition,
University of California, San Diego, 2018
Chapter 6:
✔ P. Deitel , H. Deitel; C++ how to program, 10th edition,
Global Edition (2017)

Chapter 5 46
Thank You
For Your Attention!!

47

You might also like