Question

In: Advanced Math

a) In your own words, explain the concept of variable scope. Include one or more code...

a) In your own words, explain the concept of variable scope. Include one or more code fragments that you write for this discussion post to help illustrate your case. Clearly mark as appropriate any code fragments as either "script" code or "function" code, and use comments in your code to note the locality of each variable at least the first time the variable appears in the script or function. If a variable is local in scope, be sure to notate what the variable is local to (i.e. the script, a particular function, etc.). Your code for this discussion post should include at least two different local variables, and at least one global variable.

b) Compare and contrast separate function files with anonymous functions. In your opinion, when may it be best to use one over the other? Can you use them interchangeably in all circumstances?

Solutions

Expert Solution

A scope in any programming is a region of the program where a defined variable can have its existence and beyond that variable it cannot be accessed. There are three places where variables can be declared in C programming language.

  • Inside a function or a block which is called Local variables.
  • Outside of all functions which is called Global variables.
  • In the definition of function parameters which are called Formal parameters.

Scope of an identifier is the part of the program where the identifier may directly be accessible. In C, all identifiers are lexically (or statically) scoped. C scope rules can be covered under following two categories.

Global Scope: Can be accessed anywhere in a program.

// filename: file1.c
int a;
int main(void)
{
a = 2;
}

// filename: file2.c
// When this file is linked with file1.c, functions
// of this file can access a
extern int a;
int myfun()
{
a = 2;
}

To restrict access to the current file only, global variables can be marked as static.

Block Scope: A Block is a set of statements enclosed within left and right braces ({ and } respectively). Blocks may be nested in C (a block may contain other blocks inside it). A variable declared in a block is accessible in the block and all inner blocks of that block, but not accessible outside the block.

What if the inner block itself has one variable with the same name?
If an inner block declares a variable with the same name as the variable declared by the outer block, then the visibility of the outer block variable ends at the pint of the declaration by inner block.

#include<stdio.h>
int main()
{
{
   int x = 10, y = 20;
   {
       // The outer block contains declaration of x and y, so
       // following statement is valid and prints 10 and 20
       printf("x = %d, y = %d\n", x, y);
       {
           // y is declared again, so outer block y is not accessible
           // in this block
           int y = 40;
  
           x++; // Changes the outer block variable x to 11
           y++; // Changes this block's variable y to 41
  
           printf("x = %d, y = %d\n", x, y);
       }

       // This statement accesses only outer block's variables
       printf("x = %d, y = %d\n", x, y);
   }
}
return 0;
}

b)

What about functions and parameters passed to functions?
A function itself is a block. Parameters and other local variables of a function follow the same block scope rules.

Can variables of the block be accessed in another subsequent block?*
No, a variable declared in a block can only be accessed inside the block and all inner blocks of this block. For example, the following program produces a compiler error.

int main()
{
{
   int x = 10;
}
{
   printf("%d", x); // Error: x is not accessible here
}
return 0;
}

we cannot use local and global scope interchanagabley .

Real Life example for scope:

If you know how to drive a car and bike only and I ask you what is your scope in driving, you will say I only know car and bike. So that means you are limited to car and bike. That means your scope is local.
If you know how to drive all the available transports and I ask you what is your scope in driving, you will say I can drive anything. That means your scope is global as you can drive anything.


Related Solutions

explain the scope of financial management by your own words!
explain the scope of financial management by your own words!
Explain in your own words the concept of the sensitivity analysis.
Explain in your own words the concept of the sensitivity analysis.
In your own words, explain, the difference, between the concept of ANOVA/ANCOVA with the concept of...
In your own words, explain, the difference, between the concept of ANOVA/ANCOVA with the concept of MANOVA/MANCOVA. Include in your explanation, what type of research question one might use the statistical concepts over the other. Include in your explanation the advantage and disadvantage of each.
explain in your own words the concept of the time value of money.
explain in your own words the concept of the time value of money.
Provide in your own words one example of the concept of counterculture in Canada and your...
Provide in your own words one example of the concept of counterculture in Canada and your own country, from personal experience or observations in the media, books or movies. Be sure that your examples prove clearly you understand the definition.
In your own words, explain the concept of corporate entrepreneurship. Under this concept, what is expected...
In your own words, explain the concept of corporate entrepreneurship. Under this concept, what is expected of the corporation? What is expected of the employees in such an entrepreneurial organization? Do you have the knowledge, skills and attitude to behave like an “entrepreneur” within a corporation (a corporate entrepreneur)? Explain why so? Big Bang Disruption is affecting the fields of Education, Healthcare, Transportation (Taxi Services) and Travel -Lodging (Hotels). Explain with examples of new businesses (or new ventures).
Research the concept of “externalities” and explain in your own words, the idea of an externality....
Research the concept of “externalities” and explain in your own words, the idea of an externality. (The Wikipedia entry on externalities is a good source. Skip the mathematical definition.) (15 points) List five examples of externalities, preferably from engineering projects, processes, inventions, or products. You must be specific, - what is the technology or process and what is the resulting social impact. Do not cut and paste or copy from Wikipedia. Be creative and interesting with your examples. Your list...
Explain in your own words the concept of the sensitivity analysis in 2 paragraphs.
Explain in your own words the concept of the sensitivity analysis in 2 paragraphs.
Explain, in your own words, the concept of the time value of money in a relationship...
Explain, in your own words, the concept of the time value of money in a relationship to both the simple direct investment and timing for maximizing the value of a corporations excess cash as well as for cash flows of projects and capital/project investment analysis. Be sure to discuss the limitation of time value analysis in terms of the component financial data points used in the underlying formulas and financial analysis results.
in 300 hundred words or more, please write, in your own words, about one of the...
in 300 hundred words or more, please write, in your own words, about one of the entities that establish the rules accountants must follow when preparing financial reports. they include, but are not limited to, the Financial Accounting Standards Board (FASB), International Accounting Standards Board (IASB), and the Security Exchange Commission (SEC). please also state your opinion wether the mission of the organization you are writing on helps to provide guidance to aacountants or is repetitive of other standard-making bodies....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT