1 lecture 6 functions c standard libraries invocation definition call-by-value prototype ...
Post on 21-Dec-2015
215 views
TRANSCRIPT
1
Lecture 6 Functions
C Standard Libraries Invocation Definition Call-by-value Prototype
Multi-file program Macros Readings: Chapter 4 Section 1 to 12
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.
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<assert.h> <float.h> <math.h> <stdarg.h>
<stdlib.h> <ctype.h> <limits.h> <setjmp.h>
<stddef.h> <string.h> <errno.h> <locale.h>
<signal.h> <stdio.h> <time.h>
4
Functions
Why functions? Natural for top-down design approach Enhance software reusability Avoid repeating code
main
worker3worker2worker1
worker3worker4
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 )
6
Calling Predefined Functions
#include <stdio.h>
#include <math.h>
void main()
{
float area;
printf(“Enter the area of a square: “);
scanf(“%f”, &area);
printf(“The square has perimeter: %f”,
4*sqrt(area));
}
7
Another example#include <stdio.h>#include <stdlib.h>
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;}
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.
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
10
Example#include <stdio.h>
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;}
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
12
Call-by-value (cont’)
powern
resulti
112870
1287
Destroyed afterfunction invocation
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;
14
Example of return#include <stdio.h>
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;}
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()
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);
Ortype function_name(parameter_list);
17
Example of function prototype
#include <stdio.h>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;}
18
A multi-file C program
pgm.h
#include <stdio.h>
#define N 3
void fct1(int);
void fct2(void);
void prn_info(void);
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;}
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”);
}
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”);
}
22
How are the files related?
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<stdio.h>
int main(void)
{
printf("Result is %d\n", SQUARE(2+3));
return 0;
}