Using global variable as formal argument of function gives error but if I use global variable inside the function it not gives error?

  arguments, c++, global-variables

1

Code

#include<iostream>
int a=4;

void fun(int);
int main()
{
    fun(7);
}
void fun(a)
{
    std::cout<<a;
}

Output

[Error] variable or field 'add' declared void

2

Code

#include<iostream>
int a=4;

void fun(int);
int main()
{
    fun(7);
}
void fun(int b)
{
    a=b;
    std::cout<<a;
}

Output

7

If I am using global variable as parameter it gives error but if I use same global variable inside the function rather than it’s parameters it works fine.

I think function fun() has to access global variable in any situation rather we use it at parameter place or inside the function so why is such difference ?

Source: Windows Questions C++

LEAVE A COMMENT