Presentation is loading. Please wait.

Presentation is loading. Please wait.

0 4.2 Functions Returning Non-integers The return statement is the mechanism for returning a value : return expression; Remark : The expression will be.

Similar presentations


Presentation on theme: "0 4.2 Functions Returning Non-integers The return statement is the mechanism for returning a value : return expression; Remark : The expression will be."— Presentation transcript:

1 0 4.2 Functions Returning Non-integers The return statement is the mechanism for returning a value : return expression; Remark : The expression will be converted to the return type of the function if necessary. Imperative Programming, B. Hirsbrunner, diuf.unifr.ch/pai/ipS6 - 24 April 2007

2 1 Function Prototypes 1. Full function prototype (recommended !) return-type function-name (argument declarations) 2. Shortest recommended function prototype (ok in some special cases) return-type function-name (type declarations) 3. Older function declaration (never recommended !) return-type function-name () 4. Shortest function declaration (never recommended !) function-name () Remark. (3) and (4) are accepted in the ANSI standard, but are only intended to permit older C programs to compile with new compilers. It’s a very bad idea to use it with new programs. Example 1. int toto (char c) 2. int toto (char) 3. int toto () 4. toto ()

3 2 4.3 External Variables A program is just a set of external objects, which are either variables or functions Functions are always external C does not allow functions to be defined inside other functions External variables are defined outside any function Internal variables to a function come into existence when the function is entered, and disappear when it is left

4 3 First Large Example: A Calculator Example : Write a calculator program that provides the operators +, -, *, and /. * - 1234 + Hint: use the reverse polish notation, i.e. an infix expression like (1-2)*(3+4) is entered as the postfix expression 1 2 - 3 4 + * Pseudo C code while (next operator or operand is not end-of-file indicator) if (number) push it else if (operator) pop operands do operation push result else if (newline) pop and print top of stack else error Skeleton Program #include … #define … function declarations for main main() {…} external variables for push and pop void push(double f) {…} double pop(void) {…} int getop(char s[]) {…}

5 4 4.4 Scope Rules Definition The scope of a name is the part of the program within which the name can be used. Scope of an external variable or function lasts from the point at which it is declared to the end of the file being compiled Scope of an automatic variable (i.e. local variable of a function) is the block in which the name is declared Remarks about the declaration and definition of an external variable A declaration announces the properties of a variable (primary its type and name), whereas a definition also causes storage to be aside : extern double val[]; /* declaration of an external variable */ double val[MAXVAL]; /* definition of an external variable */ Usage: if an external variable is to be referred before it is defined, or if it is defined in a different file, then the extern declaration is mandatory

6 5 4.5 Header Files : Hello World (revisited) hello.c tellMe.c Dependency graph tellMe.h #include "tellMe.h" /* for tellMe() */ main() { tellMe("Hello World"); } hello.c : #include /* for printf() */ #include "tellMe.h" /* for tellMe() */ void tellMe(char s[]) { printf("\n tellMe> %s\r\n\n", s); } tellMe.c : void tellMe(char s[]) tellMe.h : Header file.h : specification Source file.c : implementation Makefile : hello : hello.o tellMe.o gcc -o hello hello.o tellMe.o hello.o : hello.c gcc -c hello.c tellMe.o : tellMe.c gcc -c tellMe.c hello hello.otellMe.o hello.ctellMe.c Dependency graph

7 6 4.5 Header Files : Calculator (revisited 1/2) calc.h void push(double); double pop(void); #define NUMBER ‘0’ int getop(char []); int getch(void); void ungetch(int); #include #include "calc.h" #define MAXOP 100 main() {…} calculator.c #include #include "calc.h" int getop(char []) {…} getop.c #include #include "calc.h" #define BUFSIZE ‘100’ char bufsize[BUFSIZE ]; int bufp = 0; int getch(void) {…} void ungetch(int) {…} getch.c stack.c #include #include "calc.h" #define MAXVAL 100 int sp = 0; double val[MAXVAL]; void push(double) {…} double pop(void) {…} calculator.c stack.cgetop.c getch.c Dependency graph (all files share calc.h)

8 7 4.5 Header Files : Calculator (revisited 2/2) #include #include "my_stack.h" #include "my_special_io.h" #define MAXOP 100 main() {…} calculator.c #include #include "my_io.h" #include "my_special_io.h" int getop(char []) {…} my_special_io.c #include #include "my_io.h" #define BUFSIZE ‘100’ char bufsize[BUFSIZE ]; int bufp = 0; int getch(void) {…} void ungetch(int) {…} my_io.c my_stack.c #include #include "my_stack.h" #define MAXVAL 100 int sp = 0; double val[MAXVAL]; void push(double) {…} double pop(void) {…} my_special_io.h #define NUMBER ‘0’ int getop(char []); my_io.h int getch(void); void ungetch(int); my_stack.h void push(double); double pop(void); calculator.c my_stack.cmy_special_io.c my_io.c Dependency graph my_stack.hmy_special_io.h my_io.h

9 8 4.6 Static Variables Definition 1.The static declaration, applied to an external variable or function, limits the scope of that object to the rest of the source file. 2.The static declaration, applied to an automatic variable (i.e. a variable defined locally to a function), promote that variable to a permanent variable. Usage static char buf[BUFSIZE]; /* buffer for ungetch */ static int bufp = 0 /* next free position in buf */ Remark 1.A static external variable is a "private global variable with permanent storage" (private and global relative to the file in which it is declared) 2.A static automatic variable is a "private local variable with permanent storage" (private and local relative to the function in which it is declared)

10 9 4.7 Register Variables Definition A register declaration advises the compiler that the variable in question will be heavely used. Usage void f(register unsigned m, register long n) { register int i;... } Remarks 1.The idea is that register variables are to be placed in machine registers (i.e. a small very rapid but very expensive memory). 2.The compilers are free to ignore the advice. 3.The register declaration can only be applied to automatic variables and to the formal parameters of a function.

11 10 4.8 Block Structure Variable can be defined in a block-structured fashion !!! Declarations of variables (including initializations) may follow the left brace that introduces any coumpound statement, not just the one that begins a function. Function cannot be defined in a block-strutured fashion !!! Functions may not be defined within other functions. Example if (n>0) { int i; /* declare a new integer i */ for (i=0; i<n; i++)... } The scope of the variable i is the "true" branch of the if; this i is unrelated to any i outside the block. Remark. Let av and sv be an automatic resp. static variable declared and initialized in a block. Then av is initialized each time the block is entered and sv only the first time. (Hint: av is a local variable without permanent storage and sv is a local variable with permanent storage!)

12 11 4.9 Initialization Fundamentals External and static variables : initialization at compile time –The initialization is done once, conceptually before the program begins execution –The initializer must be a constant expression –The value is initialized to zero in the absence of explicit initialization Automatic and register variables : initialization at run time –The initialization is done during the execution of the program, that is each time the function or block is entered –The initializer can be any expression –The value is undefined (i.e. garbage) in the absence of explicit initialization Usage int x = 1; char squote = '\''; long day = 1000L * 60L * 60L * 24L; /* milliseconds/day */ int binsearch(int x, int v[], int n) { int low=0, high=n-1, mid;... }

13 12 4.10 Recursion + cshell + Execution Diagram #include int f(int n) {if (n>1) return n*f(n-1); else return 1;} /* f(n)=n! */ main () {printf("%d\n", f(getchar()-'0')); return 0;} fac.c % gcc -o fac fac.c %./fac In C Shell (i.e. terminal) main() f(3) f(2) n1=3; f(2) f(1) n2=2; f(1) return 1; n3=1; 1 return 2*1; 2 return 3*2; 6 getchar(); printf("%d\n", 6); cshell (terminal)./fac Assume the user hit the key '3' See next slide ! return 0 0 Execution Diagram (for %./fac) %./fac ;./fac %./fac &&./fac %./fac ||./fac %./fac |./fac Try (hit 3 when needed) (factorial test program)

14 13 cshell Application and System Calls 1 System call 2 OS kernel is in charge to (1) read the next character from the standard input file, i.e. from the keyboard (assume the user hit the key '3'), (2) display it on the terminal window and (3) return it to getchar() 3 OS kernel is in charge to write the received value to the standard output file, i.e. the terminal window main() cshell (terminal) fac getchar() '3' f(3) OS kernel readWithEcho(...) 1 2 'read c' and display it getchar() printf(...) printf() write(...) 1 3 'write' return 0 0 printf()

15 14 4.11 The C Preprocessor : C language facilities (1/2) For a multifile program, the first three operations are performed on each individual source code file, creating an object file for each source code file. The final linking operation combines all object codes. When you invoke gcc without options, it initiates a sequence of four basic operations: (1) Preprocessing, (2) Compilation, (3) Assembly, (4) Linking. The cpp preprocessor allow you, among other things, to display the result of a preprocessing operation. Example. Consider we have the following text in the 'macro.txt' file: #define square(X) (X)*(X) square(2) square(x+1) and then invoke the cpp preprocessor: % cpp macro.txt (2)*(2) (x+1)*(x+1) % Note that 'macro.txt' is not a correct C source code file; but it is fine for preprocessing.

16 15 4.11 The C Preprocessor : C language facilities (2/2) Macro. A macro is a fragment of code which has been given a name. Whenever the name is used, it is replaced by the contents of the macro. Usage: #define name replacement-text Example: #define square(x) (x)*(x) #define square(x) x*x // right syntax, but wrong semantics! Conditional. A conditional is a directive that instructs the preprocessor to select whether or not to include a chunk of code in the final token stream passed to the compiler. For example, a program may need to use different code depending on the machine or operating system it is to run on. "Usage": There are 3 conditional directives: #if, #ifdef or #ifndef File inclusion. The #include directive works by directing the C preprocessor to scan the specified file as input before continuing with the rest of the current file. Usage: #include // for system header files #include "file" // for header files of your own program Example: #include


Download ppt "0 4.2 Functions Returning Non-integers The return statement is the mechanism for returning a value : return expression; Remark : The expression will be."

Similar presentations


Ads by Google