Top Banner
Top-Down Design with Functions What do programmer’s (not programs!) use as input to the design of a program? Documentation Problem definition Requirements analysis Design document (algorithm) Previously written code Library functions
26

Top-Down Design with Functions

Jan 07, 2016

Download

Documents

Kirk

Top-Down Design with Functions. What do programmer’s (not programs!) use as input to the design of a program? Documentation Problem definition Requirements analysis Design document (algorithm) Previously written code Library functions. Building Programs from Existing Information. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Top-Down Design with Functions

Top-Down Design with Functions What do programmer’s (not programs!) use

as input to the design of a program?– Documentation

• Problem definition

• Requirements analysis

• Design document (algorithm)

– Previously written code• Library functions

Page 2: Top-Down Design with Functions

Building Programs from Existing Information Problem analysis precedes requirements

analysis Requirements analysis precedes solution

design Solution design precedes programming This sequence of steps (and successive steps)

or phases is known as the software lifecycle

Page 3: Top-Down Design with Functions

Building Programs from Existing Information An algorithm can be developed following a

methodology called stepwise refinement In this methodology, an initial high-level

solution is sketched, and each step in the algorithm is refined in a series of steps

We go from the human-oriented initial solution to the machine-oriented final program

Page 4: Top-Down Design with Functions

Program Design Steps

Problem Statement Problem Analysis Data Requirements

– Inputs– Outputs– Constants

Program Design– Initial Design

• Pseudocode

Page 5: Top-Down Design with Functions

Program Design Steps

– Algorithm refinement

Implementation Testing

– Test cases

Self-Check 3.1, 3.3

Page 6: Top-Down Design with Functions

Library Functions

There are two major reasons to use functions– Code reuse– Divide and conquer approach to program

design

C provides numerous functions to provide useful tasks– Similar functions are grouped into a library

Page 7: Top-Down Design with Functions

Library Functions

We have already seen the standard i/o library– We can use the functions in this library by

including the line “#include <stdio.h>” at the beginning of our program

– The file <stdio.h> is an example of a header file• It gives the definitions of all of the functions and

constants in the library so that they can be used by the compiler to check the program

Page 8: Top-Down Design with Functions

Library Functions

Another important standard library (what is a standard library and how does it differ from a non-standard library?) is the standard math library - #include <math.h>

Among the functions in this library are• ceil(x) and floor(x)

• cos(x), sin(x) and tan(x) (all inputs given in radians)

• sqrt(x) and pow(x, y)

• exp(x), log(x) and log10(x)

• All have input, output of type double

Page 9: Top-Down Design with Functions

Mathematical Functions

Consider the following use of the math functions– Given two sides (b and c) of a triangle and the

angle between them (the length of the third side can be computed using the following formula: a2 = b2 + c2 -2bc cos

– In C, we compute a as follows:– a = sqrt(pow(b,2) + pow(c,2) -2 * b * c *

cos(alpha * PI / 180.0));

Page 10: Top-Down Design with Functions

User-Defined Functions

Besides the standard library functions (and main) C allows the programmer to define his own functions

The essential parts of the declaration of a function are:– Function name– Number of arguments and argument types – Return type

Page 11: Top-Down Design with Functions

Top-Down Design and Structure Charts As we said previously, top-down design is a

methodology for program design– We apply the divide and conquer philosophy of a

dividing a problem into subproblems– Concentrate on the subproblems one at a time– In the implementation, each subproblem may be

implemented as a function– We may document a top-down design using a

diagramming technique known as structure charts

Page 12: Top-Down Design with Functions

Functions Without Arguments

The simplest functions are those which have no arguments and which return no value

These are also the most limited type of function since they always have the same outcome

An example of a function call for such a function is: do_input();

Page 13: Top-Down Design with Functions

Functions Without Arguments

Just as we needed to declare variables prior to using them, we must also declare functions prior to use

The function is declared in a function prototype– The prototype gives the function’s name, argument

types, and return types– If there is no value returned we use the reserved word

void in place of the data value– If there are no arguments, use void

Page 14: Top-Down Design with Functions

Functions Without Arguments

Example function prototype for a simple function: void do_input(void);

Function prototypes are placed after the includes and defines and before the main function of the program

Note that the header files for the standard libraries contain function prototypes for, e.g., scanf

Page 15: Top-Down Design with Functions

Functions Without Arguments

After giving the function prototype, we must also have the function itself (function header and function body)

The function is placed after the function prototypes and before or after the main function (but not inside the main function!)

The function prototypes and preprocessor directives may also be place in a header file (when would you do this?)

Page 16: Top-Down Design with Functions

Functions Without Arguments

If a function gets too big (say, > 200 lines of code) it should be split into multiple functions to make it easier to read

If a file gets too (say, > 1000 lines of code) it should be split into multiple files to make it easier to read

Functions should be commented to give their intended use

Page 17: Top-Down Design with Functions

Flow of Control

A function call causes program execution to be transferred from the calling function to the called function

The return statement causes program execution to return to the calling function (or to the operating system in the case of the main function)

Page 18: Top-Down Design with Functions

Functions with Arguments

Arguments carry information from the calling function (or operating system in the case of the main function) into the called function

Called functions can also return values to the calling function (or operating system in the case of the main function)

Page 19: Top-Down Design with Functions

Functions with Arguments

Functions with arguments are much more powerful than those without since they can calculate a different value for each different argument

Arguments carry information into the function or allow multiple results to be returned by the function

Parameters can be classified as input or output arguments

Page 20: Top-Down Design with Functions

Functions with Arguments

We can also have functions which have input parameters but a void return type– For example: print_sum(x,y);

Another possibility is to have a function with input arguments and a single result– In this case, the function call most often

appears as part of an assignment statement– result = compute_sum(x,y);

Page 21: Top-Down Design with Functions

Functions which return a value

These functions pass the value they compute back to the calling program using the return statement

A function to find the circumference of a circle might have a single statement - return: return(2.0*PI*r); /* r - argument */

The arguments of a function are given names in the implementation of the function

Page 22: Top-Down Design with Functions

Functions which return a value

double compute_sum(int first, int second)

We may then use the arguments of the function just as we would variables of the same types (only within the function though)

The values of the arguments are lost when we leave the function

Page 23: Top-Down Design with Functions

Functions which return a value

Imagine we have a function called zero_out with an argument z: – int zero_out(int z);

Now in our main function we have an int variable x which we set to 3:– X = 3;

Call the function zero_out as follows:– y = zero_out(x);

Page 24: Top-Down Design with Functions

Functions which return a value

In the body of zero_out we do the following:– z = 0; return(z);

What are the values of x and y? If we have multiple arguments in the function,

we must have a corresponding number of arguments in the function call and the must be of the same types as in the function prototype

Page 25: Top-Down Design with Functions

Functions

If there are some variables that we need to perform the task of the function, we may declare these local variables at the start of the function body

Each time the function is called, the computer reserves space for the functions local variables and arguments (on the stack)

This space is freed when the function terminates

Page 26: Top-Down Design with Functions

Testing Functions using Drivers

In top-down design, a programs functions may be tested separately from the programs which use them

In order to do this, write a short function which calls the function you want to test with some input and then displays the result

This calling function is called the driver function