We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Passing a variable from one function to another
Page Index Toggle Pages: 1
Passing a variable from one function to another (Read 599 times)
Passing a variable from one function to another
Oct 16th, 2009, 12:11pm
 
Hello everyone,

I have been trying to understand this code that accesses the twitter API. I have looked at the posts about the twitter API but I am having this problem.

Obviously I am not a good programmer, but I am learning. Here is what I have.

What I am trying to do is have a function that searches the twitter api and stores the results in a variable.

Then call that function, and have it return it's results to another function that formats and displays the results.

in psudocode:

void setup()
// Stuff to setup

void search()
//perform search and load results into array

void display()
//read results from search() and display with println

void draw()
search()
display()

For some reason I can not pass the results from one function to another. I am trying to get my head around this style of programming. The error is Cannot find anything named user, I assume this means it can't see the results from the other function. Thanks.

- Shawn


Quote:
Twitter myTwitter = new Twitter();
 
void setup() {
  size(800,600);
  background(100);
  noLoop();
}
void search() {
  try {
 
    Query query = new Query("sandwich");
    query.setRpp(10);
    QueryResult result = myTwitter.search(query);
 
    ArrayList tweets = (ArrayList) result.getTweets();
 
    for (int i = 0; i < tweets.size(); i++) {
      Tweet t = (Tweet) tweets.get(i);
      String user = t.getFromUser();
      String msg = t.getText();
      Date d = t.getCreatedAt();
      //println(user + " " + msg + " " + d);
      
      }
    }
  catch (TwitterException te) {
    println("Couldn't connect: " + te);
  }
}

 
void draw() {
  search();
  println("Tweet by " + user); // Get error here <---
 
}



Re: Passing a variable from one function to another
Reply #1 - Oct 16th, 2009, 1:34pm
 
a variable's scope is defined by the chunk of code it's declared in.  If you want a global var, it needs to be declared outside of all functions...(local in that case to the PApplet class, global enough for us).  So you would declare "String user" outside of setup(), at the top of your sketch, and fill it (define its contents) during setup().  Then draw would have access to it.

But, you'll hit another problem: because your loop continually redefines user for each t object, only the last one will take effect.  For now, an array of users[] with a length equal to your t[] length will help, but in the long run you might want to make a class that holds all those variables for each 't'.
Page Index Toggle Pages: 1