Question

Scope  What is variable scope? What are the types of scope we can have in...

Scope
 What is variable scope? What are the types of scope we can have in programming?
 What type of scope does a variable have when it is defined within a sub-block?
 What is a global variable? How are they typically declared?
 To modify a global variable inside of a function, what statement must we have at the beginning
of the function’s body?
 Why is using global variables considered poor programming practice?
 What is a constant variable? Can we have true constant variables in Python?
 What is the recommended programming style for naming constant variables?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

What is variable scope? What are the types of scope we can have in programming?

A scope in programming language can be define as the region or the part and the scope of the variable is the region where the variable is accessed after the declaration. i.e it is the area where we can access the variable and if we try to access the variable other than access region compiler will flag an error message as variable out of scope message.

A scope of the variable can be Local , Global and formal parameters.

A variable scope is local when we are going to use in Inside a function or a block.

example:

main()

{

int x ; // local variable

printf(x);   // prints the vaue of x

}

function()

{

printf(x) // flags error... x is not defined..out of scope

}

It can be global when it is used Out of all functions.i.e accessible to all functions or throughout the program.

example:

int x ; // Global variable

main()

{

printf(x);   // prints the vaue of x

}

function()

{

printf(x) // prints the value of x

}

Variable can also be used as the formal parameters where it is used in the function parameters.

What type of scope does a variable have when it is defined within a sub-block?

A variable defined inside a sub-block can only be used within that block.i.e it remains local to that block only and we cannot access that variable outside that sub block.

What is a global variable? How are they typically declared?

Since a program has variables or we can say that the representation of the memory blocks and we can use those variables in our program. Now there are restrictions for using these variables which can be controlled by the developer as given access to them. An access can be Local to the function of the respected program and it can be global to the program. Those variables which can be used throughout the program without any hindrance are defined as global. They can be used from any block or the function of the program. Below is the global variable declaration for the simple program.

example:

int x ; // Global variable

main()

{

int y;

printf(x);   // prints the vaue of x

printf(y) // Local variable

}

function()

{

printf(x) // prints the value of x

}

To modify a global variable inside of a function, what statement must we have at the beginning
of the function’s body?

In order to modify the value of the variable inside a function please follow the below code. Here i have showed how to modify ht e value of the global variable from the local function of the program.

Why is using global variables considered poor programming practice?

The reason for this is that it can be accessed from everywhere and makes it difficult to remember that lastest value and from where it has been accessed. i.e it remains in no access control state, and Using global variables causes very tight coupling of code.

What is a constant variable? Can we have true constant variables in Python?

No, there is no constant keyword in python and you can not declare the variable as constant in python

If you are in a class, the equivalent would be:

class function(object):
    CONST_Variable= "XYZ"

if not, it is just

CONST_Variable = "XYZ"

What is the recommended programming style for naming constant variables?

Recommended programming style for naming a constant variable is same as the variable but the only difference is to put Const keyword before the data type followed by the name of the constant variable.

Add a comment
Know the answer?
Add Answer to:
Scope  What is variable scope? What are the types of scope we can have in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT