Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Lecture 6  Functions  C Standard Libraries  Invocation  Definition  Call-by-value  Prototype  Multi-file program  Macros  Readings: Chapter.

Similar presentations


Presentation on theme: "1 Lecture 6  Functions  C Standard Libraries  Invocation  Definition  Call-by-value  Prototype  Multi-file program  Macros  Readings: Chapter."— Presentation transcript:

1

2 1 Lecture 6  Functions  C Standard Libraries  Invocation  Definition  Call-by-value  Prototype  Multi-file program  Macros  Readings: Chapter 4 Section 1 to 12

3 2 Program Components  A C program is made up of one or more functions, one of them being main() which is the starting point of program execution  Typically, some functions are defined by the programmer and the others are predefined functions provided in the C standard libraries or other libraries.

4 3 The C standard libraries  provide functions for:  Input/output, math calculation, character/string processing, error-checking …  Related library functions are grouped and can be referenced by including the corresponding header file into the source program during preprocessing  Header files for ANSI C’s standard library

5 4 Functions  Why functions?  Natural for top-down design approach  Enhance software reusability  Avoid repeating code main worker3worker2worker1 worker3worker4

6 5 Function Invocation  When a function is called (invoked), program control is passed to that function.  When that function ends, program control is returned to the statement immediately after the function call.  Syntax: function_name ( argument_list )

7 6 Calling Predefined Functions #include void main() { float area; printf(“Enter the area of a square: “); scanf(“%f”, &area); printf(“The square has perimeter: %f”, 4*sqrt(area)); }

8 7 Another example #include int main() { int i=0, n; printf(“No. of random integers you want to see? ”); scanf(“%d”, &n); while (i < n) { if (i++ % 6 == 0) /* 6 numbers (max.) per line */ printf("\n"); printf(“%9d”, rand()); /* call rand() in stdlib */ } printf(“\n”); return 0; }

9 8 Programmer-defined functions  You can define your own functions and then call them as needed.  Either the function definition or function prototype must appear before the function calls.  Function prototype describes how the function is invoked (interface)  Function definition describes how the function computes the return value (implementation)  A function definition cannot be nested within another function definition.

10 9 Function Definition  Syntax: type function_name( parameter_list ) { sequence_of_statements }  Everything before the ‘ { ’ comprises the function header; the rest constitutes the function body

11 10 Example #include int powerOfTwo(int n) { int power=1; for (; n>0; n--) power *= 2; return power; } void main() { int i, result; printf(“Enter a non-negative integer: “); scanf(“%d”, &i); result = powerofTwo(i); printf(“The %d-th power of 2 is %d\n”, i, result); return; }

12 11 Invocation & Call-by-Value  When a C function is invoked, the arguments within the parentheses are passed using a call-by- value mechanism, which means that each argument is evaluated, and its value is used locally in place of the corresponding formal parameter  Example (previous page):  Suppose user input 7 for the value of i. The argument received by function powerOfTwo is 7.  At the end, power and n are destroyed. They are called local variables (covered next time.)  Variable result will get the value 128.  Variable i is unchanged

13 12 Call-by-value (cont ’ ) power n result i 1  128 7  0 128 7 Destroyed after function invocation

14 13 The return Statement  When a return is encountered,  program control goes back to the calling function  the value of the expression after the keyword return is sent back to the calling function  The return value of a function will be converted, if necessary, to the type of function as specified in the header to the function definition  Syntax: return expression; return ;

15 14 Example of return #include int min(int a, int b) { if (a<b) return a; return b; } int main(void) { int j, k, m; printf("Input two integers: "); scanf("%d%d", &j, &k); m = min(j, k); printf("\nOf the two values %d and %d, " "the minumum is %d.\n\n", j, k, m); return 0; }

16 15 The void data type  When a function does not return any value, the type of function is void, e.g, : void err_msg(int i) { switch (i) { case 1: printf(“invalid user”); break; case 2: printf(“input too large”); break; default: printf(“system error”); } return; /* optional */ }  When a function does not take any argument, the parameter list is void, e.g. : e.g.: int fabc(void) or int fabc()

17 16 Function Prototype  A function prototype tells the compiler the number and type of arguments that are to be passed to the function and the type of the value that is to be returned by the function  Syntax: type function_name(parameter_type_list); Or type function_name(parameter_list);

18 17 Example of function prototype #include int powerOfTwo(int n); void main() { int i; printf(“Enter a non-negative integer: “); scanf(“%d”, &i); printf(“The %d-th power of 2 is %d\n”, i, powerOfTwo(i)); return; } int powerOfTwo(int n) { int power=1; for (; n>0; n--) power *= 2; return power; }

19 18 A multi-file C program pgm.h #include #define N 3 void fct1(int); void fct2(void); void prn_info(void);

20 19 A multi-file C program ( cont’d ) main.c #include "pgm.h" int main(void) { char ans; printf("\nDo you want more information? "); scanf("%c", &ans); if (ans == 'y' || ans == 'Y') prn_info(); fct1(N); printf("\nBye!\n\n"); return 0; }

21 20 A multi-file C program ( cont’d ) prn.c #include "pgm.h" void prn_info(void) { printf(”\nUsage: pgm\n\n" "This program illustrates how one can write\n" "a program in more than one file. In this\n" "example, we have a single.h file that gets\n" "included at the top of our three.c files.\n" "Thus the.h file acts as the \"glue\"\n" "that binds the program together.\n" "\n”); }

22 21 A multi-file C program ( cont’d ) fct.c #include “pgm.h” void fct1(int n) { int i; printf(“Hello from fct1()\n”); for (i = 0; i < n; ++i) fct2(); } void fct2(void) { printf(“ Hello from fct2()\n”); }

23 22 How are the files related?

24 23 Macros  Macros are defined with #define  Problem: 11 is printed instead of 25 Solution: #define SQUARE(x) (x)*(x) #define SQUARE(x) x*x #include int main(void) { printf("Result is %d\n", SQUARE(2+3)); return 0; }


Download ppt "1 Lecture 6  Functions  C Standard Libraries  Invocation  Definition  Call-by-value  Prototype  Multi-file program  Macros  Readings: Chapter."

Similar presentations


Ads by Google