News:

Please note new Forum URL: forum.designcad.com

Main Menu
DesignCAD 3D Max 2027 box
Now available

DesignCAD 2027

Hatches that follow their boundaries, an on-screen dial to turn your model, and a way to find any command by name, in a clean Windows 11 look.

Writing a function in BasicCAD ?

Started by Slash705, January 16, 2015, 05:22:38 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Slash705

Hi,

I would like to know if it's possible to write a function (like in other languages) in basic CAD, or to return a value with the 'return' keyword ?
Basically what I'm looking for is the following:


MyAwesomeFunction(parameter_a, parameter_b):
return a + b


or something like this


MyAwesomeLabel:
myvalue = 42
return myvalue


Thanks for the help!

DrollTroll

No, BasicCAD doesn't support user-defined functions. The best you can do is use global variables for your arguments and for the result, and call a subroutine to do the work. Example:

'sub_example.d3m
' demonstrates use of a subroutine to emulate a function
param_a = 3
param_b = 7
my_result = 0
' calculate the average of param_a and param_b
Gosub mean
Message my_result
End

mean:
' input global variables are param_a and param_b
' result stored in global value my_result
my_result = 0.5*(param_a + param_b)
return
25 years with DesignCAD

teachcad269

Hello Slash705,

What is your actual problem? Why do you need to write a function in BasicCAD?

I suggest you to report yourself on JavaScript, this is actually the best language in programmation so it could solve your problems.

However, if you really need to use BasicCAD, you could go on the CADoc, which is the online documentation (you can find it here : http://image.noelshack.com/fichiers/2010/49/1291760631-cznznc.swf)

Friendly your !

Slash705

#3
Thank you DrollTroll, this is actually what I'm already doing, I was just wondering if there was an other / easier way to do it.

teachcad269, or should I call you Oliver, you should really consider doing something with your life if you really think JavaScript is a decent language.

samdavo

#4
Quote from: DrollTroll on January 16, 2015, 09:08:07 AM
No, BasicCAD doesn't support user-defined functions. The best you can do is use global variables for your arguments and for the result, and call a subroutine to do the work. Example:

'sub_example.d3m
' demonstrates use of a subroutine to emulate a function
param_a = 3
param_b = 7
my_result = 0
' calculate the average of param_a and param_b
Gosub mean
Message my_result
End

mean:
' input global variables are param_a and param_b
' result stored in global value my_result
my_result = 0.5*(param_a + param_b)
return


Slash (and/or DT)
At risk of making a fool of myself,
If that program (subroutine) calculates My-result, then surely it can use that value in subsequent expressions?
Whence/whither the problem?

Sorry, I don't understand,  either the question or the answer.
No biggie if you too busy to explain :)
cheers

(edit) you could always ask the program to print a message "my_result =", my_result
and then continue, yes?

Or I suppose you could get creative, and draw a line my_result in length (?) - or a graph for that matter - which would be available for interrogation after the program was run(?)

Dr PR

Sam,

Functions define their own internal variables. So you can have a global variable named "x" and a local variable in the function named "x". Calculations in the function change only the local variable and not the global variable. Then the function returns a value to the assigned variable in the function call.

So if the function is defined as

function(x, y)
{
z = x + y;
return z
}

the function call

y = function(a, b)

calls function, passing the values of the variables a and b and returns a value to variable y. In this case x, y and z are internal variable names for the function, a, b and y are external variables. Only the external variable y is changed outside the function. Any variables x and z outside the function are not changed.

Note that functions can call functions and the above variable rules apply.

The value in all of this is that you can assign global variable names and function variable names with impunity, and can call functions in libraries that were written by other people without having to know anything about the variable names used in the functions. This is pretty much essential in large programs because no one can remember the names of all the variables in all the functions in all the libraries.

With BASICAD you have to remember all variable names and ensure that every variable used in subroutines (calls) is unique.

****

And in my opinion NO programming language is a "decent language." They all suck! Of course, like many other things, some have their devote followers who insist that FORTRAN, Pascal, C, C++, Java, Python, ALGOL, Pearl, Stoic, Forth, Basic, Legoscript, APL. Lisp, Ada, PL/I ... (whatever) is the ultimate programming language. So far in my experience they have all been wrong.

For a list of about 700 imperfect programming languages see:

http://en.wikipedia.org/wiki/List_of_programming_languages

Perhaps the most accurately descriptive programming language name is POOP. The most screwball was APL (Another Programming Language) that used only special graphic symbols that do not appear on a normal computer keyboard. So the only way to program was with pencil on paper (or charcoal on the back of a shovel). Not perfect for computer programming!

Phil
DesignCAD user since 1987

samdavo

Thanks Phil,
I never realised that functions could be so complex :)
sam

prl

You could adapt the Call statement to simulate a function call, with each function having its own variables etc. 

see http://forum.designcad.com/index.php?topic=5456.0

Besides this example, I never bothered with all that.


Subroutines and ensuring you have unique variable names is easier for short little programs.

dcadRob

#8
You can  use CALL and  pass parameters using the SYSEX() and SYSEX$() variables
Example:
'rem call function TESTCALLFUNCTION.BSC
sysex(1)=5
sysex(2)=10
call "testfunction.bsc"
print sysex(100)
anykey
end

'rem function TESTFUNCTION.BSC
x=sysex(1)
y=sysex(2)

sysex(100)=x+y
end