Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 4 (II) Functions and Structured Programming.

Similar presentations


Presentation on theme: "1 Chapter 4 (II) Functions and Structured Programming."— Presentation transcript:

1 1 Chapter 4 (II) Functions and Structured Programming

2 2 Review of Class on Oct 12

3 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 4 How to write a function? #include void prn_message(void); int main(void) { prn_message(); prn_message(); printf(“Back to main function\n”); return 0; } 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 Invocation  How to specify the function? Function Definition  How to get the control back? return statement

5 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 special Usually, the programmer does not supply a function prototype for main().

6 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: int parameter type list: char a, char b  Parameter type list A comma-separated list of types Identifiers are optional Examples: void f(char c, int i); is equivalent to void f(char, int);

7 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 8 Function Prototypes #include 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 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 9 How to write a function? #include void prn_message(void); int main(void) { prn_message(); prn_message(); printf(“Back to main function\n”); return 0; } 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 Invocation  How to specify the function? Function Definition  How to get the control back? return statement

10 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 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 function  After the function does its work, control is passed back to the calling environment program execution continues.  A program  is made up of one or more functions.  One of them is function main()

12 12 #include 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 13 How to write a function? #include void prn_message(void); int main(void) { prn_message(); prn_message(); printf(“Back to main function\n”); return 0; } 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 Invocation  How to specify the function? Function Definition  How to get the control back? return statement

14 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: oIdentifiers, 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 } header body

15 15 Function Definition — Example #include 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 16 #include 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 Invocation  How to specify the function? Function Definition  How to get the control back? return statement What is the difference between  function prototype and  the header of a function definition?

17 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 Semicolon Function prototype: parameter identifiers in parameter type list is optional.

18 18 How to write a function? #include void prn_message(void); int main(void) { prn_message(); prn_message(); printf(“Back to main function\n”); return 0; } 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 Invocation  How to specify the function? Function Definition  How to get the control back? return statement

19 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 20 #include 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 21 How to write a function? #include void prn_message(void); int main(void) { prn_message(); prn_message(); printf(“Back to main function\n”); return 0; } 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 Invocation  How to specify the function? Function Definition  How to get the control back? return statement

22 22 How to write a function? Preprocessing directives function prototype of fucntion 1 int 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 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 24 #include Preprocessing directives Function prototype of min2 Function prototype of min3 int 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 25 End of Review

26 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 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) oensure that the value of cond is true oif cond is false, the system prints out a message and abort the program.

28 28 Program Correctness: The assert() Macro  Argument a > 0  Argument b >=7 and b <=11  The value returned > 3 #include 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 true  if cond is false, the system prints out a message and abort the program.

29 29 Program Correctness: The assert() Macro  Argument a > 0  Argument b >=7 and b <=11  The value returned > 3 #include 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.out Assertion failed: a>0, file assert1.c, line 11 Abort %

30 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); oensure that the value of exp is true oif exp is false, the system prints out a message and abort the program.

31 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 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 and The 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 33 Function Declarations from the Compiler’s Viewpoint Function prototype: tells the compiler  number and type of arguments  type of the value returned #include 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 34 Function Declarations from the Compiler’s Viewpoint Function Definition: The header  number and type of arguments  type of the value returned #include 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 serve as function prototypes.

35 35 Function Declarations from the Compiler’s Viewpoint #include 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 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 36 Function Declarations from the Compiler’s Viewpoint #include 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 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 37 Function Declarations from the Compiler’s Viewpoint Preprocessing directives function prototype of fucntion 1 int main(void) { Body of function definition } Header of function1 definition { Body of function definition } Header of function1 definition { Body of function definition } Preprocessing directives Header of function1 definition { Body of function definition } Header of function1 definition { Body of function definition } int main(void) { Body of function definition }

38 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(); oReturn value is of type int oNothing 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 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 40 Function Declarations from the Compiler’s Viewpoint Preprocessing directives function prototype of fucntion 1 int main(void) { Body of function definition } Header of function1 definition { Body of function definition } Header of function1 definition { Body of function definition } Preprocessing directives Header 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 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 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 43 Invocation and Call-by-Value #include 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 44 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.

45 45 Invocation and Call-by-Value #include 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 46 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.

47 47 Invocation and Call-by-Value #include 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.out main(): n=3 compute_sum():n=0 man(): 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 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 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 50 Developing a Large Problem  Example:  A single.c file, containing Function prototypes, main function Function definitions #include 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 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 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 53 #include 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 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  it looks in system- dependent places for the file. #include int min2(int a, int b); int min3(int a, int b, int c);

54 54 #include 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 55 #include 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 prototypes f1.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 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 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 true if exp is false, the system prints out a message and abort the program.

58 58 Function Declarations from the Compiler’s Viewpoint Preprocessing directives function prototype of fucntion 1 int main(void) { Body of function definition } Header of function1 definition { Body of function definition } Header of function1 definition { Body of function definition } Preprocessing directives Header 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 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 60 #include 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 prototypes f1.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 61 End of Chapter 4: Function Read 4.1- 4.12


Download ppt "1 Chapter 4 (II) Functions and Structured Programming."

Similar presentations


Ads by Google