250 likes | 497 Views
Functions. C standard library Function prototype, call, & definition Pass by value, reference Storage Classes Scope Recursion. C Standard Library. Prepackaged functions already written D & D textbook, Section 5.8, p. 152 On web: http://en.wikipedia.org/wiki/C_standard_library.
E N D
Functions • C standard library • Function prototype, call, & definition • Pass by value, reference • Storage Classes • Scope • Recursion
C Standard Library • Prepackaged functions already written • D & D textbook, Section 5.8, p. 152 • On web: http://en.wikipedia.org/wiki/C_standard_library
<assert.h> : Diagnostics • <ctype.h> : Character Class Tests • <errno.h> : Error Codes Reported by Library Functions • <float.h> : Implementation-defined Floating-Point Limits • <limits.h> : Implementation-defined Limits • <locale.h> : Locale-specific Information • <math.h> : Mathematical Functions* • <setjmp.h> : Non-local Jumps • <signal.h> : Signals • <stdarg.h> : Variable Argument Lists • <stddef.h> : Definitions of General Use • <stdio.h> : Input and Output • <stdlib.h> : Utility functions • <string.h> : String functions • <time.h> : Time and Date functions *Note: the math library functions are not supported on the UHUNIX gcc software at this time
Random Number Generation • Use function rand() from the <stdlib.h> library • n = a + rand() % b; • n = pseudorandom number • a = shifting value (the 1st number in the range of integers) • b = scaling factor (the width of the range of integers) • Also use function srand() from the <stdlib.h> library & function time() from the <time.h> library • srand(time (NULL)); • srand( ) = seeds the pseudorandom numbers • time(NULL) = gives the number of seconds that have elapsed since January 1, 1970 • See Section 5.10 in textbook
Dice-Rolling Example • Simulates rolling a six-sided die 6000 times to test the random number generator • See rand.c on the class webpage in the “examples” link • From p. 154-156 in Deitel & Deitel textbook
How to Write Functions in C //See add.c #include <stdio.h> int add3(int, int, int); int main(void) { printf("1+2+3=%d\n", add3(1,2,3)); return 0; } int add3(int a, int b, int c){ return a+b+c; }
How to Write Functions in C int add3(int, int, int); • Function prototype (function declaration) • Compiler uses to validate function calls printf("%d\n", add3(1,2,3)); • Function call • Arguments int add3(int a, int b, int c){…} • Function definition • Return type, parameter types & names, body
//See add.c #include<stdio.h> /*Function prototype: Compiler uses to validate function calls. Contains return type & parameter types.*/int add3(int, int, int);int main(void){ /*Function call: Contains arguments in parenthesis*/ printf("1+2+3=%d\n", add3(1,2,3));return 0; } /*Function definition: Contains the return type, parameter types & names, and body*/int add3(int a, int b, int c){return a+b+c; }
Possible Bugs • If a return type or parameter is not listed, then the compiler will assume it is of type "int" • The return type and the argument types of the function prototype & the function definition must be the same
Possible Bugs 1 //See bugs.c 2 #include<stdio.h> 3 //compiler assumes the return type is "int" 4 add3(int, int, int); 5 int main(void){6 add3(1,2,3);7 return 0;8 } 9 void add3(int a, int b, int c){10 printf("1+2+3=%d\n", a+b+c);11 }12 program.c:4: warning: data definition has no type or storage class program.c:9: error: conflicting types for 'add3' program.c:4: error: previous declaration of 'add3' was here program.c:9: error: conflicting types for 'add3' program.c:4: error: previous declaration of 'add3' was here
Function Calls • Call by value • Copy of argument’s value is passed to function • Changes in the copy do not affect the original variable (no “side effects”) • All calls are “call by value” in C • Call by reference • Can modify the argument’s value • Simulated in C by using pointers • Make a copy of address & modify the contents • Used in C arrays & pointers
Call by Value 1 //See value.c 2#include<stdio.h> 3 void fun1(int, int); 4 int main(void) { 5 int x=10, y=20; 6 printf("x=%d y=%d\n", x, y); 7 fun1(x,y); 8 printf("x=%d y=%d\n", x, y); 9 return 0; 10 } 11 void fun1(int x, int y){ 12 x++; y++; 13 printf("x=%d y=%d\n", x, y); 14 return; 15 } //x=10 y=20 //x=11 y=21 //x=10 y=20
Storage Classes • C has four: auto, register, extern, static • A variable’s storage class determines its storage duration, scope, and linkage • Storage duration (automatic & static) • Period it exists in memory • Scope (file, function & block) • Where it can be referenced in a program • Linkage (internal & external) • Whether a variable is accessed only in current source file or in other source files
Auto Storage Class • auto • Has automatic storage duration • Variable created when block is entered, exist while block is active, destroyed when block is exited • Default for all variables inside functions auto int x = 100;
Register Storage Class • register • Also has automatic storage • Put variable in high-speed register • Compiler is free to ignore it register int counter = 1;
Extern Storage Class • extern • Has static storage duration • Exists from start of program • Default for global variables & functions • Referenced by any function that follows its declaration • If a variable is used in a different file, then it must be declared “extern”, while the original variable must not be declared “extern”
file2.c #include <stdio.h> extern int x; int main2(){ printf("x=%d\n",x); return 0; } file1.c #include <stdio.h> int x=50; int main(){ printf("x=%d\n",x); main2(); return 0; } gcc file1.c file2.c ./a.out x=50 x=50 Note: Unless you are sharing a variable in multiple files, then you don’t use the “extern” declaration for global variables
Static Storage Class • static • Also has static storage duration • In functions • Declared in a function & retains its value when function is exited • Next time function is called, it still has the same value • Example program • See storage.c for an example of extern, register, auto, static variables
Storage Class • extern (global variable) • Exists for the life of the program • Use anywhere in the program • static • Exists for the life of the program • Only use inside the function where it is declared • auto, register (local variable) • Exists for the life of the function • Only use inside the function where it is declared
Scope • Portion of a program where a variable or function can be referenced • Four kinds • Function • File • Block • Function-prototype
Function Scope • Can be used anywhere within a function • Only labels have function scope • Identifier followed by a colon • Used in switch & goto statements switch(ch){ case 'A': case 'a': a++; break; . . .} start: printf("hello"); goto start;
File & Function-Prototype • File scope • Identifiers declared outside any function • Known from declaration to end of file • Global variables, function prototypes • function definitions • Function-prototype scope • Variables declared in function prototypes are not visible outside function
Block Scope • Identifiers declared within a block • Only visible inside the block • Variables declared inside a block may hide any identically named variables in outer blocks • Will remain in existence until the matching right brace
#include <stdio.h> /*See block.c*/ int main(void){ int x = 10; { int x = 20; { int x = 30; printf("x=%d\n",x); } printf("x=%d\n",x); } printf("x=%d\n",x); return 0; } x=30 x=20 x=10