Presentation is loading. Please wait.

Presentation is loading. Please wait.

C Programming Lecture 7 Functions. Structured Programming b Keep the flow of control in a program as simple as possible. b Use top-down design. Keep decomposing.

Similar presentations


Presentation on theme: "C Programming Lecture 7 Functions. Structured Programming b Keep the flow of control in a program as simple as possible. b Use top-down design. Keep decomposing."— Presentation transcript:

1 C Programming Lecture 7 Functions

2 Structured Programming b Keep the flow of control in a program as simple as possible. b Use top-down design. Keep decomposing (also known as factoring) a problem into smaller problems until you have a collection of small problems that you can easily solve.Keep decomposing (also known as factoring) a problem into smaller problems until you have a collection of small problems that you can easily solve.

3 Top-Down Design Using Functions b C programs normally consist of a collection of user- defined functions. Each function solves one of the small problems obtained using top-down design.Each function solves one of the small problems obtained using top-down design. Functions call or invoke other functions as needed.Functions call or invoke other functions as needed.

4 Function Definitions, Prototypes, and Calls #include void prn_message(void);/* fct prototype */ /* tells the compiler that this */ /* function takes no arguments */ int main(void) /* and returns no value. */ { prn_message();/* fct invocation */ } void prn_message(void) /* fct definition */ { printf(“A message for you: “); printf(“Have a nice day!\n”); }

5 Form of a Function Definition type function_name ( parameter type list ) { declarations statements } Some Terminology Header: Everything before the first brace. Body:Everything between the braces. Type:Type of the value returned by the function. Parameter List: A list of identifiers that provide information for use within the body of the function. Also called formal parameters.

6 The return Statement b When a return statement is executed, program control is immediately passed back to the calling environment. If an expression follows the keyword return, the value of the expression is returned to the calling environment as well.If an expression follows the keyword return, the value of the expression is returned to the calling environment as well.return; return expression;

7 If There is No return b Control is passed back to the calling environment when the closing brace of the body is encountered. Known as “falling of the end.”Known as “falling of the end.”

8 Exit Status and return Verus exit( ) b In main() either return expr; or orexit(expr); will return an integer value to the operating system. will return an integer value to the operating system. b In functions other than main(), the effects of return and exit are different.

9 return expr Versus exit(expr) b return expr returns the value of expr to the calling function. b exit(expr) always causes the program to terminate and returns an exit status to the operating system. The value in expr is the exit status.

10 Demo Program – Using a Function to Calculate the Minimum of 2 Values #include int min(int a, int 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 minimum is %d.\n\n”, j, k, m); return 0; } int min(int a, int b) { if (a < b) return a; else return b; }

11 Function Prototypes b A function prototype tells the compiler: The number and type of arguments that are to be passed to the function.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.The type of the value that is to be returned by the function. b General Form of a Function Prototype type function_name( parameter type list);

12 Examples of Function Prototypes double sqrt(double); double sqrt(double); b The parameter list is typically a comma-separated list of types. Identifiers are optional. void f(char c, int i); void f(char c, int i); is equivalent to is equivalent to void f(char, int); void f(char, int);

13 The Keyword void b void is used if: A function takes no arguments.A function takes no arguments. If no value is returned by the function.If no value is returned by the function.

14 Function Invocation b As we have seen, a function is invoked (or called) by writing its name and an appropriate list of arguments within parentheses. The arguments must match in number and type the parameters in the parameter list of the function definition.The arguments must match in number and type the parameters in the parameter list of the function definition.

15 Call-by-Value b In C, all arguments are passed call-by-value. This means that each argument is evaluated, and its value is used in place of the corresponding formal parameter in the called function.This means that each argument is evaluated, and its value is used in place of the corresponding formal parameter in the called function.

16 Demonstration Program for Call-by-Value #include int compute_sum(int n); int main(void) { int n = 3, sum; printf(“%d\n”, n); /* 3 is printed */ sum = compute_sum(n); printf(“%d\n”, n); /* 3 is printed */ printf(“%d\n”, sum); return 0; } int compute_sum(int n) { int sum = 0; for (; n > 0; --n) /* in main(), n is unchanged */ sum += n; printf(“%d\n”, n); /* 0 is printed */ return sum; }

17 #include list of function prototypes int main(void) {... } int max(int a, int b) {... } int min(int a, int b) {... } void prn_random_numbers(int k) {... } Standard Style for Function Definition Order

18 #include int max(int a, int b) {... } int min(int a, int b) {... } void prn_random_numbers(int k) {... } int main(void) {... } “Alternate Style for Function Definition Order We will use the standard style.

19 Common Programming Errors b If f() is a function and v is a variable, then the function call f(v) cannot change the value in the variable v. A common error for beginners is assuming the the value in v can be changed by a function call such as f(v).A common error for beginners is assuming the the value in v can be changed by a function call such as f(v).

20 Style b Avoid naming functions you write with the same name as system functions. Example: read, write, printExample: read, write, print b Minimize the number of return statements in a given function. b Use names for parameters that clearly identify their purpose.


Download ppt "C Programming Lecture 7 Functions. Structured Programming b Keep the flow of control in a program as simple as possible. b Use top-down design. Keep decomposing."

Similar presentations


Ads by Google