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 a method that returns a value. That’s it.

If you select that method, copy, and paste into a Text editor, you will see the word Function on top.

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

where exactly does this code go?

Create the Area method on a module, make it global.

The word Function will not appear in your project, though. It is internal.

When you see something like what you posted, the first line should not be copied in the method. Only this goes in the method code :

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.)

OK This gets to the issue. Why would the docs show the definition the way it did if that is NOT the way you define it.

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.

1 Like

Try copying and pasting either type of method into a text editor and you’ll see why the docs are as they are.$returning-values-from-methods

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 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

Ok, I’ve got the process, thanks for helping me understand the language…

Hey Guys,

What I am about to ask may be a repeatition to the above and hence kindly excuse me if that’s the case.
I am quite an amateur to Programming per say.

I have a query as to what can be done if there is a repeatition of code.
Say for example in Java or C, we define a function

void xyz(){


and then call that function


wherever we want, so that we don’t need to repeat the statements everywhere.

In Xojo, how do we achieve this. say suppose I create a global method



with some name xyz,

now how do i call this method in the Action Event wherever i need this ?

Is there a syntax like xyz.execute or something ?

I hope I am able to explain myself clearer…

simply xyz
there are no () or {} or ; in xojo.

[quote=482148:@Jean-Yves Pochez]simply xyz
there are no () or {} or ; in xojo.[/quote]
there are ( )
but they arent always required

Hey Jean / Norman

Thanks a lot for the input.
Really appreciate the valuable support.

I remember a change in API2 (I am not 100% sure): if you call a Method with (), they will be removed.

CallMe() will become CallMe.

The IDE does that
Turn off Preferences > Coding > Standardize Format
It removes them in places they are empty and are therefore extraneous