This is just a general question. I don’t need to use a function right now. Just reading the Language Reference about Functions. I totally get the difference between a function and a subroutine. But what I don’t get is WHERE to define the Function.
The language reference for the FUNCTION page uses the following example which is as clear as it can be. But again, where do you put this code? In Method on a Window? In a Method in a Module? I have tried both and get syntax errors.
a “Function” is just a method that returns a value
a “Subroutine” is one that does not
I usually try to define them “as close to where I need them as I can” - ie/ if the code will only be called from methods on the window then define it in the window
if its more generally useful and gets called from all over then a module makes sense
I did say I understand the difference in a function and subroutine. But let’s say I want to define a function that can be called from anywhere in the app. I have a global module with methods that work like this. But I copied the example from the Lang Ref and pasted it into a method in the global module and get a syntax error.
I’m just trying to understand the language. I see the docs that show how to define a function yet I am unable to use the example from the Lang Ref :
This example is a function that calculates area based on the length and height passed.
Function Area(Length as Double, Height as Double) As Double
Dim theArea As Double
theArea=Length*Height
Return theArea
You leave off the Function part, the IDE automatically determines that based on whether or not you specify a return type.
So when you’re setting up your global function, you create a new method name it Area, give it the parameters, and specify it returns a Double. Then you copy everything between Function and End Function into the code editor.
Unfortunately, the IDE doesn’t like it when you write functions by hand and try to paste them into the Navigator (I have never had success with this, ever.)
So as far as writing code is concerned, the WORDS subroutine and function are meaningless. You just build a method either with or without a return value.
I think maybe the Xojo docs are mixing programming “Theory” with the application in Xojo code.
Don’t misunderstand. I really love Xojo, but the docs are the weakest link in the product.
just wait until you get where you can overload a method and function with the SAME NAME (and yes there are valid reasons for doing do)…[although in most cases a computed property does the job better)
function and subs are both methods
just one returns a value and one doesn’t
[quote=325505:@william plunkett]
Don’t misunderstand. I really love Xojo, but the docs are the weakest link in the product.[/quote]
the example on http://documentation.xojo.com/index.php/Function was slightly incomplete
It was missing the “end function” line and so copy paste would not allow you to paste this in as is
Normally it should just let you paste in the “function” and it would define the right method that returns the value
Thats been fixed
Other than that you normally dont see “sub” or “function” in the ide - you just have a method that does or doesn’t return a value