Download presentation
Presentation is loading. Please wait.
1
Top-Down Design with Functions 4 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
2
Building Programs from Existing Information 4 Problem analysis precedes requirements analysis 4 Requirements analysis precedes solution design 4 Solution design precedes programming 4 This sequence of steps (and successive steps) or phases is known as the software lifecycle
3
Building Programs from Existing Information 4 An algorithm can be developed following a methodology called stepwise refinement 4 In this methodology, an initial high-level solution is sketched, and each step in the algorithm is refined in a series of steps 4 We go from the human-oriented initial solution to the machine-oriented final program
4
Program Design Steps 4 Problem Statement 4 Problem Analysis 4 Data Requirements –Inputs –Outputs –Constants 4 Program Design –Initial Design Pseudocode
5
Program Design Steps –Algorithm refinement 4 Implementation 4 Testing –Test cases 4 Self-Check 3.1, 3.3
6
Library Functions 4 There are two major reasons to use functions –Code reuse –Divide and conquer approach to program design 4 C provides numerous functions to provide useful tasks –Similar functions are grouped into a library
7
Library Functions 4 We have already seen the standard i/o library –We can use the functions in this library by including the line “#include ” at the beginning of our program –The file 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
8
Library Functions 4 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 4 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
9
Mathematical Functions 4 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: a 2 = b 2 + c 2 -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));
10
User-Defined Functions 4 Besides the standard library functions (and main) C allows the programmer to define his own functions 4 The essential parts of the declaration of a function are: –Function name –Number of arguments and argument types –Return type
11
Top-Down Design and Structure Charts 4 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
12
Functions Without Arguments 4 The simplest functions are those which have no arguments and which return no value 4 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();
13
Functions Without Arguments 4 Just as we needed to declare variables prior to using them, we must also declare functions prior to use 4 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
14
Functions Without Arguments Example function prototype for a simple function: void do_input(void); 4 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
15
Functions Without Arguments 4 After giving the function prototype, we must also have the function itself (function header and function body) 4 The function is placed after the function prototypes and before or after the main function (but not inside the main function!) 4 The function prototypes and preprocessor directives may also be place in a header file (when would you do this?)
16
Functions Without Arguments 4 If a function gets too big (say, > 200 lines of code) it should be split into multiple functions to make it easier to read 4 If a file gets too (say, > 1000 lines of code) it should be split into multiple files to make it easier to read 4 Functions should be commented to give their intended use
17
Flow of Control 4 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)
18
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)
19
Functions with Arguments 4 Functions with arguments are much more powerful than those without since they can calculate a different value for each different argument 4 Arguments carry information into the function or allow multiple results to be returned by the function 4 Parameters can be classified as input or output arguments
20
Functions with Arguments We can also have functions which have input parameters but a void return type –For example: print_sum(x,y); 4 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);
21
Functions which return a value 4 These functions pass the value they compute back to the calling program using the return statement 4 A function to find the circumference of a circle might have a single statement - return: return(2.0*PI*r); /* r - argument */ 4 The arguments of a function are given names in the implementation of the function
22
Functions which return a value double compute_sum(int first, int second) 4 We may then use the arguments of the function just as we would variables of the same types (only within the function though) 4 The values of the arguments are lost when we leave the function
23
Functions which return a value 4 Imagine we have a function called zero_out with an argument z: –int zero_out(int z); 4 Now in our main function we have an int variable x which we set to 3: –X = 3; 4 Call the function zero_out as follows: –y = zero_out(x);
24
Functions which return a value 4 In the body of zero_out we do the following: –z = 0; return(z); 4 What are the values of x and y? 4 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
25
Functions 4 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 4 Each time the function is called, the computer reserves space for the functions local variables and arguments (on the stack) 4 This space is freed when the function terminates
26
Testing Functions using Drivers 4 In top-down design, a programs functions may be tested separately from the programs which use them 4 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 4 This calling function is called the driver function
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.