LoginLogin

Why can't I pass multiple arguments to a DEF subroutine/function

Root / Programming Questions / [.]

_Nyap_Created:
I'm not using GOSUB functions because they can't take any arguments at all in the first place, plz correct me if im wrong Here's the code I'm having trouble with:
'---WINDOW HANDLER---
DEF MAKEWINDOW(XSIZE,YSIZE,PROGID)
PROGRAMSXY[PROGID,0]=199
PROGRAMSXY[PROGID,1]=119
FOR E=119 TO 139 STEP 1
GLINE 199,E,199+XSIZE,E
NEXT
END

'---TEST PROGRAM---
DEF TEST(ID)
MAKEWINDOW(100,100,ID)
END
Note: this is just a snippet of the program, not the entire program. There's a syntax error on line 107 (the call to MAKEWINDOW), and removing the last 2 arguments fixes it. Any suggestions?

I think multiparameter functions have to be in the form FUNC ARG1,ARG2 rather than FUNC(ARG1,ARG2) or something.

ohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh thnx

do function definitions have to be before any calls to the function? because I'm getting another function-related error

The way you define the function changes how it is supposed to be called:
variable=FUNC(args)
FUNC2 args
DEF FUNC(args)
 'do something with the arguments
 RETURN something
END
DEF FUNC2 args
 'do something with the arguments
END
This means you'll want this code instead:
'---WINDOW HANDLER---
DEF MAKEWINDOW XSIZE,YSIZE,PROGID
PROGRAMSXY[PROGID,0]=199
PROGRAMSXY[PROGID,1]=119
FOR E=119 TO 139 STEP 1
GLINE 199,E,199+XSIZE,E
NEXT
END

'---TEST PROGRAM---
DEF TEST ID
MAKEWINDOW 100,100,ID
END

Oh that's right. Yes, internally there's a difference between user-defined commands and user-defined functions. Functions will return a value and use the parenthetical syntax while commands do not. And no, there's no need to define before calling or anything.

So if I use brackets it goes on the stack, and without brackets it doesn't?

So if I use brackets it goes on the stack, and without brackets it doesn't?
Well it's not a stack/heap issue. If you use parenthesis, it internally wants a return value. If you don't, it doesn't expect a return value. So
DEF FUNC(A,B,C) 'this will want a RETURN at the end of the block
DEF FUNC A,B,C 'this is essentially a subroutine
It would be the same (in C#/Java) as doing:
public object func(a,b,c) //requires a return value...type doesn't matter
public void func(a,b,c) //no return value

is that still one of the differences tho?

is that still one of the differences tho?
Honestly I'm not sure. SB is a mainly interpreted language, so concepts like the stack and heap have much less to do with the programming aspect of the language. You could have concepts like the heap that are managed on the stack, and vice versa. I don't even worry about what's going on behind the hood when it comes to interpreted languages like SB.

This has taught me a lot about DEF functions and whatnot. I always thought that DEF functions were just another way to do a subroutine in the program.