Jump to content

Recommended Posts

Hi

Is it possible to modify, from inside a function, a variable that was declared outside that function?

For example this code wont work:

void functionX()
{    
    y = 5;
}

int y;
functionX();

Is there a way to reference that variable inside the function?

 

Share this post


Link to post
Share on other sites
Guest
void functionX(int var)
{    
    var = 5;
}

int y;
printf(itoa(y) + "\n"); //printing out the current y value (0)

functionX(y); //we're now passing on the y variable into the function.

printf(itoa(y) + "\n"); //y should now be 5
int functionX()
{
	return 5;
}

int y = functionX();
printf(itoa(y));

Here's 2 methods of doing so.

Edited by Guest

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×