Top-Down Design with Functions (Chapter 3)

 

Functions

 

- In general, functions can be represented visually as:

 

 

 

 

 

 


which can be interpreted as:

Output = F(Input)

 

- In mathematics, Input and Output are often thought of as numerical variables

- In C, Input and Output can consist of variables (called arguments) of any permissible data type (such as int, double, char)

- Input and output can contain

- zero variables

- one variable

- more than one variable

- Examples of function that has empty input and output:

- do a predetermined set of actions (such as print a message, or plot a figure to the screen)

- get all needed information from a user and report the result back to the user directly by printing it to the screen)

- Chapter 3 covers all types of functions, except those that have more than one variable at the output (to be covered in Chapter 6)

 

Library Functions

 

- C provides many predefined functions in libraries such as stdio.h, math.h, stdlib.h, time.h (look at Appendix B)

- examples of library functions: printf, scanf, sqrt

            Function call:

y = sqrt(x);    /* x is argument, sqrt is function name */

- other examples from math.h: cos(x), floor(x), log10(x), pow(x,y)

 

Case Study: Drawing Diagrams

 

- read Section 3.3 in the textbook

 

Example:

The task is to draw the figure of Mary (from Lab03) using printf. The task can be decomposed into several subproblems. Solution of each subproblem can be used as a component in similar problems (e.g. draw the figure of Beldar from Lab03)

 

    *

  *   *

 *     *             draw_circle

  *   *

    *

    *

   * *

  *   *                    draw_triangle” (consists of “draw_intersecting_lines

 *     *                   and “draw_base”)

       *********                 

    *

   * *               draw_intersecting_lines

  *   *

 *     *  

 

Solution that does not use functions:

 

/* This is a program that displays figure of Mary using functions */

 

# include<stdio.h>

 

int main(void)

{

/* A sequence of steps that plots figure of Mary */

printf(    *    \n”);

printf(  *   *  \n”);

printf(“ *     * \n”);

printf(  *   *  \n”);

printf(    *    \n”);

 

printf(    *     \n”);

printf(   * *   \n”);

printf(  *   *  \n”);

printf(“ *     * \n”);

printf(“********* \n”);

 

printf(    *     \n”);

printf(   * *   \n”);

printf(  *   *  \n”);

printf(“ *     * \n”);

}

 

 

Solution that uses functions without arguments:

 

/* This is a program that displays figure of Mary using functions */

 

# include<stdio.h>

 

/*The function prototypes */

void plot_circle(void);

/* you write the remaining 3 prototypes... */

 

int main(void)

{

/* A sequence of steps that plots figure of Mary */

plot_circle();

plot_triangle();

plot_legs();

}

 

/* plot_circle function definition */

void plot_circle(void)

{

printf(    * \n”);

printf(  *   * \n”);

printf(“ *     * \n”);

printf(  *   * \n”);

printf(    * \n”);

}

 

/* plot_legs function definition */

void plot_legs(void)

{

/* you do it... */

 

}

 

/* plot_base function definition */

void plot_legs(void)

{

/* you do it... */

 

}

 

/* plot_triangle function definition */

void plot_triangle(void)

{

plot_legs();

plot_base();

}

 

Functions without Arguments

 

- They do not require any value to be passed during the function call

- Examples (also see Figure 3.14):

draw_circle(), draw_intersecting_lines(), draw_base(), draw_triangle()

- There are 3 basic steps that need to performed to write and use a function:

- write “function prototype

- write “function definition

- use function in the program

- Function prototype:

- must be declared before it can be referenced in the program

- it should be inserted before the main function

- general form:

ftype fname(void)

- example:

void draw_circle(void);

- Function definition:

- similar form as the main function

- must declare its own variables (variables from main are invisible, unless they are passed as input arguments)

- function definition should be written after the main function

- general form:

ftype fname(void)

{

     local declaration

     executable statements

}

- examples: see functions declared in Figure 3.14

- Function call

- general form (shows how function is called in the program):

fname();

- example:

draw_circle();

- Order of execution

- when function is called the control of the program goes to function execution; the control is transferred back to the main after the last statement is executed

- how the memory is allocated: when the function is called, the values of all variables from main function are frozen, additional space is allocated to store variables declared by the function; when control goes back to the main function, the memory occupied by the local variables is released

- Advantages of functions

- procedural abstraction (decompose a big problem into subproblems and solve each subproblem separately)

- reuse of functions

 

 

Functions with Input Arguments

 

- They require values to be passed as input arguments during the function call

- Example: Compute the area of a circle with diameter d

Solution 1:

/*

 * Computes the area of a circle with diameter d.

 * Pre:  d is defined and is > 0.

 *       PI is a constant macro.

 */

double

find_area(double d)

{

       double r;    /* radius r */

double area; /* area is the output argument */

 

r = d/2;

area = PI * r * r;

return(area);

}

 

Solution 2:

/*

 * Computes the area of a circle with diameter d.

 * A fancy version of the previous function

 * Pre:  d is defined and is > 0.

 *       PI is a constant macro.

 *       Library math.h is included.

 */

double

find_area(double d)

{

return (PI * pow(r, 2));

}

 

- Function output:

- function does not provide output - e.g.

draw_circle();

- function provides one output – uses the return statement

- function provides more than one output (explained in Chapter 6)

- Function input:

            - function does not need anything as input – e.g.

draw_circle();

a = random_number();

            - function needs one or more inputs – e.g.

a = find_area(d);

print_sum(a,b);

- There are 3 basic steps that need to be performed to write and use any function in C:

- write “function prototype

- write “function definition

- use function in the program

- Function prototype:

- general form:

ftype fname(argument declaration)

- examples:

void print_number(double number);

double find_area(double d);

double calculate_sum(double a, double b);

-Function definition:

- general form:

ftype fname(argument declaration)

{

     local declaration

     executable statements

}

- examples: see functions declared in Figure 3.21

- Function call

- general form (shows how function is called in the program):

fname(variables); /* no output arguments */

variable = fname(variables); /* with output argument */

- example:

a = find_area(b);

- Programming style: Precondition

- example:

/* Pre:  d is defined and is > 0.

 *       PI is a constant macro.

 *       Library math.h is included.

 */

- describes what conditions need to be satisfied before the function can be called

- in the example above, we require that d is positive number, that PI is defined as constant macro using #define at the top of the program, and that math.h is included using #include at the top of the program

- Argument list correspondence

- number: number of argument used in the function call should equal the number of arguments listed in the function prototype

- order: order of arguments in the function call should respect the order of arguments in function definition

- type: only arguments of compatible type to function definition should be used in the function call