61
1 Chapter 4 (II) Functions and Structured Programming

1 Chapter 4 (II) Functions and Structured Programming

  • View
    222

  • Download
    0

Embed Size (px)

Citation preview

1

Chapter 4 (II)

Functions and Structured Programming

2

Review of Class on Oct 12

3

Outline of Chapter 4

How to write a function? Function Invocation Function Definition The return Statement Function Prototypes

More about function Program Correctness: The assert() Macro Function Declarations from the Compiler’s Viewpoint Invocation and Call-by-Value Developing a Large Problem

4

How to write a function?

#include <stdio.h>void prn_message(void);void prn_message(void);int main(void){ prn_message();prn_message(); printf(“Back to main function\n”); return 0;}void prn_message(void)void prn_message(void){{ printf(“A message for you: “);printf(“A message for you: “); printf(“Have a nice day!\n”);printf(“Have a nice day!\n”); return;return;}}

How to give the compiler information about the function?

Function prototype:How to pass control to the function?

Function InvocationHow to specify the function?

Function DefinitionHow to get the control back?

return statement

5

Function Prototype

Functions should be declared before they are used. Function prototype: tells the compiler

The number and type of arguments that are to be passed to the function

The type of the value that is to be returned by the function

main function is specialUsually, the programmer does not supply

a function prototype for main().

6

Function Prototypes

The general form of a function prototype type function_name(parameter type

list);Example: int fun (char a, char b);

the type of the value returned by fun: intparameter type list: char a, char b

Parameter type listA comma-separated list of typesIdentifiers are optional

Examples:void f(char c, int i); is equivalent to void f(char, int);

7

Function Prototypes

Why function prototypes: All the compilers need to check the types of

values passed to a function. Values are coerced, where necessary.

8

Function Prototypes

#include <stdio.h>int fun(int a);int main(void){ double a = 17.2; printf("fun(a)=%d\n", fun(a));

}int fun(int a){ return (a/3*3);} fun(a)=15

#include <stdio.h>int fun(double a);int main(void){ double a = 17.2; printf("fun(a)=%d\n", fun(a));

}int fun(double a){ return (a/3*3);} fun(a)=17

9

How to write a function?

#include <stdio.h>void prn_message(void);void prn_message(void);int main(void){ prn_message();prn_message(); printf(“Back to main function\n”); return 0;}void prn_message(void)void prn_message(void){{ printf(“A message for you: “);printf(“A message for you: “); printf(“Have a nice day!\n”);printf(“Have a nice day!\n”); return;return;}}

How to give the compiler information about the function?

Function prototype:How to pass control to the function?

Function InvocationHow to specify the function?

Function DefinitionHow to get the control back?

return statement

10

Function Invocation

Function Invocation: A function is called in the following format:

a function name followed by appropriate arguments enclosed by parentheses

Examples:o fun_name();o fun_name(argument1, argument2);

A function can be called in another function.

11

Function Invocation

How control is passed among functions?execution begins with main()When program control encounters a function name followed by parentheses, that is, a function is called,

control passes to the functionAfter the function does its work,

control is passed back to the calling environmentprogram execution continues.

A programis made up of one or more functions.One of them is function main()

12

#include <stdio.h>int min2(int a, int b);int min3(int a, int b, int c);int main(void){ printf("min of 11,23,24 is %d\n", min3(11,23,24)); return 0;}int min2(int a, int b){ if (a<b) return a; else return b;}int min3(int a, int b, int c){ int mofab = min2(a,b); return min2(mofab, c);}

13

How to write a function?

#include <stdio.h>void prn_message(void);void prn_message(void);int main(void){ prn_message();prn_message(); printf(“Back to main function\n”); return 0;}void prn_message(void)void prn_message(void){{ printf(“A message for you: “);printf(“A message for you: “); printf(“Have a nice day!\n”);printf(“Have a nice day!\n”); return;return;}}

How to give the compiler information about the function?

Function prototype:How to pass control to the function?

Function InvocationHow to specify the function?

Function DefinitionHow to get the control back?

return statement

14

Function Definition

Header: Type: the type of the value returned by the function. parameter type list:

the number and types of the arguments that are passed into the function when it is called.

parameters, called formal parameters:o Identifiers, can be used within the function body

Upon invocation, the value of the argument corresponding to a formal parameter is used within the function body.

void: no arguments or no return value

Body: specifies the behavior of a function.

type function_name (parameter type list){

declarations statements}

headerbod

y

15

Function Definition — Example#include <stdio.h>void prn_message(int no_of_messages);int main(void){ int how_many; printf(“How many times do you want to see the message?”); scanf(“%d”, &how_many); prn_message(how_many); return 0;}void prn_message(int no_of_messages){ int i; for (i = 0; i < no_of_messages; ++i) printf(“ Have a nice day!\n);}

16

#include <stdio.h>void prn_message(int);int main(void){ int how_many; printf(“How many times?”); scanf(“%d”, &how_many); prn_message(how_many); return 0;}void prn_message(int no_of_msges){ int i; for (i = 0; i < no_of_msges; ++i) printf(“ Have a nice day!\n);}

How to write a function?

How to give the compiler information about the function?

Function prototype:How to pass control to the function?

Function InvocationHow to specify the function?

Function DefinitionHow to get the control back?

return statement

What is the difference between function prototype and the header of a function definition?

17

How to write a function?

What is the difference between function prototype and the header of a function definition? There are two different things. Formats are different

SemicolonFunction prototype: parameter identifiers

in parameter type list is optional.

18

How to write a function?

#include <stdio.h>void prn_message(void);void prn_message(void);int main(void){ prn_message();prn_message(); printf(“Back to main function\n”); return 0;}void prn_message(void)void prn_message(void){{ printf(“A message for you: “);printf(“A message for you: “); printf(“Have a nice day!\n”);printf(“Have a nice day!\n”); return;return;}}

How to give the compiler information about the function?

Function prototype:How to pass control to the function?

Function InvocationHow to specify the function?

Function DefinitionHow to get the control back?

return statement

19

The return Statement

When control is passed back to the calling environment? When a return statement is executed, control

is passed back If no return statement, control is passed back

when the closing brace is encountered.Two formats

return; return exp;

the value of exp is returned.This value is converted, if necessary, to the

type of the function.

What do we mean by“the value is returned”?

20

#include <stdio.h>int min2(int a, int b);int min3(int a, int b, int c);int main(void){ printf("min of 11,23,24 is %d\n", min3(11,23,24)); return 0;}int min2(int a, int b){ if (a<b) return a; else return b;}int min3(int a, int b, int c){ int mofab = min2(a,b); return min2(mofab, c);}

What do we mean by“the value is returned”?

21

How to write a function?

#include <stdio.h>void prn_message(void);void prn_message(void);int main(void){ prn_message();prn_message(); printf(“Back to main function\n”); return 0;}void prn_message(void)void prn_message(void){{ printf(“A message for you: “);printf(“A message for you: “); printf(“Have a nice day!\n”);printf(“Have a nice day!\n”); return;return;}}

How to give the compiler information about the function?

Function prototype:How to pass control to the function?

Function InvocationHow to specify the function?

Function DefinitionHow to get the control back?

return statement

22

How to write a function?Preprocessing directivesfunction prototype of fucntion 1function prototype of fucntion 1int main(void){ Body of function definition}Header of function1 definition{

Body of function definition}Header of function1 definition{

Body of function definition}

function invocations

23

How to write a function?

Question: write a code to computer the minimum value of three integers. Define a function min2 which compute the

minimum value of two integers. Define a function min3 which calls function

min2 to compute the minimum value of three integers.

24

#include <stdio.h> Preprocessing directivesFunction prototype of min2Function prototype of min3int main(void){ ……. …… Call function min3 ……

}Header of min2 definition{ Body of function definition}Header of min3 definition{

Body of function definition

(Call function min2)}

int min2(int a, int b);int min3(int a, int b, int c);int main(void){ printf("min of 11,23,24 is %d\n", min3(11,23,24)); return 0;}int min2(int a, int b){ if (a<b) return a; else return b;}int min3(int a, int b, int c){ int mofab = min2(a,b); return min2(mofab, c);}

25

End of Review

26

Outline of Chapter 4

How to write a function? Function Invocation Function Definition The return Statement Function Prototypes

More about function Program Correctness: The assert() Macro Function Declarations from the Compiler’s Viewpoint Invocation and Call-by-Value Developing a Large Problem

27

Program Correctness: The assert() Macro

Example: Consider a function f(int a, int b) We want to be sure that

Argument a > 0 Argument b >=7 and b <=11 The value returned > 3

if one of these conditions is not true, abort the program.

How to guarantee these conditions? C provides the assert() macro in assert.h assert(cond), eg. assert(a>0)

o ensure that the value of cond is trueo if cond is false, the system prints out a message

and abort the program.

28

Program Correctness: The assert() Macro

Argument a > 0Argument b >=7 and b <=11 The value returned > 3

#include <assert.h>double f(int a, int b);main(){

f(1,2);}double f(int a, int b){

double x;assert(a>0);assert(b>=7 && b<=11);……assert(x>3);return x;

}

What if the condition is not true?

assert(cond)ensure that the value of cond is trueif cond is false, the system prints out a message and abort the program.

29

Program Correctness: The assert() Macro

Argument a > 0Argument b >=7 and b <=11 The value returned > 3

#include <assert.h>double f(int a, int b);main(){

f(-1,2);}double f(int a, int b){

double x;assert(a>0);assert(b>=0 && b<=11);X =5.0;assert(x>=3.0);return x;

}

% gcc assert1.c% a.outAssertion failed: a>0, file assert1.c, line 11Abort%

30

Program Correctness: The assert() Macro

Summary: C provides the assert() macro in assert.h to

ensure that the value of an expression satisfies certain conditions.

Example, if we want to ensure the value of

expression exp is true, assert(exp);

o ensure that the value of exp is trueo if exp is false, the system prints out a

message and abort the program.

31

Outline of Chapter 4

How to write a function? Function Invocation Function Definition The return Statement Function Prototypes

More about function Program Correctness: The assert() Macro Function Declarations from the Compiler’s Viewpoint Invocation and Call-by-Value Developing a Large Problem

32

Function Declarations from the Compiler’s Viewpoint

Compiler Requires the following information of a

function:the number and type of arguments that are

to be passed andThe type of the value that is to be returned

checks the values passed to functions,the values are coerced wherever necessary.

How to provide the required information to the compiler? Function Declarations

33

Function Declarations from the Compiler’s Viewpoint

Function prototype: tells the compiler

number and type of argumentstype of the value returned

#include <stdio.h>int min2(int a, int b);int min3(int a, int b, int c);int main(void){ printf("min of 11,23,24 is %d\n", min3(11,23,24) ); return 0;}int min2(int a, int b){ if (a<b) return a; else return b;}int min3(int a, int b, int c){ int mofab = min2(a,b); return min2(mofab, c);}

34

Function Declarations from the Compiler’s Viewpoint

Function Definition: The header

number and type of argumentstype of the value returned

#include <stdio.h>int min2(int a, int b);int min3(int a, int b, int c);int main(void){ printf("min of 11,23,24 is %d\n", min3(11,23,24) ); return 0;}int min2(int a, int b){ if (a<b) return a; else return b;}int min3(int a, int b, int c){ int mofab = min2(a,b); return min2(mofab, c);}

Function defintion can serveas function prototypes.

35

Function Declarations from the Compiler’s Viewpoint

#include <stdio.h>int min2(int a, int b);int min3(int a, int b, int c);int main(void){ printf("min of 11,23,24 is %d\n", min3(11,23,24) ); return 0;}int min2(int a, int b){ if (a<b) return a; else return b;}int min3(int a, int b, int c){ int mofab = min2(a,b); return min2(mofab, c);}

#include <stdio.h>int min2(int a, int b){ if (a<b) return a; else return b;}int min3(int a, int b, int c);int main(void){ printf("min of 11,23,24 is %d\n", min3(11,23,24) ); return 0;}int min3(int a, int b, int c){ int mofab = min2(a,b); return min2(mofab, c);}

36

Function Declarations from the Compiler’s Viewpoint

#include <stdio.h>int min2(int a, int b);int min3(int a, int b, int c);int main(void){ printf("min of 11,23,24 is %d\n", min3(11,23,24) ); return 0;}int min2(int a, int b){ if (a<b) return a; else return b;}int min3(int a, int b, int c){ int mofab = min2(a,b); return min2(mofab, c);}

#include <stdio.h>int min2(int a, int b){ if (a<b) return a; else return b;}int min3(int a, int b, int c){ int mofab = min2(a,b); return min2(mofab, c);}int main(void){ printf("min of 11,23,24 is %d\n", min3(11,23,24) ); return 0;}

37

Function Declarations from the Compiler’s Viewpoint

Preprocessing directivesfunction prototype of fucntion 1function prototype of fucntion 1int main(void){ Body of function definition}Header of function1 definition{

Body of function definition

}Header of function1 definition{

Body of function definition

}

Preprocessing directivesHeader of function1 definition{

Body of function definition

}Header of function1 definition{

Body of function definition

}int main(void){ Body of function definition}

38

Function Declarations from the Compiler’s Viewpoint

What if function is not declared before it is called?

If a function call, say f(x), is encountered before any declaration, definition, or prototype, the compiler assumes a default declaration.

Default function declaration int f();

o Return value is of type into Nothing is assumed about the parameter list.

The compiler has no information about parameter list.

It is the programmer’s responsibility to pass correct arguments to the function.

39

Function Declarations from the Compiler’s Viewpoint

It is not recommended to call a function before any declaration,

definition, or prototype.

A good programming style is to give either the function definition or the function

prototype or both before a function is used.

40

Function Declarations from the Compiler’s Viewpoint

Preprocessing directivesfunction prototype of fucntion 1function prototype of fucntion 1int main(void){ Body of function definition}Header of function1 definition{

Body of function definition}Header of function1 definition{

Body of function definition}

Preprocessing directivesHeader of function1 definition{

Body of function definition}Header of function1 definition{

Body of function definition}int main(void){ Body of function definition}

Summary: give either the function definition or the function prototype or both before a function is used

41

Outline of Chapter 4

How to write a function? Function Invocation Function Definition The return Statement Function Prototypes

More about function Program Correctness: The assert() Macro Function Declarations from the Compiler’s Viewpoint Invocation and Call-by-Value Developing a Large Problem

42

Invocation and Call-by-Value

Function Invocation A function is called by writing

its name and an appropriate list of arguments within

parentheses. Example:

compute_sum(n);

43

Invocation and Call-by-Value#include <stdio.h>int compute_sum(int n);int main(void){ int n=3, sum; printf("main(): n=%d\n",n); sum = compute_sum(n); printf("man(): n=%d, sum==%d\n",n,sum);}int compute_sum(int n){ int sum = 0; for (; n>0; --n) sum += n; printf("compute_sum():n=%d\n", n); return sum;}

n is passed to compute_sum()and the value of n in the body of that function is changed,

Will the value of n in the main() be changed?

No

44

Invocation and Call-by-Value

Function Invocationfun_name(exp1, exp2);

All arguments are passed call-by-valueEach argument is evaluated, and its

value is used locally in place of the corresponding formal parameter.

45

Invocation and Call-by-Value

#include <stdio.h>int max(int a, int b);int main(void){ int j=1, k=2, m; m = max(2*j, j+k); printf(“\n The maximum is %d.”, m); return 0;}

int max(int a, int b){ if (a>b) return a; else return b;}

All arguments are passed call-by-value Each argument is evaluated, and its

value is used locally in place of the corresponding formal parameter.

46

Invocation and Call-by-Value

Function Invocationfun_name(exp1, exp2);

All arguments are passed call-by-valueEach argument is evaluated, and its

value is used locally in place of the corresponding formal parameter.

If a variable is passed to a function, the stored value of that variable in the calling environment is not changed.

47

Invocation and Call-by-Value#include <stdio.h>int compute_sum(int n);int main(void){ int n=3, sum; printf("main(): n=%d\n",n); sum = compute_sum(n); printf("man(): n=%d, sum==%d\n",n,sum);}int compute_sum(int n){ int sum = 0; for (; n>0; --n) sum += n; printf("compute_sum():n=%d\n", n); return sum;}

% a.outmain(): n=3compute_sum():n=0man(): n=3, sum==6%

Even though n is passed to compute_sum(), and the value of n in the body of that function is changed, the value of n in the calling environment remains unchanged.

48

Invocation and Call-by-Value

Summary Function Invocation

fun_name(exp1, exp2); All arguments are passed call-by-value

Each argument is evaluated, and its value is used locally in place of the corresponding formal parameter.

If a variable is passed to a function, the stored value of that variable in the calling environment is not changed.

49

Outline of Chapter 4

How to write a function? Function Invocation Function Definition The return Statement Function Prototypes

More about function Program Correctness: The assert() Macro Function Declarations from the Compiler’s Viewpoint Invocation and Call-by-Value Developing a Large Problem

50

Developing a Large Problem

Example: A single .c file,

containingFunction

prototypes,main functionFunction definitions

#include <stdio.h>int min2(int a, int b);int min3(int a, int b, int c);int main(void){ printf("min of 11,23,24 is %d\n", min3(11,23,24) ); return 0;}int min2(int a, int b){ if (a<b) return a; else return b;}int min3(int a, int b, int c){ int mofab = min2(a,b); return min2(mofab, c);}

Large Program

A single .c file containing one million lines?

51

Developing a Large Problem

Typically, a large program is written in A separate directory as a collection of files

Questions:How to divide a program into separated files?How the files are integrated together?

52

Developing a Large Problem

How to divide a large program into separated files: .h and .c files,

Usually .h file contains function prototypes

each .c file containing one or more function definitions.

53

#include <stdio.h>int min2(int a, int b);int min3(int a, int b, int c);

int main(void){ printf("%d\n", min3(1,3,4)); return 0;}int min2(int a, int b){ if (a<b) return a; else return b;}int min3(int a, int b, int c){ int mofab = min2(a,b); return min2(mofab, c);}

#include <stdio.h>int min2(int a, int b);int min3(int a, int b, int c);

min.h

int main(void){ printf("%d\n", min3(1,3,4) ); return 0;}int min2(int a, int b){ if (a<b) return a; else return b;}int min3(int a, int b, int c){ int mofab = min2(a,b); return min2(mofab, c);}

main.c

How to integrate these two separated files?Functions should be declared before they are called. But How?

#include “min.h”#include “f.h”

The preprocessor looks in the current directory for the file f.h. If it is not found in the current directory, it looks in system-dependent pl,aces for the file.

#include <f.h>it looks in system-dependent places for the file.

#include <stdio.h>int min2(int a, int b);int min3(int a, int b, int c);

54

#include <stdio.h>int min2(int a, int b);int min3(int a, int b, int c);

min.h

#include “min.h”int main(void){ printf("%d\n", min3(1,3,4) ); return 0;}

main.c

int min2(int a, int b){ if (a<b) return a; else return b;}int min3(int a, int b, int c){ int mofab = min2(a,b); return min2(mofab, c);}

min.c

How to integrate these two separated .c files?

gcc -o min main.c min.c

55

#include <stdio.h>int min2(int a, int b);int min3(int a, int b, int c);

min.h

#include “min.h”int main(void){ printf("%d\n", min3(1,3,4) ); return 0;}

main.c

int min2(int a, int b){ if (a<b) return a; else return b;}int min3(int a, int b, int c){ int mofab = min2(a,b); return min2(mofab, c);}

min.c

Summary:

f.h: function prototypesf1.c, f2.c, f3.c

function definitions

in each .c file#include “f.h”

gcc f1.c f2.c f3.c

gcc main.c min.c

56

Outline of Chapter 4

How to write a function? Function Invocation Function Definition The return Statement Function Prototypes

More about function Program Correctness: The assert() Macro Function Declarations from the Compiler’s Viewpoint Invocation and Call-by-Value Developing a Large Problem

57

Program Correctness: The assert() Macro

C provides the assert() macro in assert.h to ensure that the value of an expression satisfies certain conditions.

Example, if we want to ensure the value of expression

exp is true, assert(exp);

ensure that the value of exp is trueif exp is false, the system prints out a

message and abort the program.

58

Function Declarations from the Compiler’s Viewpoint

Preprocessing directivesfunction prototype of fucntion 1function prototype of fucntion 1int main(void){ Body of function definition}Header of function1 definition{

Body of function definition}Header of function1 definition{

Body of function definition}

Preprocessing directivesHeader of function1 definition{

Body of function definition}Header of function1 definition{

Body of function definition}int main(void){ Body of function definition}

Give either the function definition or the function prototype or both before a function is used

59

Invocation and Call-by-Value

Function Invocation

fun_name(exp1, exp2); All arguments are passed call-by-value

Each argument is evaluated, and its value is used locally in place of the corresponding formal parameter.

If a variable is passed to a function, the stored value of that variable in the calling environment is not changed.

60

#include <stdio.h>int min2(int a, int b);int min3(int a, int b, int c);

min.h

#include “min.h”int main(void){ printf("%d\n", min3(1,3,4) ); return 0;}

main.c

int min2(int a, int b){ if (a<b) return a; else return b;}int min3(int a, int b, int c){ int mofab = min2(a,b); return min2(mofab, c);}

min.c

f.h: function prototypesf1.c, f2.c, f3.c

function definitions

in each .c file#include “f.h”

gcc f1.c f2.c f3.c

gcc main.c min.c

Developing a Large Problem

61

End of Chapter 4: FunctionRead 4.1- 4.12