In: Advanced Math
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?
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.
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.