We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi
I've had some experience with JavaScript, and it uses functions in a traditional way to modularize code, so it is reusable. In JavaScript, if I wanted a function to add two numbers and print the result to the console, I'd have something like this:
function myAdder(a, b) {
var c;
c = a + b;
console.log(c);
}
Then later on I'd call it like this:
var n1 = prompt("Enter first number");
var n2 = prompt("Enter second number");
myAdder(n1, n2);
What's the equivalent of this in Processing?
Many thanks /Jason
Answers
https://forum.Processing.org/two/discussion/12532/windowjs-cross-mode-alert-confirm-prompt-other-js-api-for-java
Thanks - but I want to know how this concept is generally applied in Processing, not import or convert code. I'm not interest in JavaScript, but interested in writing modular code.
Do I have to make a class?
Thanks /Jason
http://studio.ProcessingTogether.com/sp/pad/export/ro.9$Bjf6i21oXBw
http://docs.Oracle.com/javase/8/docs/api/javax/swing/JOptionPane.html
Thanks.
The prompt was just an example. The concept I'm trying to understand is how to modularize code in Processing. It seems the only way to do it is to create a class and make objects with it? Can you create your own methods for the draw()?
For example, I have a bunch of code in Processing that I'm using for outputting a range of variable values to the console, to help me debug. I need to re-use this code in different places in the program. Right now I'm copy pasting the code into those places where I need it. This is just another example of an issue I keep running into.
It was a very bad 1 since nothing of that sort exists in Processing's API:
https://processing.org/reference/
If you wish you can encapsulate all of those utility functions in 1 or more classes.
Choose a
package
name for them.Then you can compile them using "javac.exe" so you can
import
them into your sketches.But that's outside the scope of Processing. It's actual Java stuff instead.
Processing Tutorial -7.0 Functions
https://www.youtube.com/playlist?list=PLRqwX-V7Uu6ajGB2OI3hl5DZsD1Fw1WzR
Certainly. Processing can define functions and let you call them from anywhere. This isn't really done in the examples because they are so simple but it is mentioned here: https://processing.org/reference/return.html
(Gotoloop's answer was more about reusing code between sketches. Barbara's I can't see because I'm on my phone, but I think you're asking a simpler question than that. Apologies if I'm wrong)
Thanks everyone for the help. Barbara's linked videos explained it perfectly. >Before, when I tried to do it, I had put my definition inside the draw function but it must go outside it.