local vs global variables
Download
Skip this Video
Download Presentation
Local vs Global variables

Loading in 2 Seconds...

play fullscreen
1 / 27

Local vs Global variables - PowerPoint PPT Presentation


  • 66 Views
  • Uploaded on

Local variable Known only to the function in which it is declared Cannot be accessed outside the function. Local vs Global variables. Global variable Known to all functions in the same file Can be known to other functions in other files

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Local vs Global variables' - saxton


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
local vs global variables
Local variable

Known only to the function in which it is declared

Cannot be accessed outside the function

Local vs Global variables
local vs global variables1
Global variable

Known to all functions in the same file

Can be known to other functions in other files

Cannot be accessed inside a function if that function has a variable by the same name, unless the scope resolution operator is used

Local vs Global variables
slide3

#include <iostream.h>void Func(void);

void main( )

{ int Num = 1; // Num is a local variable

cout << “in main, Num is”

<< Num << endl; Func( );

cout << “back in main, Num is still” << Num; }

void Func(void)

{ int Num = 20; // Num is a local variable

cout << “In Func, Num is” << Num << endl;}

slide4

#include <iostream.h>void Func(void);

void main( )

{ int Num = 1; // Num is a local variable

cout << “in main, Num is”

<< Num << endl; Func( );

cout << “back in main, Num is still” << Num;

}

void Func(void)

{ int a = 20; // Num is not known here

cout << “In Func, Num is” << Num << endl;}

global variables
Global variables

Variables that are defined outside a function

2

void Func( ); // function prototype

int Num =2; // global variable

void main( )

{ cout << “In main, Num is” << Num << endl;

Func();

cout << “Back in main Num is ” << Num ;

return 0; }

// the first cout displays 2 for Num

Num

global variables1
Global variables

Variables that are defined outside a function

Global above main

2

Num

void Func( )

{ cout << “In Func, Num is” << Num << endl;

Num = 50;

cout << “But, it is now changed to” << Num; }

// the first cout displays 2

// the second cout displays 50

global variables2
Global variables

Variables that are defined outside a function

50

Num

void Func( )

{ cout << “In Func, Num is” << Num << endl;

Num = 50;

cout << “But, it is now changed to” << Num; }

// the first cout displays 2

// the second cout displays 50

global variables3
Global variables

Variables that are defined outside a function

void Func( ); // function prototype

int Num =2; // global variable

void main( )

{ cout << “In main, Num is” << Num << endl;

Func();

cout << “Back in main Num is ” << Num ;

return 0; }

// the first cout displays 2 for Num

// the second cout displays 50 for Num

50

2

Num

global variables4
Global variables

Variables that are defined outside a function

void Func( ); // function prototype

void main( )

{ cout << “In main, Num is not visible”;

Func();

cout << “Back in main Num is not visible” ;

return 0; }

int Num =2; // global variable defined between

// main and Func

global variables5
Global variables

Variables that are defined outside a function

int Num =2; // global variable defined between

// main and Func

void Func( )

{ cout << “In Func, Num is” << Num << endl;

Num = 50;

cout << “But, it is now changed to” << Num; }

// the first cout displays 2

// the second cout displays 50

scope resolution operator
Scope resolution operator ::

int Val = 1;

void main( )

{ int Val = 100;

cout << “The local variable is set to ”

<< Val << endl; // displays 100

cout << “The global variable is set to ”

<< ::Val ; // displays 1

slide12

Scope resolution operator ::

int Val = 1;

void main( )

{ int Val = 100;

cout << “The local variable is set to ”

<< Val << endl; // displays 100

cout << “The global variable is set to ”

<< ::Val; // displays 1

storage classification of variables
auto default type, automatic variables are the ones we’ve have been using

extern tells the computer that the variable is defined elsewhere in the program

register tells the computer to use one of the CPU’s registers

static persists (it’s value remains) even after leaving function

Storage classification of variables
slide14

double Value = 3.5;

void main( )

{ extern double Value; // unnecessary in this use

cout << “The value is” << Value << endl;

register int Number; // use arithmetic/logic unit

for (Number =2; Number <=120; Number*=2)

cout << Number << endl; }

slide15

void ShowLocal(void);

void main( )

{ ShowLocal( );

ShowLocal( );

return 0;

}

void ShowLocal(void)

{ int LocalNum=5;

cout << “localNum is “ << LocalNum << endl;

LocalNum = 99; } // displays 5 always

slide16

void ShowStatic(void);

void main( )

{ for (int Count=0; Count < 5; Count++)

ShowStatic( ); // function is called 5 times

}

void ShowStatic(void)

{ static int StatNum=5; // initialized only once

cout << “StatNum is “ << StatNum << endl;

StatNum ++; } // value persists

// 5 6 7 8 9 will display for StatNum value

default arguments
Default arguments

void DisplayStars(int = 10, int =1);

void main( )

{ DisplayStars( ); cout << endl;

DisplayStars(5); cout << endl;

DisplayStars(7,3); }

void DisplayStars(int Cols, int Rows)

{ for (int Down = 0; Down < Rows; Down++)

{ for (int Across=0; Across < Cols;Across++)

cout << ‘*’;

cout << endl; } }

default arguments1
Default arguments

void DisplayStars(int = 10, int =1);

void main( )

{ DisplayStars( ); cout << endl;

DisplayStars(5); cout << endl;

DisplayStars(7,3); }

void DisplayStars(int Cols, int Rows)

{ for (int Down = 0; Down < Rows; Down++)

{ for (int Across=0; Across < Cols;Across++)

cout << ‘*’;

cout << endl; } }

default arguments2
Default arguments

void DisplayStars(int = 10, int =1);

void main( )

{ DisplayStars( ); cout << endl;

DisplayStars(5); cout << endl;

DisplayStars(7,3); }

void DisplayStars(int Cols, int Rows)

{ for (int Down = 0; Down < Rows; Down++)

{ for (int Across=0; Across < Cols;Across++)

cout << ‘*’;

cout << endl; } }

returning a value from a function
Returning a value from a function

A function may return a value back to the part of the program that called the function by using a return statement

Argument

Argument

Argument

Argument

Return Value

Function

returning a value from a function1
Returning a value from a function

int Square (int); // function prototype

void main(void)

{ int Value, Result;

cout << “Enter a number and I will square it:”;

cin >> Value;

Result = Square (Value);

cout << Value << “squared is” << Result;}

int Square (int Number)

{ return Number * Number; }

slide22

Write a program that asks the user to enter a number and calls a function which receives the number. The purpose of the function is to determine if the number is even or odd. The function will return a 0 or false if the number is odd or a 1 or true if the number is even.

slide23

#include <iostream.h>

bool IsEven(int);

void main( )

{ int Val;

cout << “Enter an integer”;

cin >> Val;

if (IsEven(Val))

cout << Val << “is even\n”;

else

cout << Val << “is odd\n”; }

0 or 1

slide24

5

Val

5

bool IsEven(int Number)

{ if ( Number % 2 )

return 0;

else

return 1;

}

Number has a copy of whatever is inside Val in main

slide25

5

Val

5

bool IsEven(int Number)

{ if ( Number % 2 )

return 0;

else

return 1;

}

Number has a copy of whatever is inside Val in main

5/2 is 2 with a remainder of 1

slide26

5

Val

5

bool IsEven(int Number)

{ if ( Number % 2 )

return 0;

else

return 1;

}

Number has a copy of whatever is inside Val in main

5/2 is 2 with a remainder of 1

slide27

36

Val

36

bool IsEven(int Number)

{ if ( Number % 2 )

return 0;

else

return 1;

}

Number has a copy of whatever is inside Val in main

36/2 is 18 with a remainder of 0

ad