Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions C and Data Structures Baojian Hua

Similar presentations


Presentation on theme: "Functions C and Data Structures Baojian Hua"— Presentation transcript:

1 Functions C and Data Structures Baojian Hua bjhua@ustc.edu.cn

2 Typical C Program Organization Program file1 function1functionm … … … filen function1functionn

3 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; }

4 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; }

5 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 ”

6 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 (); }

7 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

8 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[]; …

9 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

10 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

11 Static Variables // main.c extern int myDollar; extern void add (int a); int main () { // Ooooops! myDollar -= 999999; add (999999); …; } // visa.c int myDollar; int lookup () { return myDollar; } void add (int a) { myDollar += a; } // void sub (int a)

12 Static Variables // main.c // compiler complains… extern int myDollar; extern void add (int a); int main () { // Ooooops! myDollar -= 999999; add (999999); …; } // visa.c static int myDollar; int lookup () { return myDollar; } void add (int a) { myDollar += a; } // void sub (int a)

13 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

14 Initialization variable sortsautomatic init ’ user init ’ autoNOany expression extern variableYES. init ’ to 0constants “ extern ” NOForbidden

15 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

16 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; }

17 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; }

18 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; }

19 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)); }

20 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) = 1 + 0 + fib(1)+fib(2)+fib(3) = 1 + 1 + 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!


Download ppt "Functions C and Data Structures Baojian Hua"

Similar presentations


Ads by Google