![]() |
Forum Index : Microcontroller and PC projects : Need some help with a command conversion.
Author | Message | ||||
Oldbitcollector![]() Senior Member ![]() Joined: 16/05/2014 Location: United StatesPosts: 172 |
I'm currently converting an old GWBASIC program which has the following line: DEF FND(D)=Sqr((K(I,1)-S1)^2+(K(I,2)-S2)^2) This DEF FND has me a little stumped. I don't remember using it back in the day. Could someone help me with a MMBASIC equivalent? Thanks My Propeller/Micromite mini-computer project. |
||||
palcal![]() Guru ![]() Joined: 12/10/2011 Location: AustraliaPosts: 1967 |
I think it is defining a function. Paul. "It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all" |
||||
WhiteWizzard Guru ![]() Joined: 05/04/2013 Location: United KingdomPosts: 2927 |
Correct - Defines a function. See below for extract from GW-Basic manual: DEF FN Statement Purpose: To define and name a function written by the user. Syntax: DEF FNname[arguments] expression Comments: name must be a legal variable name. This name, preceded by FN, becomes the name of the function. arguments consists of those variable names in the function definition that are to be replaced when the function is called. The items in the list are separated by commas. expression is an expression that performs the operation of the function. It is limited to one statement. In the DEF FN statement, arguments serve only to define the function; they do not affect program variables that have the same name. A variable name used in a function definition may or may not appear in the argument. If it does, the value of the parameter is supplied when the function is called. Otherwise, the current value of the variable is used. The variables in the argument represent, on a one-to-one basis, the argument variables or values that are to be given in the function call. User-defined functions may be numeric or string. If a type is specified in the function name, the value of the expression is forced to that type before it is returned to the calling statement. If a type is specified in the function name and the argument type does not match, a "Type Mismatch" error occurs. A user-defined function may be defined more than once in a program by repeating the DEF FN statement. A DEF FN statement must be executed before the function it defines may be called. If a function is called before it has been defined, an "Undefined User Function" error occurs. DEF FN is illegal in the direct mode. Recursive functions are not supported in the DEF FN statement. Examples: [code]400 R=1: S=2 410 DEF FNAB(X, Y)=X^3/Y^2 420 T=FNAB(R, S) [/code] Line 410 defines the user-defined function FNAB. The function is called in line 420. When executed, the variable T will contain the value R3 divided by S2, or .25. |
||||
mindrobots Newbie ![]() Joined: 21/05/2014 Location: United StatesPosts: 32 |
Nothing like traveling 1/2 way around the world to get an answer from 30 miles away! OBC, MMBASIC has the FUNCTION/END FUNCTION construct which is much more powerful than the old GWBASIC DEF FN. DEF FN was limited to a single line user function. FUNCTION/END FUNCTION is not limited. The end result is you have a chunk of code that can return a value from a FUNCTION as opposed to a SUB which does not return a value. In your function code, you just set the function name = to the value you want to return. Example from the MMBASIC manual: FUNCTION SQUARE(a) SQUARE = a * a END FUNCTION PRINT SQUARE(56.8) ' calls my user function It's actually in the manual. ![]() (This goes back a oouple month when I was asking about something in MMBASIC and was soundly told by my mates on PropellerPowered that it was "in the manual") Rick P.S. Your example should be something like this in MMBASIC: FUNCTION D() D=Sqr((K(I,1)-S1)^2+(K(I,2)-S2)^2 END FUNCTION I'm not sure why the GWBASIC version passes D into the function but never uses it. S1, S2, K and I all need to be set up as valid variables some place before the function is actually called. |
||||
Oldbitcollector![]() Senior Member ![]() Joined: 16/05/2014 Location: United StatesPosts: 172 |
Thank you all for the great answers... Forgot I invited Rick over to my secret hideout across the pond.. :) Answers from 30 miles away, by way of the other side of the globe. You know this Internet thing might actually catch on.... Jeff My Propeller/Micromite mini-computer project. |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |