17

Scope of variables

Embed Size (px)

Citation preview

Page 1: Scope of variables
Page 2: Scope of variables

Anu

[email protected]

www.facebook.com/Anu Sasidharan

twitter.com/Anu

in.linkedin.com/in/Anu

9400892764

SCOPE OF VARIABLES IN C

Page 3: Scope of variables

Disclaimer: This presentation is prepared by trainees ofbaabtra as a part of mentoring program. This is not officialdocument of baabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd

Page 4: Scope of variables

•Region of the program where a defined variable can

have its existence .

•3 places where variables can be declared in C

programming language: Inside a function or a block -local variables

Outside of all functions - global variables

In the definition of function parameters-formal parameters.

What Actually Scope Of A Variable Mean??

Page 5: Scope of variables

What are local variables ???

•It exists only inside the specific function that creates them.

•They are unknown to other functions and to the main

program.

•Local variables cease to exist once the function that created

them is completed.

•They are recreated each time a function is executed or

called.

Page 6: Scope of variables

EXAMPLE

int sum(int a, int b)

{

int tot;

tot=a+b;

}

Page 7: Scope of variables

•These variables can be accessed by any function

comprising the program.

•They do not get recreated if the function is recalled.

What are global variables

???

Page 8: Scope of variables

EXAMPLE

#include<stdio.h>

int sum(int, int);

int tot;

main()

{

int a=10,b=5;

tot=sum(a,b);

}

sum(int c, int d)

{

tot=c+d;

return tot;

}

Page 9: Scope of variables

#include <stdio.h>

int sqr( int x ) ;

main()

{

int i , j ;

sqr( i ) ;

printf( "%d %d\n", i, j ) ;

}

int sqr( int x )

{

j = x * x ; }

SIMPLE EXAMPLE ILLUSTRATING

SCOPE OF VARIABLES

Page 10: Scope of variables

CORRECT METHOD

#include <stdio.h>

int sqr( int x ) ;

main()

{

int i , j ;

sqr( i ) ;

printf( "%d %d\n", i, j ) ;

}

int sqr( int x )

{ int j;

j= x * x ; }

Page 11: Scope of variables

LOCAL VARIABLES Vs

GLOBAL VARIABLES

Using local variables allows to focus attention on smaller more

manageable pieces of the program. Globals forces to keep track

of how they're being used throughout the entire system.

While working with other people, you must coordinate who's

creating which global variables. It is not valid to create two

globals of the same name.

The effects of algorithm steps reduces the cases of unexpected

behavior.

Localizing the effects of algorithm steps makes the program

easier for others to understand.

Page 12: Scope of variables

If the two functions need to communicate, then we

generally need global variables.

If the entire program is built around some central data

and if access to that data is needed in nearly every

function and particularly if that is a large data structure,

then it makes sense to make such data global.

When do we use global variables ???

Page 13: Scope of variables

A Final Implementation with an Example#include <stdio.h> int f1( void ) ;int f2( int x, int a ) ;int a ; main() { int a, b, c ;

a = 7 ; b = f1() ; c = f2( a, b ) ;printf( "%d\n %d\n %d\n", a, b, c ) ;

}int f1( void ){ a = 12 ;

printf( "%d\n ", a ) ; return( a + 5 ) ; } int f2( int x, int a ) { printf( "%d\n ", a ) ; return( x * a ) ; }

OUTPUT1217717119

a=7,12a=7,12,7b=17x=7a=7,12,7,17c=119

Page 14: Scope of variables

CONCLUSION

Scope of variable was explained. It defines the existence or

the availability of variables in a program.

Types of scope was discussed. Scope depends upon the type

of variable declaration.

Global and Local variables were briefly explained.

Page 15: Scope of variables

Want to learn more about programming or Looking tobecome a good programmer? Are you wasting time on searchingso many contents online? Do you want to learn things quickly? Doan online course @ baabtra.com. We provide development, testingand production environments just like in the industry to practice.The courses are so structured to make a quick, good softwareprofessional.

Page 16: Scope of variables

Follow us @ twitter.com/baabtra

Like us @ facebook.com/baabtra

Subscribe to us @ youtube.com/baabtra

Become a follower @ slideshare.net/BaabtraMentoringPartner

Connect to us @ in.linkedin.com/in/baabtra

Thanks in advance.

www.baabtra.com | www.massbaab.com |www.baabte.com

Page 17: Scope of variables

Contact Us

Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

Cafit Square,Hilite Business Park,Near Pantheerankavu,Kozhikode

Start up VillageEranakulam,Kerala, India.

Email: [email protected]