DOC PREVIEW
IUPUI CSCI 23000 - Functions Scope, Parameter Passing, Storage Specifiers

This preview shows page 1-2-3-4 out of 11 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1Slide 2Slide 3Scope RulesHeader FilesCalling Functions: Call by Value and Call by ReferenceSlide 7Storage ClassesSlide 9Slide 10AcknowledgementsDale RobertsCSCI 230Functions Scope, Parameter Passing, Storage SpecifiersDepartment of Computer and Information Science,School of Science, IUPUIDale Roberts, LecturerDale Roberts, [email protected]@cs.iupui.eduDale Roberts1 /* Fig. 5.12: fig05_12.c2 A scoping example */3 #include <stdio.h>45 void a( void ); /* function prototype */6 void b( void ); /* function prototype */7 void c( void ); /* function prototype */89 int x = 1; /* global variable */1011 int main()12 {13 int x = 5; /* local variable to main */1415 printf("local x in outer scope of main is %d\n", x );1617 { /* start new scope */18 int x = 7;1920 printf( "local x in inner scope of main is %d\n", x );21 } /* end new scope */2223 printf( "local x in outer scope of main is %d\n", x );2425 a(); /* a has automatic local x */26 b(); /* b has static local x */27 c(); /* c uses global x */28 a(); /* a reinitializes automatic local x */29 b(); /* static local x retains its previous value */30 c(); /* global x also retains its value */1. Function prototypes2. Initialize global variable3. Initialize local variable4. Initialize local variable in block5. Call functions6. Output resultsDale Roberts3132 printf( "local x in main is %d\n", x );33 return 0;34 }3536 void a( void )37 {38 int x = 25; /* initialized each time a is called */3940 printf( "\nlocal x in a is %d after entering a\n", x );41 ++x;42 printf( "local x in a is %d before exiting a\n", x );43 }4445 void b( void )46 {47 static int x = 50; /* static initialization only */48 /* first time b is called */49 printf( "\nlocal static x is %d on entering b\n", x );50 ++x;51 printf( "local static x is %d on exiting b\n", x );52 }5354 void c( void )55 {56 printf( "\nglobal x is %d on entering c\n", x );57 x *= 10;58 printf( "global x is %d on exiting c\n", x );59 }local x in outer scope of main is 5local x in inner scope of main is 7local x in outer scope of main is 5 local x in a is 25 after entering alocal x in a is 26 before exiting a local static x is 50 on entering blocal static x is 51 on exiting b global x is 1 on entering cglobal x is 10 on exiting c local x in a is 25 after entering alocal x in a is 26 before exiting a local static x is 51 on entering blocal static x is 52 on exiting b global x is 10 on entering cglobal x is 100 on exiting clocal x in main is 5Program OutputDale RobertsScope RulesScope RulesFile scope File scope Identifier defined outside function, known in all functionsIdentifier defined outside function, known in all functionsUsed for global variables, function definitions, function prototypesUsed for global variables, function definitions, function prototypesFunction scope Function scope Can only be referenced inside a function bodyCan only be referenced inside a function bodyUsed only for labels (Used only for labels (start:start:, , case:case: , etc.) , etc.)Block scope Block scope Identifier declared inside a block Identifier declared inside a block Block scope begins at declaration, ends at right braceBlock scope begins at declaration, ends at right braceUsed for variables, function parameters (local variables of function)Used for variables, function parameters (local variables of function)Outer blocks "hidden" from inner blocks if there is a variable with Outer blocks "hidden" from inner blocks if there is a variable with the same name in the inner blockthe same name in the inner blockFunction prototype scope Function prototype scope Used for identifiers in parameter listUsed for identifiers in parameter listDale RobertsHeader FilesHeader FilesHeader filesHeader filesContain function prototypes for library functionsContain function prototypes for library functions<stdlib.h><stdlib.h> , , <math.h><math.h> , etc , etcLoad with Load with #include <filename>#include <filename>#include <math.h>#include <math.h>Custom header filesCustom header filesCreate file with functions Create file with functions Save as Save as filename.hfilename.hLoad in other files with Load in other files with #include "filename.h"#include "filename.h"Reuse functionsReuse functionsDale RobertsCalling Functions: Call by Value and Call by ReferenceCalling Functions: Call by Value and Call by ReferenceUsed when invoking functionsUsed when invoking functionsCall by valueCall by valueCopy of argument passed to functionCopy of argument passed to functionChanges in function do not effect originalChanges in function do not effect originalUse when function does not need to modify argumentUse when function does not need to modify argumentAvoids accidental changesAvoids accidental changesExampleExample::addone(int);addone(int);main ()main (){{int i = 5, j;int i = 5, j;j=addone(i);j=addone(i);printf(“%d %d\n”,i, j);printf(“%d %d\n”,i, j); 5 65 6}}addone(int x)addone(int x){{return ++x;return ++x;}}Dale RobertsCall by reference (Passing Address)Call by reference (Passing Address)This is not actually call by reference, although some books called This is not actually call by reference, although some books called this ‘call-by-reference’this ‘call-by-reference’Passes original argumentPasses original argumentChanges in function effect original using Changes in function effect original using && operator to pass address operator to pass addressOnly used with trusted functionsOnly used with trusted functionsExampleExample::addone(int *);addone(int *);main ()main (){{int i = 5, j;int i = 5, j;j=addone(&i);j=addone(&i);printf(“%d %d\n”,i, j);printf(“%d %d\n”,i, j);6 66 6}}addone(int *x)addone(int *x){{return ++(*x);return ++(*x);}}For now, we focus on call by valueFor now, we focus on call by valueCalling Functions: Call by Value and Call by ReferenceCalling Functions: Call by Value and Call by ReferenceDale RobertsStorage ClassesStorage ClassesStorage class specifiersStorage class specifiersStorage duration Storage duration –– how long an object exists in memory how long an object exists in memoryScope Scope –– where object can be referenced in program where object can be referenced in programLinkage Linkage –– specifies the files in which an identifier is known (more in specifies the


View Full Document

IUPUI CSCI 23000 - Functions Scope, Parameter Passing, Storage Specifiers

Download Functions Scope, Parameter Passing, Storage Specifiers
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Functions Scope, Parameter Passing, Storage Specifiers and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Functions Scope, Parameter Passing, Storage Specifiers 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?