Docsity

Fundamentals of Programming: Variables, Scoping, Storage Classes, Inline Functions, Default Arguments, Slides of C programming

Partial preview of the text

Download Fundamentals of Programming: Variables, Scoping, Storage Classes, Inline Functions, Default Arguments and more Slides C programming in PDF only on Docsity!

Fundamentals of Programming

(FOP)

Attributes of a Variable

  • Variables have attributes

which include;

type, name, value, size

int x = 4; We know 4 bytes are reserved for int

and;

  • Scope
    • Where variable can be referenced in program
  • Storage class
    • How long variable exists in memory

Automatic Storage Class

  • Automatic storage class (auto & register)
    • Variable created when program enters block in which it is

defined

  • Variable destroyed when program leaves block
  • Local variables of functions are automatic
    • Automatic by default
    • keyword auto explicitly declares automatic
  • register keyword
    • Hint to place variable in high-speed register
    • Good for often-used items (loop counters)
    • Often unnecessary, compiler optimizes on its own
  • Specify either register or auto, not both
    • register int counter = 1;

Static Storage Classes

  • Static storage class
    • Variables exist for entire program
      • For functions, name exists for entire program
    • Not accessible outside one file, scope rules still apply
  • static keyword
    • Local variables
    • Keeps value between function calls
    • Only known in own function
  • extern keyword
    • Default for global variables/functions
      • Globals: defined outside of a function block
    • Known in any function that comes after it

28 cout << "local x in main's outer scope is " << x << endl; 29 30 useLocal(); // useLocal has local x 31 useStaticLocal(); // useStaticLocal has static local x 32 useGlobal(); // useGlobal uses global x 33 useLocal(); // useLocal reinitializes its local x 34 useStaticLocal(); // static local x retains its prior value 35 useGlobal(); // global x also retains its value 36 37 cout << "\nlocal x in main is " << x << endl; 38 39 return 0 ; // indicates successful termination 40 41 } // end main 42

43 // useLocal reinitializes local variable x during each call 44 void useLocal( void ) 45 { 46 int x = 25 ; // initialized each time useLocal is called 47 48 cout << endl << "local x is " << x 49 << " on entering useLocal" << endl; 50 ++x; 51 cout << "local x is " << x 52 << " on exiting useLocal" << endl; 53 54 } // end function useLocal 55

Automatic variable (local

variable of function). This is

destroyed when the function

exits, and reinitialized when

the function begins.

72 // useGlobal modifies global variable x during each call 73 void useGlobal( void ) 74 { 75 cout << endl << "global x is " << x 76 << " on entering useGlobal" << endl; 77 x *= 10 ; 78 cout << "global x is " << x 79 << " on exiting useGlobal" << endl; 80 81 } // end function useGlobal local x in main's outer scope is 5 local x in main's inner scope is 7 local x in main's outer scope is 5 local x is 25 on entering useLocal local x is 26 on exiting useLocal local static x is 50 on entering useStaticLocal local static x is 51 on exiting useStaticLocal global x is 1 on entering useGlobal global x is 10 on exiting useGlobal

This function does not declare

any variables. It uses the

global x declared in the

beginning of the program.

local x is 25 on entering useLocal local x is 26 on exiting useLocal local static x is 51 on entering useStaticLocal local static x is 52 on exiting useStaticLocal global x is 10 on entering useGlobal global x is 100 on exiting useGlobal local x in main is 5

1 // code 11 2 // Using an inline function to calculate. 3 // the volume of a cube. 4 #include 5 6 using std::cout; 7 using std::cin; 8 using std::endl; 9 10 // Definition of inline function cube. Definition of function 11 // appears before function is called, so a function prototype 12 // is not required. First line of function definition acts as 13 // the prototype. 14 inline double cube( const double side ) 15 { 16 return side * side * side; // calculate cube 17 18 } // end function cube 19

20 int main() 21 { 22 cout << "Enter the side length of your cube: "; 23 24 double sideValue; 25 26 cin >> sideValue; 27 28 // calculate cube of sideValue and display result 29 cout << "Volume of cube with side " 30 << sideValue << " is " << cube( sideValue ) << endl; 31 32 return 0 ; // indicates successful termination 33 34 } // end main Enter the side length of your cube: 3. Volume of cube with side 3.5 is 42.

1 // code 15 2 // Using default arguments. 3 #include 4 5 using std::cout; 6 using std::endl; 7 8 // function prototype that specifies default arguments 9 int boxVolume( int length = 1 , int width = 1 , int height = 1 ); 10 11 int main() 12 { 13 // no arguments--use default values for all dimensions 14 cout << "The default box volume is: " << boxVolume(); 15 16 // specify length; default width and height 17 cout << "\n\nThe volume of a box with length 10,\n" 18 << "width 1 and height 1 is: " << boxVolume( 10 ); 19 20 // specify length and width; default height 21 cout << "\n\nThe volume of a box with length 10,\n" 22 << "width 5 and height 1 is: " << boxVolume( 10 , 5 ); 23

Set defaults in function

prototype.

Function calls with some

parameters missing – the

rightmost parameters get their

defaults.

24 // specify all arguments 25 cout << "\n\nThe volume of a box with length 10,\n" 26 << "width 5 and height 2 is: " << boxVolume( 10 , 5 , 2 ) 27 << endl; 28 29 return 0 ; // indicates successful termination 30 31 } // end main 32 33 // function boxVolume calculates the volume of a box 34 int boxVolume( int length, int width, int height ) 35 { 36 return length * width * height; 37 38 } // end function boxVolume The default box volume is: 1 The volume of a box with length 10, width 1 and height 1 is: 10 The volume of a box with length 10, width 5 and height 1 is: 50 The volume of a box with length 10, width 5 and height 2 is: 100

Copyright Ā© 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley cout << "Called square with int argument: " << x << endl; return x * x; 1 // Fig. 3.25: fig03_25.cpp 2 // Using overloaded functions. 3 #include 4 5 using std::cout; 6 using std::endl; 7 8 // function square for int values 9 int square( int x ) 10 { 11 12 13 14 } // end int version of function square 15 16 // function square for double values 17 double square( double y ) 18 { 19 cout << "Called square with double argument: " << y << endl; 20 return y * y; 21 22 } // end double version of function square 23

Overloaded functions have

the same name, but the

different parameters

distinguish them.

Copyright Ā© 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley // calls int version er 7 is " << intResult e 7.5 is " << doubleResult 24 int main() 25 { 26 int intResult = square( 7 ); 27 double doubleResult = square( 7.5 ); // calls double version 28 29 cout << "\nThe square of integ 30 << "\nThe square of doubl 31 << endl; 32 33 return 0 ; // indicates successful termination 34 35 } // end main Called square with int argument: 7 Called square with double argument:

The square of integer 7 is 49 The square of double 7.5 is 56.

The proper function is called

based upon the argument (int

or double).