Fill This Form To Receive Instant Help
Homework answers / question archive / Global Variable Parameters The parameter list is the normal means to supply values to a function
Global Variable Parameters
The parameter list is the normal means to supply values to a function. Global variables are an alternate means for a function to have access to values. Describe what a global variable is, how it is declared, and then discuss the pros and cons of combining global variables and functions.
Global variables are the variables that has the global scope i.e. they can be accessed from any function that the program comprises. These variables are declared outside all the function definitions including the main() function and are accessible from the place of declaration till the end of the program. And any changes made to these variables remain consistent (effective) throughout the program execution. When the local variable name coincides with that of the global variable, then the global variable is ignored.
For example,
#include<stdio.h>
function aa(); /*prototype function declaration*/
int a,c;
function bb()
{
c=10;
a=10; /*valid*/
b=10; /*Invalid as b is still unknown to this function*/
}
int b; /*global variables*/
function aa()
{
int c;
c=1;
a=10;b=10; /*absolutely valid*/
}
void main()
{
a=1;
b=1;
aa(); /*a=10*/
a++; /*a=11*/
bb(); /*c=10*/
aa(); /*c=10*/
}
Global variables avoid passing multiple times to the functions as parameters and decreases the length of the program. These variables allows to execute the program faster and in some cases improves the understanding of the code.
But, these variables often confuse the programming beginners when encountered with local variables with the same name as that of global variables. It is highly recommended to avoid using the global variables as it reduces the structured programming philosophy and modular design of the code.