Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function with in a function #46

Closed
MarcLenahan opened this issue Oct 12, 2017 · 3 comments
Closed

Function with in a function #46

MarcLenahan opened this issue Oct 12, 2017 · 3 comments

Comments

@MarcLenahan
Copy link

Does anyone know if a function can be placed inside a function. I want to make it so that if a cursor is on the right side of the window and pressed it would change the color of the right side of the window. This would use a mousePressed function and an if/else statement.

@michaelmusick
Copy link
Member

michaelmusick commented Oct 12, 2017 via email

@allenharguess701
Copy link

allenharguess701 commented Oct 12, 2017 via email

@quintinbruderer
Copy link

quintinbruderer commented Oct 16, 2017

There is a lot going on in here...take your time and read this thoroughly.
If this doesn't make sense yet, it soon will. 👅
Since I can't do "Live Demos", or have the effort to figure it out right now, just copy paste this code into an empty example.

You actually already are putting functions within functions. All your functions used (like fill, shapes, noCursor, etc) are living in other functions, like setup and draw.


//***************
// creating 3 functions. Functions, within functions (within functions)


// doSomething draws 2 circles. Simple. It uses the 2 passed in functions...
// ... to create the fills. Not so simple.

function doSomething(colorChooser, greyScaler) {
  console.log(colorChooser()) // check the console and see the 'string' result
  fill(colorChooser());
  ellipse(20,20,20);

  console.log(greyScaler()); // check the console and see the integer produced
  fill (greyScaler())
  ellipse(40,40,20);
};

// colorChooser will create a 'string' of an rgb value. (Probably could be done better)
// the string will look something like rgb(40,200,140)
function colorChooser() {
  return 'rgb(' + floor(random(255)) + ',' + floor(random(255)) + ',' + floor(random(255)) + ')'
};

// greyScaler will "divide"/multiply 255 (the fullest color range) by a decimal.

function greyScaler() {
  return floor(255 * (random(1)));
};

//***************

// now back to confort area
function setup () {
  createCanvas(300,500);
  // we have to pass the doSomething function the 2 other functions as arguments. Notice without () parenthesis.
  // ...Because we dont want to call them, just pass them. Earlier we use them, as we want to call them for their returned result
  doSomething(colorChooser, greyScaler);

}

function draw () {
  // cause green is the coolest color. And to show a regular drawn circle.
  // do realize we could call doSomething here instead of setup too.
  // if we wanted to go even more crazy and animate/strobe the doSomething circles on each loop.
  fill (0,255,0);
  ellipse (60, 60, 20);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants