1 / 20

Functions

This text explores the organization of C programs, function definitions, external variables, static variables, header files, and conditional inclusion. Includes examples and explanations.

jmejia
Download Presentation

Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Typical C Program Organization Program … file1 filen … … function1 functionm function1 functionn

  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

  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. variable definitions variable declarations External Variables // file1.c int i; float j[10]; int f () { …; } // file2.c extern int i; extern float j[]; int g () { i = 9; …; }

  8. Declarations and Definitions // file3.c extern int i; extern float j[]; … // file1.c int i; float j[10]; int f() { … } // file2.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 // visa.c int myDollar; int lookup () { return myDollar; } void add (int a) { myDollar += a; } // void sub (int a) // main.c extern int myDollar; extern void add (int a); int main () { // Ooooops! myDollar -= 999999; add (999999); …; }

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

  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

  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 // area.c #include “area.h” double area (int r) { double pi = 3.14; double f = pi *r *r; return f; } Why these? We’d discuss it in future slides. // main.c #include “area.h” …

  19. Recursive Functions // Consider the fibnacci numbers: // { 0, if n == 0; // fib (n) = { 1, if n == 1; // { fib(n-1) + fib(n-2), otherwise. int fib (int n) { switch (n) { case 0: return 0; case 1: return 1; default: 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!

More Related