5 Replies - 10799 Views - Last Post: 20 August 2008 - 08:08 PM Rate Topic: -----

#1 baconbeastnz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 29-May 08

How to access a variable in a function from another function?

Posted 20 August 2008 - 04:03 AM

Sorry I don't know the terminology...

I'm getting the error . myObject is not declared in this scope..

fucntion init() {
Object myObject;

}

function draw() {
myObject.draw();

}
Is This A Good Question/Topic? 0
  • +

Replies To: How to access a variable in a function from another function?

#2 baavgai   User is online

  • Dreaming Coder
  • member icon


Reputation: 7233
  • View blog
  • Posts: 15,070
  • Joined: 16-October 07

Re: How to access a variable in a function from another function?

Posted 20 August 2008 - 04:30 AM

This doesn't look like C or C++, let's pretend it's a language called bacon.

For draw to have the instance of myObject referenced in "function init()", bacon might do something like this:
function draw(Object myObject) {
myObject.draw();
}


Was This Post Helpful? 1

#3 gabehabe   User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1433
  • View blog
  • Posts: 11,006
  • Joined: 06-February 08

Re: How to access a variable in a function from another function?

Posted 20 August 2008 - 04:30 AM

Because you create it in one function, the scope of it is only within that function. One way which you could do it is to pass "myObject" to your draw function, like so:

void init()
{
    Object myObject;
}

void draw (Object obj) // the things in the brackets are known as 'parameters'
{
    obj.draw (); // 'obj' is the object which we passed to the function
}

Hope this helps :)
Was This Post Helpful? 0
  • +
  • -

#4 baconbeastnz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 29-May 08

Re: How to access a variable in a function from another function?

Posted 20 August 2008 - 05:27 PM

Ah ok Thanks. I will end up having alot to pass in. Is there anyway to declare an object/variable in a function, and make it global so that I don't need to pass it into other functions to use it?
Was This Post Helpful? 0
  • +
  • -

#5 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: How to access a variable in a function from another function?

Posted 20 August 2008 - 06:55 PM

You can create global variables... but they are the devil's candy!!!

global variables tend to cause problems (because the changes may occur when they are not expected within the logic of the current function.)

To avoid the use of global functions here is some advice:
#1 A function should do one thing. i.e. it should be as simple as possible
and preform the action that its name implies and ONLY that action.

#2 You can use Pass by reference to return multiple values from a function.

#3 You can use a class to encapsulate data that can be shared among the methods of that class.

This post has been edited by NickDMax: 20 August 2008 - 08:29 PM

Was This Post Helpful? 0
  • +
  • -

#6 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,234
  • Joined: 14-September 07

Re: How to access a variable in a function from another function?

Posted 20 August 2008 - 08:08 PM

:lol: devil's candy--i like that description
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1