# Calling a function that returns array stops printArray

Try uncommenting grabline’s (r[]=details;) line.

I haven’t been instructed where to place things really. Any help?

``````int ws=6;
int ls=6;
int sx=0;
int ex=findex();
int myline[];

void setup() {
background(255);
size(640,640);
myline = new int[ls+1];
myline=grabline();
}

void draw() {
for(int i=0;i<ws;i++){
for(int j=0;j<ls;j++){
fill(255);
rect(100+ i*10,100+ j*10, 10,10);
}
}
}

int findex(){
int result=-1;
result+=(ws*ls);
return result;
}

int[] details(int nom, int smallw){

int[] r;
r=new int[6];
r[0]=nom;
r[1]=smallw;
// denominator will always go in once to larger number above
for(int total=smallw;total<=nom;total+=smallw){
// how many w go into nom
r[2]++;
r[3]=total+smallw;
r[4]=total;
}
// find Error in total+smallw
int nearzero=greater(r[0],r[3]) - lesser(r[0],r[3]);
// find error in total
int nearzero2=greater(r[0],r[4]) - lesser(r[0],r[4]);
r[5]=lesser(nearzero, nearzero2);
// if E better when total+smallw best, add 1 to R
if(nearzero<nearzero2){
r[2]++;
}
return r;
}

int greater(int nominator, int denominator){
int g=0;
if(nominator>denominator){
g=nominator;
}
if(denominator>=nominator){
g=denominator;
}
return g;
}

int lesser(int nominator, int denominator){
int result=0;
if(nominator<denominator){
result=nominator;
}
if(denominator<=nominator){
result=denominator;
}
return result;
}

int w(int l, int w){
if(l>w){
return l-w;
}
if(w>l){
return w-l;
}
return 0;
}

int l(int sx,int ex){
int c=ex;
int L=0;
while(c>=sx){
c-=ws;
L++;
}
return L;
}

int d(int sx,int ex){
int L=0;
int c=ex;
int result=0;
while(c>=sx){
c-=ws;
L++;
}
result=ex-(ws*(L-1)-sx);
return result;
}

int[] grabline(){
int mylength=l(sx,ex);
int mywidth=d(sx,ex)+1;
int mydepth=d(sx,ex);
int longside=greater(mylength,mywidth);
int shortside=lesser(mylength,mywidth);
int w=w(longside,shortside);
int nom=greater(shortside,w);
int denom=lesser(shortside,w);
//int r[]=details(nom,denom);
int subtract=ex;
int depth=mydepth;
int x=0;

printArray(myline);

return myline;
}
``````
1 Like

ha, that’s good
so what you do in that kind of situation is

• -a- use println(""); on variables
• -b- disable lines // or parts of code /* */
until it works to dig deeper into
and find the wrong line of code.

``````  println("nom "+nom+" denom "+denom);
int r[]=details(nom,denom);

//shows:
//nom 6 denom 0

``````

now inside

``````int[] details(int nom, int smallw) {
//..
// you do a for loop
for (int total=smallw; total<=nom; total+=smallw) { }

``````

that can not work good on

total+=0

so a easy fix is

``````  if (smallw>0) for (int total=smallw; total<=nom; total+=smallw) {

``````
2 Likes

Sorry kll, thanks for the quick reply (I was busy in a group setting when I posted), but I don’t see why a for loop is needed?
One other question… Where can " myline = grabline() " go without being in draw() or setup()? I tried placing a function call before setup but it does not run.
I did move " r = new int[6] " into setup to make it global to the program, just to clean it up.
Thanks for the compliment I’m working on designing a Master System type console with chips and wires… Processing will be my learning aid.

ahm, i did not check your code or try to understand
as you not say what it is for, and as it only prints the rectangles
( what can be done in one line ) i can not guess what the rest is for.
where you get it from?

It is my own creation that I scribble down on notepads… I think it is similar to the way a program like SDL will draw to the screen very quickly. What happens is I get basic details of a rectangle, I take the shorter side, minus it from the greater side, get the difference (w) … I then have a ratio between how many pixels left to how many pixels up I should be drawing. I can find the size of the Width and Length of this rectangle by being given the start point and the end point only.
When you have different sized rectangles, the ratio R is changing. Say for instance a rectangle 10 high, 5 wide. 10 - 5 is 5… if you drew diagonal line across from the bottom… it would take 5 extra pixels upwards to reach the top. If the rectangle were 16 by 3… that’s 13 extra pixels up… you divide 13 by 3 to get R (4 with a remainder of 1), this ends up saying every time you go across… move up 4 times.

I am still working on a better version. The remainder (or error E as I call it) may not be the suitable option. A int() or adding up the fractions gives an accurate lines with evenly spaced pixels in between.

grabline() is where it will figure out what pixel numbers need to be drawn on
0,1,2,3
4,5,6,7
8,9,0,1
2,3,4,5

and details are the ratios of the rectangle.

Get me some wires a breadboard and some counter chips… I could be drawing lines on a monitor : )

That would be cool!

so i try to help you with that code ( still without understanding the purpose )
by put in a diagnostic line at each function,

• so you know when it is executed
• you know what is returned

and i hope it helps you with your project

code
``````// https://discourse.processing.org/t/calling-a-function-that-returns-array-stops-printarray/6108

int ws=6, ls=6;
int sx=0;
int ex=findex();
int myline[] = new int[ls+1];
int posx = 100, posy = 100, w = 20;                // rect grid settings
boolean dbug = true;                               // print each function result

void setup() {
background(255);
size(640, 640);
for (int i = 1; i<ls+1; i++) myline[i]=i;          // set in some numbers as test ??
myline=grabline();                                 // this overwrites it? but gives same, see print
}

void draw() {
for ( int i =0; i<ls*ws; i++)  rect(posx+w*(i%ws), posy+w*floor(i/ws), w, w);
}

int findex() {
int result=-1;
result+=(ws*ls);
if (dbug) println("findex_result: "+result);
return result;
}

int[] details(int nom, int smallw) {
int[] r = new int[6];                              // keep local
r[0]=nom;
r[1]=smallw;
// denominator will always go in once to larger number above
if (smallw>0) for (int total=smallw; total<=nom; total+=smallw) {
// how many w go into nom
r[2]++;
r[3]=total+smallw;
r[4]=total;
}
// find Error in total+smallw
int nearzero=greater(r[0], r[3]) - lesser(r[0], r[3]);
// find error in total
int nearzero2=greater(r[0], r[4]) - lesser(r[0], r[4]);
r[5]=lesser(nearzero, nearzero2);
// if E better when total+smallw best, add 1 to R
if (nearzero<nearzero2) {
r[2]++;
}
if (dbug) {
println("details_r: ");
println(r);
}
return r;
}

int greater(int nominator, int denominator) {
int g=0;
if (nominator>denominator) {
g=nominator;
}
if (denominator>=nominator) {
g=denominator;
}
if (dbug) println("greater_g: "+g);
return g;
}

int lesser(int nominator, int denominator) {
int result=0;
if (nominator<denominator) {
result=nominator;
}
if (denominator<=nominator) {
result=denominator;
}
if (dbug) println("lesser_result: "+result);
return result;
}

int w(int l, int w) {
if (l>w) {
if (dbug) println("w_: "+(l-w));
return l-w;
}
if (w>l) {
if (dbug) println("w_: "+(w-l));
return w-l;
}
if (dbug) println("w_: 0");
return 0;
}

int l(int sx, int ex) {
int c=ex;
int L=0;
while (c>=sx) {
c-=ws;
L++;
}
if (dbug) println("l_L: "+L);
return L;
}

int d(int sx, int ex) {
int L=0;
int c=ex;
int result=0;
while (c>=sx) {
c-=ws;
L++;
}
result=ex-(ws*(L-1)-sx);
if (dbug) println("d_result: "+result);
return result;
}

int[] grabline() {
int mylength=l(sx, ex);
int mywidth=d(sx, ex)+1;
int mydepth=d(sx, ex);
int longside=greater(mylength, mywidth);
int shortside=lesser(mylength, mywidth);
int w=w(longside, shortside);
int nom=greater(shortside, w);
int denom=lesser(shortside, w);
int r[]=details(nom, denom);
int subtract=ex;
int depth=mydepth;
int x=0;
if (dbug) println("grabline_myline:");
printArray(myline);
return myline;
}

``````

How can I copy the code in your box? I’m on a laptop… or even left clicking and scrolling down seems awkward.

yes, the </> code box is not that nice / easy handling
i also often get stuck / try several times top bottom / bottom top
keep mouse pressed even add use arrow keys…
to get the whole thing selected for [ctrl[c]

worst case select the first line by mouse,then

press shift and go down by arrow key

or select first line by mouse,
go down to last line
hold shift key
and click end of last line.

some other forum with code entry have a [copy] button

1 Like

I see you take the values of each function. I’ve not been over beginner or novice level to do unittest or debugging yet, so you are my introduction to them.

This was working last week. Calling details is messing it up. I thought it was due to incorrect syntax.

it is a simple print before a function returns a value:
“functionname_variablename: variablevalue”

all can switch of by the top definition change to:

``````boolean dbug = false;
``````

as you nest so many functions it might help

Are you saying nesting functions is a bad idea?
Say I had to do some calculations for a person.
I’m not on my laptop so am half writing this code…

``````int[] getPerson(){
new Array called PersonArray size[4];

PersonArray[0] = getHeight();
PersonArray[1] = figureBMI();
PersonArray[2] = getAgeOfDeath();
PersonArray[3] = morgageCost();

return PersonArray;
}
``````

What other way could you grab a load of information for ‘somebody’ when you create them without doing a bunch of functions in a row? Just curious…

``````import java.util.Date;

class Person {
float height, bmi, morgage;
Date death;
}
``````

Sorry GoToLoop to be a 'but’er…. but if I had to call functions to figure out these values… I’ve placed multiple functions inside a function to grab all this information. Just wondering if there was another way.

This is called inline functions I think…
I’ll just have to make this program pretty clunky to start off with and improve over the years

Currently you are writing lots of top-level functions in order to create flow controls that will do what you want.

@GoToLoop is suggesting that at some point in the future you may want to use Object Oriented Programming to organize your function into classes. See for example the tutorial “Objects”:

https://processing.org/tutorials/objects/

As @kll has hinted, you will get much better help if you say more about what, specifically, you want to accomplish with this sketch. Are you trying to write your own paint program? Are you trying to create a model for a generative art work involving rectangles? Are rectangles all just an excuse to learn more about how functions work? Is sending the output to a breadboard (e.g. with an Arduino) the goal?

Some of your code looks like you are trying to develop the basic functions of a programming language, rather than use those functions that already exist. This is a great way to learn programming, if learning is your only goal – but if you have some other goal for your sketch, it is much easier to use the existing Processing API. The function “lesser”, for example, looks like you are writing your own `min()`, which is a standard part of Processing.

https://processing.org/reference/min_.html

“greater” is `max()`.

I would suggest spending a bit of time browsing through the reference – looking at things in Math and Data, for example.

https://processing.org/reference/

1 Like

Jeremy, I thought that was a really cool reply when I read it two days ago!

It is my own way to draw lines on a screen. Sure I could learn point1 to point2 in Processing, but the final goal is to build a circuit which can draw Doom (1993) or Quake onto a monitor. Sort of like the Arduino project a enthusiast would do. It’s a lot of hard work, and figuring out how to do Doom is going to be really tricky… that’s probably why I started off at the beginning, because then I can get used to the hard stuff early on (a stupid beginner you may think - yes).

Also, I am doing top-level functions, because eventually this project will be brought to chips and wires… I can also take this code and place it in different programming languages without max() min() functions specific to certain programming langauges…

Well, I like your enthusiasm. Just be aware that Doom was originally programmed in C with some assembly bits. Even if you wanted to do the whole thing in assembly, I’m not sure Processing is the best way to learn how to do that.

Programming a game rendering engine in assembly as a learner project is going to be pretty thankless, especially if you want output like Quake. It may be less like “building a house out of LEGO” and more like “using only silverware as tools to build a house.” Possible, but a miserable experience that may not produce an impressive end product.

If all-assembly rendering is your passion, I’d suggest focusing on learning it directly, not simulating it in Java. But ultimately it is up to you!