-3

I have been trying to include the declaration of the variable in the function itself but it doesn't work unless I include it in the main function. Why does this happen?

#include<iostream>
using namespace std;



function1(int x)
{
int x =1;
cout << x << endl;
return 0;
}

int main ()
{
function1( x);
return 0;
}`  `
Kragon
  • 15

2 Answers2

0

Why does this happen?

It happens because x is now undefined in the context where you're using it in main, and it's already defined (as a parameter) in the context where you're trying to define it in function1().

In the first case, you'll definitely get an error because x as used in main is completely undefined... the compiler is going to look at that function1(x) call and wonder what the heck it's supposed to supply for the x.

In the latter case, the compiler might let you redefine x, but it'll probably at least issue a warning. Also, if it's allowed, then your function1() will always print out 1 regardless of what you pass in for the x parameter because the newly declared x will be used instead of the value passed in the parameter.

The concept that you're missing is called scope. Every variable has a scope, which is essentially the realm in which it's known. The scope of a variable can be global, in which case it's available to the entire program, or limited to a single file, or it can be declared inside a function or block, in which case it's local to that block of code.

int x = 12;   // x is available anywhere in the file and has initial value 12

void foo(int x)
{
    cout << x << endl;    // prints the value passed to foo by the caller
    int x = 34;           // this new x hides the parameter and is available
                          //   anywhere inside this function, but not outside
    cout << x << endl;    // prints the new x, i.e. 34
    {
        cout << x << endl;    // still prints 34
        int x = 97;           // hides the previous x, available only within
                              //   this block or sub-blocks
        cout << x << endl;    // prints 97
    }                         // x from the enclosed block goes out of scope
    cout << x << endl;    // prints 34, because we're back to the x at function scope
}

Understanding scope is very important because variable names are often duplicated, either on purpose or by accident, and you need to be able to tell where a given variable is in scope and therefore valid to use, and when it's not.

Caleb
  • 120,112
  • 19
  • 171
  • 259
0

Welcome to the world of C++ coding! Looks like there's more than a few issues in the code here - let's break it down and see what we can find.

First and foremost, to answer your original question, your declaration of x (in function1) was made outside of the function you tried to use the variable in (in main). C++ can't normally see variables you declare in one function when it's running in another; that's by design, and is called scope.

To start, the code won't compile for a number of reasons, first and foremost the presence of stray backticks in your code at the very end. These need to be removed.

int main ()
{
function1( x);
return 0;
}`  `  //<-- the `  ` will make the compiler angry

Now let's have a look at what's causing the error: x isn't yet declared. In C++, a variable has to be "declared" before it can be used. Since "x" hasn't been declared before its use in function( x);, the compiler kicks it back since it doesn't know what "x" means. Try this:

int x = 0;
function1( x);

We're not quite done yet, though. Once we make this change, the compiler will throw another error: In function 'int function1(int)': 8:5: error: declaration of 'int x' shadows a parameter. You've already included an int x in the definition of function1; by creating another int x inside function1, you've steamrolled your original x. Let's change that from a definition to an assignment.

function1(int x)
{
x =1;
cout << x << endl;
return 0;
}

Getting clsoer, but we've got one more error: 6:16: error: ISO C++ forbids declaration of 'function1' with no type [-fpermissive]. THis is telling you function1 needs to have a return type, which is a keyword in front of the function's name that tells the compiler what type of data it returns (void if it doesn't return any). Looks like you're using return 0; - why not return an int?

int function1(int x)

Now, at last, we've got code that compiles and runs.

#include<iostream>
using namespace std;



int function1(int x)
{
x =1;
cout << x << endl;
return 0;
}

int main ()
{
    int x = 0;
function1( x);
return 0;
}

Try it here!

Good luck!

Nick Reed
  • 5,029
  • 4
  • 14
  • 34