html5-img
1 / 47

Functions and scope

Functions and scope. Confidence In scope. Reference: K&K textbook, Chapter 4. Postconditions. You should be able to define function prototypes draw pictures of the runtime stack during execution of code (show understanding of memory model)

tallys
Télécharger la présentation

Functions and scope

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 and scope Confidence In scope Reference: K&K textbook, Chapter 4

  2. Postconditions You should be able to • define function prototypes • draw pictures of the runtime stack during execution of code (show understanding of memory model) • state scope of any identifier in a C program, ie visibility, hiding • Draw pictures of memory based on classes • Explain reason for initialisation restrictions • compile and run multi-file programs

  3. define function prototypes • draw pictures of the runtime stack during execution of code (show understanding of memory model) • state scope of any identifier in a C program, ie visibility, hiding • Draw pictures of memory based on classes • Explain reason for initialisation restrictions • compile and run multi-file programs

  4. Function prototypes /*-------------------------------*/ do_that(){ double *p;... p = do_this(*p); } /*-------------------------------*/ double *do_this(double a){ double *p; ... p = do_that(); }

  5. Function prototype double *do_this(double); /*-------------------------------*/ do_that(){ double *p;... p = do_this(*p); } /*-------------------------------*/ double *do_this(double a){ double *p; ... p = do_that(); }

  6. define function prototypes • draw pictures of the runtime stack during execution of code (show understanding of memory model) • state scope of any identifier in a C program, ie visibility, hiding • Draw pictures of memory based on classes • Explain reason for initialisation restrictions • compile and run multi-file programs

  7. main (){ int m1, m2, m3, m4;... Do_Input(); Process_and_Print(); } Do_Input(){ int i1, i2;... } Process_and_Print(){ int p1, p2;... calc(p1, p2); } calc(int x, int y){ int c1;... }

  8. Process_and_Print(){ int p1, p2;... calc(p1, p2); } Do_Input(){ int i1, i2;... } calc(int x, int y){ int c1;... } main (){ int m1, m2, m3, m4;... Do_Input(); Process_and_Print(); }

  9. Order of function execution main Do_Input Process_and_Print calc • Static v dynamic structure • Order of functions does not affect runtime structure

  10. Scope, static v dynamic structure • Static structure of code • Holds from time code is written • Run time structures • Differ according to data in individual runs • Is scope defined by the static structure or the dynamic structure?

  11. Terminology: scope…. • Scope = Visibility • Visible v hidden

  12. main (){ int m1, m2, m3, m4;... Do_Input(); Process_and_Print(); } Do_Input(){ int i1, i2;... } Process_and_Print(){ int p1, p2;... calc(p1, p2); } calc(int x, int y){ int c1;... } m1 m2 m3 m4

  13. main (){ int m1, m2, m3, m4;... Do_Input(); Process_and_Print(); } Do_Input(){ int i1, i2;... } Process_and_Print(){ int p1, p2;... calc(p1, p2); } calc(int x, int y){ int c1;... } m1 m2 m3 m4 Housekeeping data for Do_Input i1 i2

  14. main (){ int m1, m2, m3, m4;... Do_Input(); Process_and_Print(); } Do_Input(){ int i1, i2;... } Process_and_Print(){ int p1, p2;... calc(p1, p2); } calc(int x, int y){ int c1;... } m1 m2 m3 m4

  15. main (){ int m1, m2, m3, m4;... Do_Input(); Process_and_Print(); } Do_Input(){ int i1, i2;... } Process_and_Print(){ int p1, p2;... calc(p1, p2); } calc(int x, int y){ int c1;... } m1 m2 m3 m4 Housekeeping data for Process_and_Print p1 p2

  16. main (){ int m1, m2, m3, m4;... Do_Input(); Process_and_Print(); } Do_Input(){ int i1, i2;... } Process_and_Print(){ int p1, p2;... calc(p1, p2); } calc(int x, int y){ int c1;... } m1 m2 m3 m4 Housekeeping data for Process_and_Print p1 p2 Housekeeping data for calc x y c1

  17. main (){ int m1, m2, m3, m4;... Do_Input(); Process_and_Print(); } Do_Input(){ int i1, i2;... } Process_and_Print(){ int p1, p2;... calc(p1, p2); } calc(int x, int y){ int c1;... } m1 m2 m3 m4 Housekeeping data for Process_and_Print p1 p2

  18. main (){ int m1, m2, m3, m4;... Do_Input(); Process_and_Print(); } Do_Input(){ int i1, i2;... } Process_and_Print(){ int p1, p2;... calc(p1, p2); } calc(int x, int y){ int c1;... } m1 m2 m3 m4

  19. define function prototypes • draw pictures of the runtime stack during execution of code (show understanding of memory model) • state scope of any identifier in a C program, ie visibility, hiding • Draw pictures of memory based on classes • Explain reason for initialisation restrictions • compile and run multi-file programs

  20. Communication between functions • Arguments (aka parameters) • Global data • Which is better?

  21. Scope • Within blocks • Within a file • Between files

  22. Static structure of programs Typical program structure global declarations main function other functions and global declarations interspersed Note: need function prototypes before the function is used

  23. Static structure of functions Function header Declarations Function body int Dojob(int age) { int jam; … statements of the function body }

  24. Static structure of blocks Declarations Block body while …. { int jam; … statements of the block body }

  25. Scope of variable identifiers outer a b inner a d {/* outer block */ int a; int b; ... { /* inner block */ char a; int d; ... } ... }

  26. fileA variables functions main A a1a2a3a4a5 int a1; int main(){ int a2; ... } float a3; char A(char a4){ char a5; ... }

  27. Scope between files • Importing variable identifiers extern int a1; • Hiding variable and function identifiers static

  28. Scope between files • See example on page 75, Chapter 4 of textbook

  29. Exercise • How to make a3 accessible to main? To B2? • What happens if we added to start of fileA: int b1; • and what about int b2; • What is the effect of adding to start of fileB: extern char b1; • What happens if weadd to start of fileA: • extern int B2();

  30. Storage classes • auto (stack) • static • register (optimisation - obsolete) • extern You need to distinguish these in drawings of memory models

  31. Storage classes • stack • global/static You need to distinguish these in drawings of memory models

  32. Storage classes – memory models • Dynamic, volatile memory, change through the execution of the program • auto (stack) • register • Persistent memory – stable for the duration of the runtime of the program • static • extern You need to distinguish these in drawings of memory models

  33. Initialisations - globals #define BOUND 100 static int a = 72 * sizeof(int); int b = BOUND; extern char x; int y; heap extern, static register auto Persistent storage Volatile storage

  34. Initialisations - globals #define BOUND 100 static int a = 72 * sizeof(int); int b = BOUND; extern char x; int y; heap extern, static register auto Persistent storage Volatile storage

  35. Initialisations - globals #define BOUND 100 static int a = 72 * sizeof(int); /* constant expression */ int b = BOUND; /* constant expression */ extern char x; /* cannot be initialised here */ int y; /* defaults to zero */

  36. Initialisations - local #define BOUND 100 static int a = 72 * sizeof(int); int b = BOUND; extern char x; int y; fnA(){ static int c = BOUND + 7; int d = a + fnXX(); register int e = b; extern char g; ... }

  37. Initialisations - local fnA(){ static int c = BOUND + 7; /* constant expression */ int d = a + fnXX(); /* any expression */ register int e = b; /* any expression */ extern char g; /* cannot be initialised */ ... }

  38. define function prototypes • draw pictures of the runtime stack during execution of code (show understanding of memory model) • state scope of any identifier in a C program, ie visibility, hiding • Draw pictures of memory based on classes • Explain reason for initialisation restrictions • compile and run multi-file programs

  39. Compiling multi-file programs Source code Name.c Preprocessor C compiler Assembly code Name.s Assembler Object code Name.o Linker a.out

  40. Compiling multi-file programs • Compiler can stop at the .o stage • Linker takes one or more .o files and produces the a.out • Use the ‘-c’ flag to gcc to produce the .o

  41. Compiling multi-file programs bash$gcc -c doin.c proc.c dout.c bash$gcc doin.o proc.o dout.o –o pds bash$gcc -c proc.c bash$gcc doin.o proc.o dout.o -o pds

  42. Using functions from the standard libraries #include <math.h> double sin(double); double n; double x; scanf("%f", &n); x = sin(n); __________________________________ gcc -lm ... Library name is ‘m’

  43. Postconditions You should be able to • define function prototypes • draw pictures of the runtime stack during execution of code (show understanding of memory model) • state scope of any identifier in a C program, ie visibility, hiding • Draw pictures of memory based on classes • Explain reason for initialisation restrictions • compile and run multi-file programs

  44. define function prototypes • draw pictures of the runtime stack during execution of code (show understanding of memory model) • state scope of any identifier in a C program, ie visibility, hiding • Draw pictures of memory based on classes • Explain reason for initialisation restrictions • compile and run multi-file programs

  45. Memory: the picture so far • Memory as a long stream of bits • C code associates a name with a part of the memory – with space for that type • Memory for different things: • Ordinary data • Arrays • Pointers • Draw lines showing where pointers point to

  46. Memory: the picture so far • Memory as a long stream of bits • C code associates a name with a part of the memory – with space for that type • Memory for different things: • Ordinary data • Arrays • Pointers • Draw lines showing where pointers point to nump chs[0] chs[2] chs[1] chs[3] X_accurate

More Related