Functions C and Data Structures Baojian Hua
Typical C Program Organization Program file1 function1functionm … … … filen function1functionn
Function Definition return-type function-name (argument declarations) { declarations and statements } // example: int sum (int a, int b) { int temp; temp = a + b; return temp; }
External Variables // Instead of function arguments and local // variables, we may also declare external // variables. External: not within any function. int i; int get () { return i; } void set (int a) { i = a; }
Scope Rules automatic (local) variable from the declaration point to block end extern variables; function names from the declaration point to file end “ extern ” defined somewhere, “ don ’ t care now ”
Scope Rule Example int a; // #2 extern int b; // #3 void foo () { int i; // #1 while (…) { int j; // #1 b = i+j; } extern void bar (); // #3 …; bar (); }
External Variables // file1.c int i; float j[10]; int f () { …; } // file2.c extern int i; extern float j[]; int g () { i = 9; …; } variable definitions variable declarations
Declarations and Definitions // file1.c int i; float j[10]; int f() { … } // file2.c extern int i; extern float j[]; … // file3.c extern int i; extern float j[]; …
External Variables Pros: An important way for data sharing The poor man ’ s method to build closures In future slides, we ’ ll discuss another one Also think objects in OO languages Cons: External variables blur the connections between functions and modules Involve the internal working of a linker
Static Variables By default, all external variables and functions (in all source files) are visible to all program code whether or not in same source file However, in some circumstance, we want to keep our data private Ex: visa number and passwd C provides the “ static ” mechanism
Static Variables // main.c extern int myDollar; extern void add (int a); int main () { // Ooooops! myDollar -= ; add (999999); …; } // visa.c int myDollar; int lookup () { return myDollar; } void add (int a) { myDollar += a; } // void sub (int a)
Static Variables // main.c // compiler complains… extern int myDollar; extern void add (int a); int main () { // Ooooops! myDollar -= ; add (999999); …; } // visa.c static int myDollar; int lookup () { return myDollar; } void add (int a) { myDollar += a; } // void sub (int a)
Static Variables “ Static ” can also applied to automatic variables to tie different function calls In a summary, the terminology “ static ” is a little misleading maybe “ private ” is more meaningful, just as that of C++ or Java
Initialization variable sortsautomatic init ’ user init ’ autoNOany expression extern variableYES. init ’ to 0constants “ extern ” NOForbidden
Header Files Problem with “ extern ” ? A header file group common declarations together could be included by other files typically named *.h Header file is C ’ s rudimentary module system Pros: Separate compilation Essential for linking user code with libraries Cons: flat name space
Header Files Example // area.h double area (int r); // area.c #include “area.h” double area (int r) { double pi = 3.14; double f = pi *r *r; return f; } // main.c #include “area.h” int main () { double f; f = area (5); return 0; }
Header Files Example // area.h double area (int r); // area.c #include “area.h” double area (int r) { double pi = 3.14; double f = pi *r *r; return f; } // main.c #include “area.h” int main () { double f; f = area (5); return 0; }
Conditional Inclusion // area.h #ifndef AREA_H #define AREA_H double area (int r); #endif // main.c #include “area.h” … Why these? We ’ d discuss it in future slides. // area.c #include “area.h” double area (int r) { double pi = 3.14; double f = pi *r *r; return f; }
Recursive Functions // Consider the fibnacci numbers: // { 0, if n == 0; // fib (n) = { 1, if n == 1; // { fib(n-1)+ fib(n-2), otherwise. // C code: int fib (int n) { if (0==n) return 0; else if (1==n) return 1; else return (fib(n-1) + fib(n-2)); }
Recursive Functions // Next, we crawl through this function to see // how fib(5) is computed: fib (5) = fib(4)+fib(3) = fib(3)+fib(2)+fib(3) = fib(2)+fib(1)+fib(2)+fib(3) = fib(1)+fib(0)+fib(1)+fib(2)+fib(3) = fib(1)+fib(2)+fib(3) = fib(2)+fib(3) = … = 5 // As we can see, it’s too inefficient as we are // too stupid doing much redundant computations. Exercise: design a more efficient version!