Page 1 of 1 1
Topic Options
#198193 - 2010-03-26 03:43 PM Implement Procedures
BillBarnard Offline
Starting to like KiXtart

Registered: 2007-03-14
Posts: 141
Loc: Leighton Buzzard, Bedfordshire...
Calling functions like this is OK:-

$ = SomeFunction($params)

But it would be neat to allow procedures to be defined. e.g.

DoSomething($fred, 2)

Would call the following routine to add 2 to the value of $fred

PROCEDURE DoSomething($param1, $param2)
$param1 = $param1 + $param2
ENDPROCEDURE

Where parameters can be passed either by reference or value.

Regards,

Bill
_________________________
Bill

Top
#198199 - 2010-03-26 04:14 PM Re: Implement Procedures [Re: BillBarnard]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Not sure what extra functionality you are looking for.

Functions that don't return anything are procedures, so the following will already work for example:
 Code:
HelloWorld
HelloWorld()
HelloWorld("Foo")

Function HelloWorld(Optional $s)
   "Hello "+$s+@CRLF
EndFunction


Pass-by-reference is regularly requested, and I'll second it (again).

Top
#198203 - 2010-03-26 04:29 PM Re: Implement Procedures [Re: Richard H.]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
Bill's referring to "indirection", where the UDF would identify the name of the variable to modify, instead of either passing and returning, or using a Global. Using his example, the "procedure" would use "$Fred" as its LValue, and use $Fred + 2 as its RValue, after determining that $Param1 contained "$Fred" and Param2 contained the value "2". That would add two to the variable $Fred without returning anything. Similar result to
 Code:
$Fred = MyFunc($Fred, 2)

Function MyFunc($P1, $P2)
  $MyFunc = $P1 + $P2
EndFunction
without actually having the assignment at the function.

I don't see a lot of use for this right away, and we could probably get crafty and use Execute to accomplish this.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#198205 - 2010-03-26 04:38 PM Re: Implement Procedures [Re: Glenn Barnas]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Well, that's pass by reference yes-no?

My point is that it doesn't require a new construct in the language, it simply requires the oft-requested support for pass by reference (or pointer for the old school).

Top
#198206 - 2010-03-26 04:40 PM Re: Implement Procedures [Re: Glenn Barnas]
BillBarnard Offline
Starting to like KiXtart

Registered: 2007-03-14
Posts: 141
Loc: Leighton Buzzard, Bedfordshire...
I was only asking for a way of making a call to a routine or Procedure a bit neater.
I want to do something or lots of things without necessarily needing to return a value.

DoSomething(Params)

Is much nicer than:-

Gosub "DoSomething" ; using Global variables
or
$ = DoSomething($params)

If you currently call a Function without assigning its value to a variable, it prints the returned value to the console. Messy!

Cheers anyway,
_________________________
Bill

Top
#198207 - 2010-03-26 04:51 PM Re: Implement Procedures [Re: BillBarnard]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
That's trickier.

If your function does not return a value then it is equivalent to a VB Sub and you don't need to assign the function to a value. This functionality is already present.

The problem is that the implied action in KiXtart is to output the content of the stack to the console - this is how normal output works. In other words, if there is data on the stack and there is no operator left to perform an action on it then the intention is to display it.

Now, we could work around this by implementing a specific "PRINT" command to display output to the console or maybe introduce a suppression operator so for example if you preceded an expression (or function) with say the "#" character it suppresses the implied print action.

Seems like a lot of effort to avoid typing "$=" in front of a function to discard the output ;\)

Top
#198220 - 2010-03-27 12:07 PM Re: Implement Procedures [Re: Richard H.]
ChristopheM Offline
Hey THIS is FUN
*****

Registered: 2002-05-13
Posts: 309
Loc: STRASBOURG, France
to avoid 0 shown on the screen, you can have a template of function like that :
 Code:
function MyFunction(....)
  $MyFunction = ""
  ..
  ..
endfunction

In this case, this is not 0 that is printed to screen but an empty string !!!

No need to change KiXtart syntax.
_________________________
Christophe

Top
#198230 - 2010-03-29 02:38 PM Re: Implement Procedures [Re: ChristopheM]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
No need to initialise the function return to "" - in fact it is better if you don't as the return will be type "Empty" which is more correct than a zero length string.
 Code:
Foo
Bar

"Foo returns '" Foo "' Type=" VarTypeName(Foo) ?
"Bar returns '" Bar "' Type=" VarTypeName(Bar) ?

Function Foo() EndFunction
Function Bar()  $Bar="" EndFunction


 Quote:
D:\temp>kix32 nr.kix
Foo returns '' Type=Empty
Bar returns '' Type=String

Top
#198234 - 2010-03-29 03:40 PM Re: Implement Procedures [Re: Richard H.]
BillBarnard Offline
Starting to like KiXtart

Registered: 2007-03-14
Posts: 141
Loc: Leighton Buzzard, Bedfordshire...
Thanks Guys.

I wonder if it'll work on my old BBC Micro, Sinclair Spectrum and Transam Triton ?

Don't eat too much chocolate over Easter.

Regards,
_________________________
Bill

Top
#198258 - 2010-03-30 01:54 PM Re: Implement Procedures [Re: BillBarnard]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
 Originally Posted By: BillBarnard
I wonder if it'll work on my old BBC Micro, Sinclair Spectrum and Transam Triton ?
I don't know.. I'd fire up my Altair 8800, but I don't think Kix is available on paper-tape. \:D

Wish I still had my MS BASIC on paper tape..

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
Page 1 of 1 1


Moderator:  Lonkero, ShaneEP, Jochen, Radimus, Glenn Barnas, Allen, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
2 registered (morganw, mole) and 414 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.065 seconds in which 0.025 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org