Local vs Global variables

1 / 27

# Local vs Global variables - PowerPoint PPT Presentation

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

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

## PowerPoint Slideshow about ' Local vs Global variables' - saxton

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 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

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

#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;}

#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

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 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 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 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 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 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 ::

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

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

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

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; }

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

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

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 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 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

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 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; }

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.

#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

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

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

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

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