Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions #include int sum(int i, int j); main() { int a, b, res; a = 1; b = 2; res = sum(a, b); printf("%d + %d = %d\n", a, b, res); } int sum(int i,

Similar presentations


Presentation on theme: "Functions #include int sum(int i, int j); main() { int a, b, res; a = 1; b = 2; res = sum(a, b); printf("%d + %d = %d\n", a, b, res); } int sum(int i,"— Presentation transcript:

1 Functions #include int sum(int i, int j); main() { int a, b, res; a = 1; b = 2; res = sum(a, b); printf("%d + %d = %d\n", a, b, res); } int sum(int i, int j) { int k; k = i + j; return k; } Prototype declaration Function definition Function parameters Function return value Call/invoke a function Arguments

2 Function Header The sum function takes two ints and returns an int. int sum(int i, int j) {... } The function echo_line has no parameters and returns a value of type int. int echo_line(void) {... }

3 Function Header The function print_stars has one parameter vol of type int and returns no value. void print_stars(int vol) {... } The function print_prompt has no parameters and returns no value. void print_prompt(void) {... }

4 Function Prototype Declaration The function print_stars has one parameter vol of type int and returns no value. Prototype: (prototype ends with a semicolon) void print_stars(int vol); Function definition: void print_stars(int vol) { while(vol-- > 0) putchar('*'); }

5 Prototype Declaration Function prototype declaration should be made BEFORE the function is used. If the declaration is outside any function, the declaration makes it known to all the functions after the declaration in the file. If the declaration is inside a function A, the declared function B is known only to this function A.

6 Prototype Declaration Outside a function main() {... } int sum(int a, int b); void prt(int vol) { sum(... );... } float prod(float x, int y) {... } The main() function does not know sum(). It cannot used sum (correctly). The functions following the sum prototype declaration can use sum() function.

7 Prototype Declaration Inside a Function main() {... } void prt(int vol) { int sum(int a, int b); sum(... );... } float prod(float x, int y) {... } The main() function does not know sum(). It cannot used sum (correctly). Only the prt() function can use sum() function. The sum() function is also not known in prod().

8 Define Functions You can define your function anywhere in a file. Before main() or after main(), both OK. int sum(int a, int b) { … } main() {... }

9 Program Structure #include int f1(int a,...); int f2(void); int f3(...); main() {... } int f1(int a,... ) { } main() function call other functions. A function can call any other functions, including itself. Prototypes

10 Return Statement Return statement causes a termination of the current function and execution goes back to the calling function. Return; return without a value. Return exprn; return the value exprn to the calling function.

11 How the program is executed when there are functions? #include int sum(int i, int j); main() { int a, b, res; a = 1; b = 2; res = sum(a, b); printf("%d + %d = %d\n", a, b, res); } int sum(int i, int j) { int k; k = i + j; return k; } a b res a, b, res have no defined values just after declaration. a b res 12 a b res i j 1212 res 3 res is set to 3 after the function call returns. Copies are made to new memory locations for the parameter i and j in sum() function. k i j 312 Value 3 is sent back as the value of the expression sum(a,b) in calling function.

12 Example, Printing a Bar Graph Problem –Print a bar graph that shows the volume of large lakes of the world. The input data are the names of the lakes and the volumes of the lakes in hundreds of cubic miles. Solution –We first print the name of the lake simply by echoing the input to the output a character at a time. The bar graph is created by printing a number of stars equal to the volume of each lake in hundreds of cubic miles.

13 Sample Input/Output Input data Baikal 58 Superior 54 Tanganyika 45 Nyasa 38 Michigan 26 Huron 21 Output data Baikal ********************************************************** Superior ****************************************************** Tanganyika ********************************************* Nyasa ************************************** Michigan ************************** Huron ********************* Input data consists of a line of name followed by a number.

14 C implementation without the use of functions #include main() { char c; int i, vol; while( scanf(" %c", &c) != EOF) { do { printf("%c", c); scanf("%c", &c); } while( c != '\n'); printf("\n"); scanf("%d", &vol); for(i = 1; i <= vol; i++) printf("*"); printf("\n\n"); }

15 Functions and Program Design Use functions to break a big problem into small pieces. Each small problem is solved by a function. For the bar graph problem we can –read and print the lake name by echoing the input. –Read the volume of a lake. –Print the required number of stars.

16 The main() function #include int echo_line(void); void print_stars(int vol); main() { int value; while(echo_line() != EOF) { scanf("%d", &value); print_stars(value); } return EXIT_SUCCESS; }

17 Function Definitions int echo_line(void) { char c; if (scanf(" %c", &c)==EOF) return EOF; for( ; ; ) { putchar(c); if(c == '\n') return c; c = getchar(); } void print_stars(int vol) { while(vol-- > 0) putchar('*'); printf("\n\n"); }

18 Reading/Home Working Read Chapter 5, page 154 to 172. Work on Problems –Section 5.1, page 164, exercise 1, 3, 5, 7, 9. –Section 5.3, page 171, exercise 1, 3. Check your answers in the back of the textbook. Do not hand in.


Download ppt "Functions #include int sum(int i, int j); main() { int a, b, res; a = 1; b = 2; res = sum(a, b); printf("%d + %d = %d\n", a, b, res); } int sum(int i,"

Similar presentations


Ads by Google