Download - Functions and scope
![Page 1: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/1.jpg)
Functions and scopeFunctions and scope
ConfidenceConfidence
In scopeIn scope
Reference:
K&K textbook, Chapter 4
![Page 2: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/2.jpg)
PostconditionsPostconditionsYou 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
![Page 3: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/3.jpg)
• 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
![Page 4: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/4.jpg)
Function prototypesFunction prototypes
/*-------------------------------*/do_that( ) {
double *p; ...p = do_this(*p);
}/*-------------------------------*/double *do_this(double a) {
double *p; ...p = do_that( );
}
![Page 5: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/5.jpg)
double *do_this(double);/*-------------------------------*/do_that( ) {
double *p; ...p = do_this(*p);
}/*-------------------------------*/double *do_this(double a) {
double *p; ...p = do_that( );
}
Function prototype
![Page 6: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/6.jpg)
• 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
![Page 7: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/7.jpg)
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; ...}
![Page 8: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/8.jpg)
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( );
}
![Page 9: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/9.jpg)
Order of function executionOrder of function executionmain
Do_Input
Process_and_Print
calc
• Static v dynamic structure
• Order of functions does not affect runtime structure
![Page 10: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/10.jpg)
Scope, static v dynamic structureScope, 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?
![Page 11: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/11.jpg)
Terminology: scope….Terminology: scope….
• Scope = Visibility
• Visible v hidden
![Page 12: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/12.jpg)
m1m2m3m4
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; ...}
![Page 13: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/13.jpg)
Housekeeping data for Do_Input
i1i2
m1m2m3m4
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; ...}
![Page 14: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/14.jpg)
m1m2m3m4
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; ...}
![Page 15: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/15.jpg)
Housekeeping datafor
Process_and_Printp1p2
m1m2m3m4
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; ...}
![Page 16: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/16.jpg)
Housekeeping data for calc
xyc1
Housekeeping datafor
Process_and_Printp1p2
m1m2m3m4
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; ...}
![Page 17: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/17.jpg)
Housekeeping datafor
Process_and_Printp1p2
m1m2m3m4
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; ...}
![Page 18: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/18.jpg)
m1m2m3m4
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; ...}
![Page 19: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/19.jpg)
• 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
![Page 20: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/20.jpg)
Communication between functions
• Arguments (aka parameters)
• Global data
• Which is better?
![Page 21: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/21.jpg)
ScopeScope
• Within blocks
• Within a file
• Between files
![Page 22: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/22.jpg)
Static structure of programs
Typical program structureglobal declarations
main function
other functions and global declarations interspersed
Note: need function prototypes before the function is used
![Page 23: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/23.jpg)
Static structure of functions
Function headerDeclarationsFunction body
int Dojob(int age){
int jam;… statements of the function body
}
![Page 24: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/24.jpg)
Static structure of blocks
Declarations
Block body
while ….
{int jam;
… statements of the block body
}
![Page 25: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/25.jpg)
{/* outer block */ int a; int b; ...
{ /* inner block */ char a; int d; ... } ...}
Scope of variable identifiers
outera b
innera d
![Page 26: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/26.jpg)
int main( ){ int a2; ...}
fileA
float a3;
char A(char a4){ char a5; ...}
functionsvariablesa1 a2 a3 a4 a5int a1; main A
![Page 27: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/27.jpg)
Scope between files
• Importing variable identifiers
extern int a1;
• Hiding variable and function identifiers
static
![Page 28: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/28.jpg)
Scope between files
• See example on page 75, Chapter 4 of textbook
![Page 29: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/29.jpg)
ExerciseExercise• 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 we add to start of fileA:• extern int B2();
![Page 30: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/30.jpg)
Storage classesStorage classes
• auto (stack)
• static
• register (optimisation - obsolete)
• extern
You need to distinguish these in drawings of memory models
![Page 31: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/31.jpg)
Storage classesStorage classes
• stack
• global/static
You need to distinguish these in drawings of memory models
![Page 32: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/32.jpg)
Storage classes – memory models 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
![Page 33: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/33.jpg)
Initialisations - globalsInitialisations - globals
#define BOUND 100static int a = 72 * sizeof(int);
int b = BOUND;
extern char x;
int y;
Volatile storage Persistent storageregister auto heap extern, static
![Page 34: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/34.jpg)
Initialisations - globalsInitialisations - globals
#define BOUND 100static int a = 72 * sizeof(int);
int b = BOUND;
extern char x;
int y;
Volatile storage Persistent storageregister auto heap extern, static
![Page 35: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/35.jpg)
Initialisations - globalsInitialisations - globals
#define BOUND 100static int a = 72 * sizeof(int); /* constant
expression */
int b = BOUND; /* constant expression */
extern char x; /* cannot be initialised here */
int y; /* defaults to zero */
![Page 36: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/36.jpg)
Initialisations - localInitialisations - local
#define BOUND 100static 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;
...
}
![Page 37: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/37.jpg)
Initialisations - localInitialisations - 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 */
...
}
![Page 38: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/38.jpg)
• 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
![Page 39: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/39.jpg)
Compiling multi-file programsCompiling multi-file programs
Preprocessor
C compiler
Assembler
Linker
Source codeName.c
Assembly codeName.s
Object codeName.o
a.out
![Page 40: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/40.jpg)
Compiling multi-file programsCompiling 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
![Page 41: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/41.jpg)
Compiling multi-file programsCompiling multi-file programs
bash$ gcc -c doin.c proc.c dout.cbash$ gcc doin.o proc.o dout.o –o pdsbash$ gcc -c proc.cbash$ gcc doin.o proc.o dout.o -o pds
![Page 42: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/42.jpg)
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’
![Page 43: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/43.jpg)
PostconditionsPostconditionsYou 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
![Page 44: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/44.jpg)
![Page 45: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/45.jpg)
• 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
![Page 46: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/46.jpg)
Memory: the picture so farMemory: 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
![Page 47: Functions and scope](https://reader033.vdocument.in/reader033/viewer/2022052702/568147b3550346895db4f8d9/html5/thumbnails/47.jpg)
Memory: the picture so farMemory: 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 tochs[0] nump
X_accuratechs[1]
chs[2]
chs[3]