You are on page 1of 26

Data structures and algorithms

(UCS540)

Top-down design with functions

February 6, 2023

(DSA Ashish Kumar Gupta) Unit-I February 6, 2023 1 / 26


Building programs from existing information
Software development process (SDP) documentation
Phases: Analysis and Design phases
Outcome: System documentation
System documentation
consist of a description of a problem’s data and requirements
its solution algorithm (abstract/detailed design)
can be used as a starting point in coding your program.

SDLC

(DSA Ashish Kumar Gupta) Unit-I February 6, 2023 2 / 26


Case Study: Convert distance in miles to kilometers.

PROBLEM
Get distance travelled in miles. Convert it in to kilometers.

Analysis
Input:distance in miles. Only one output : distance in
kilometers.
Type of both variables: double.
DATA REQUIREMENTS
Problem Constant: KMS PER MILE 1.609
Problem Input: miles /* distance in miles */
Problem Outputs : kms /* distance in kilometers */
Relevant Formulas
distance in kms = distance in miles × 1.609

(DSA Ashish Kumar Gupta) Unit-I February 6, 2023 3 / 26


Design
After identifying the problem inputs and outputs, list the steps
necessary to solve the problem. Pay close attention to the order of
the steps.
INITIAL ALGORITHM
1. Get the distance in miles.
2. Convert the distance to kilometers.
3. Display the distance in kilometers.
ALGORITHM REFINEMENTS
Refine any steps that do not have an obvious solution (step 2).
Step 2 Refinement
2.1 Distance in kilometers is 18. 1.609 * distance in miles.

(DSA Ashish Kumar Gupta) Unit-I February 6, 2023 4 / 26


Edited Data Requirements and Algorithm for Conversion Program

(DSA Ashish Kumar Gupta) Unit-I February 6, 2023 5 / 26


Library Functions and Code Reuse
A function is a block of code that performs a specific task.
Primary goal of software engineering: to write error-free code.
Code reuse: reusing program fragments that have already been
written and tested whenever possible.
C promotes reuse by providing many predefined functions that
can be used to perform mathematical computations.

(DSA Ashish Kumar Gupta) Unit-I February 6, 2023 6 / 26


(DSA Ashish Kumar Gupta) Unit-I February 6, 2023 7 / 26
Top-down design and structural charts

For problems requiring complex algorithms for their solution:


we must break up the problem into subproblems to develop the
program solution.
In attempting to solve a subproblem at one level, we introduce new
subproblems at lower levels.
Top-down design is a process/problem solving method
proceeds from the original problem at the top level to the sub problems
at each lower level.
in which you first break a problem up into its major subproblems and
then solve the subproblems to derive the solution to the original prob-
lem.
Structural chart: A documentation tool that shows the relationships
among the subproblems of a problem.

(DSA Ashish Kumar Gupta) Unit-I February 6, 2023 8 / 26


Case Study: Drawing Simple Diagrams

(DSA Ashish Kumar Gupta) Unit-I February 6, 2023 9 / 26


Case Study: Drawing Simple Diagrams

(DSA Ashish Kumar Gupta) Unit-I February 6, 2023 10 / 26


Structural Chart

(DSA Ashish Kumar Gupta) Unit-I February 6, 2023 11 / 26


User-defined functions

Top-down design can be implemented in a program by defining user-


defined functions.
Often, one has to write one function subprogram for each subproblem
in the structure chart.

(DSA Ashish Kumar Gupta) Unit-I February 6, 2023 12 / 26


Function Components

Figure: Function Components: Function prototype, function definition and


function call.

(DSA Ashish Kumar Gupta) Unit-I February 6, 2023 13 / 26


Function prototype

Function name is an identifier.


Must be declared before referenced.
A function prototype is a declaration, it tells the C compiler:
▶ Data type of the function: the return value.
▶ the name of the function
▶ information about the number and types of arguments that the
function expects.
Does not specify function operations.
Should be terminated by a semicolon.

(DSA Ashish Kumar Gupta) Unit-I February 6, 2023 14 / 26


Function Prototype

(DSA Ashish Kumar Gupta) Unit-I February 6, 2023 15 / 26


Function definition

Specifies function operations.


Never terminate the first line (header of the function) by a
semicolon.
Each function body may contain declarations for its own variables.
These variables are considered local to the function; in other words,
they can be referenced only within the function

(DSA Ashish Kumar Gupta) Unit-I February 6, 2023 16 / 26


Placement of Functions in a Program

The subprogram prototypes precede the main function (after any in-
clude or define directives) and the subprogram definitions follow the
main function.

The relative order of the function definitions does not affect their order
of execution

That is determined by the order of execution of the function call state-


ments.

(DSA Ashish Kumar Gupta) Unit-I February 6, 2023 17 / 26


Order of Execution of Function Subprograms and Main
Function

Compiler processes the function prototypes before other C constructs.


Each prototype enables the compiler to correctly translate a call to that
function.
Compiler: translates a function call statement as a transfer of control
to the function.
After compiling the main function, the compiler translates each func-
tion subprogram.
During translation, when the compiler reaches the end of a function
body, it inserts a machine language statement that causes a transfer
of control back from the function to the calling statement.

(DSA Ashish Kumar Gupta) Unit-I February 6, 2023 18 / 26


Advantages of Using Function Subprograms

Individual user: helps to organize the solution to a programming


problem in a better way.
Team: work division; each programmer can be assigned specific
responsibilities.
Procedural Abstraction: Function subprograms
▶ allow removal of the codes that provides the detailed solution to a
subproblem from the main function.
▶ the main function coded as a sequence of function call statements
▶ defer implementation details
Reuse of Function Subprograms
▶ Functions can be executed more than once in a program.
▶ Reduces the main function’s length and the chance of error.
▶ once a function is written and tested, it can be in other programs or
functions.

(DSA Ashish Kumar Gupta) Unit-I February 6, 2023 19 / 26


Functions with Input Arguments
Developers use functions like building blocks to construct large programs.
Functions without arguments: display information on the screen
Arguments to a function
▶ Carry information into a function subprogram (Input arguments)

▶ Return one or more results computed by the function (Output args)


▶ make it more versatile; enable manipulating different data each time it
is called.
grass area = find area(yard side) - find area(house side);
Area of the house

Area of the yard


(DSA Ashish Kumar Gupta) Unit-I February 6, 2023 20 / 26
void Functions with Input Arguments

void function does not return a value.


void function with an argument: to display its argument value in a
more attractive way.

Actual argument is an expression used inside the parentheses of a


function call.
Formal argument is an identifier that represents a corresponding
actual argument in a function definition.

(DSA Ashish Kumar Gupta) Unit-I February 6, 2023 21 / 26


Functions with Input Arguments and a Single Result

Call or reference these functions in expressions just like the library


functions

(DSA Ashish Kumar Gupta) Unit-I February 6, 2023 22 / 26


Area and Circumference of a Circle

(DSA Ashish Kumar Gupta) Unit-I February 6, 2023 23 / 26


Functions with Multiple Arguments
Function scale : multiplies its first argument (a real number) by 10 raised
to the power indicated by its second argument (an integer).

Example
The function call scale(3.7, 2) returns 370.0 (3.7 × 102 )
The function call scale(3.7, −2) returns 0.037 (3.7 × 10−2 ) Exe-
cutable stmt in function scale : scale factor = pow (10, n);

(DSA Ashish Kumar Gupta) Unit-I February 6, 2023 24 / 26


Argument List Correspondence

Constraints on number, order, and type (not) of input arguments:


# of actual arguments used in a call to a function = # of formal
parameters listed in the function prototype.
The order of arguments in the lists determines correspondence.
Each actual argument must be of a data type that can be assigned to
the corresponding formal parameter with no unexpected loss of
information.
(DSA Ashish Kumar Gupta) Unit-I February 6, 2023 25 / 26
The Function Data Area
A function requires storage cells for its formal parameters and local
variables.
Each time a function call is executed, an area of memory is allocated
for storage of that function’s data.
The function data area is always lost when the contol return to the
caller.
It is recreated empty (all values undefined) when the function is called
again.

(DSA Ashish Kumar Gupta) Unit-I February 6, 2023 26 / 26

You might also like