Functions, Methods & External Modules

Though I’m still very new to Xojo, I have a WebApp doing well now. It lets users log on and fill out a form of fields and press a button to have that emailed as a CSV file to someone else. All good.

But I keep reading articles on the Web and in XDev that refer to writing Functions and Methods with “Function … End Function”, and “Sub … End Sub”. I don’t see where they’re inserting such code in their Projects. I can see how I can use the IDE to, say, Insert Module, and then Add Method to that module, and then write the various parts of the Function or Method in the Method dialog box. But it looks like these developers are dynamically creating Functions and Methods via code. Is this correct?

Also, when I read Marc Zeedar’s “Power User Tips” (XDev March/April 2015, pp58-64), he talks about putting hundreds of such routines (“Function … End Function” stuff, or “Sub … End Sub” stuff) in an “external module called MarcLibrary.” I assume he somehow reuses these routines in various projects. When I look at a file created by Make External, I see a text file of code. I’m not sure how to use that file.

Try copy/pasting a method/function to textedit. You will see the same then.
This way, you’ll also see the parameters instead of only the code within.

You can also paste back a complete method/function (so it becomes a method/function again) but I’m always lost on how to do that. I think it has to do with clicking twice on where you want to copy it so the entry becomes blue. And then paste from the top menu.
(Or something like that.)

As for the text file, did you try to open it via file->open?


Ah, I see. I can Copy a Method and Paste into a text editor. And I can Copy it back to the IDE and get the Method in there as long as the area I want to copy it to is selected fully (blue). And I see in the text editor the true underlying code with “Function … End Function” or “Sub … End Sub”.

This clearly gives me a way of creating a library of routines. Thanks.

But I still don’t see how developer employs “Function … End Function” or “Sub … End Sub” in the IDE. Or do they just do that when creating external libraries, like Copying and Pasting an object does?

Ah. I think I now understand why people write Method/Function code with “Function … End Function” or “Sub … End Sub” in Web or XDev articles. It’s so I can copy and paste them into the IDE. Is that correct?

I thought it was to dynamically create such Methods/Functions in code.

The first and last line don’t show in the editor itself but it’s part of the function. It contains the name and parameters.

Here is a random function I copied with right click/copy:

Function scaleImage(p as Picture, maxWidth as Integer, maxHeight as Integer) As Picture
  // Scales a Picture while keeping the correct aspect ratio
  If p <> Nil Then
    // Scale ratio
    Dim ratio As Double = min(maxHeight / p.height, maxWidth / p.width)
    // Create the new picture to return
    Dim newPic As New Picture(p.width * ratio, p.height * ratio), 0, 0, newPic.width, newPic.height, 0, 0, p.width, p.height)
    Return newPic
   End If
End Function

The first line is what you normally see in the Inspector (on the right side in the IDE). It’s important because it holds the name, input parameters and return value.
If you just copy the part in between, it won’t work. (unless you figure out the parameters yourself :))

And Subs and Functions are both Methods. The difference is that Functions return a value.

Got it. Thanks, Marco.

Before the compiler generates machine code for the processor to execute, the IDE has to generate source code for the compiler to parse. A Xojo project is a complex tree structure, but managing it is the IDE’s job - the compiler doesn’t know anything about it. Before it can compile a class or module, the IDE must convert everything in it down to lines of source code so it can send the compiler the equivalent of a plain text file. The compiler reads this metaphorical text file one line at a time, from beginning to end, and a Sub or Function statement therefore introduces the body of a method in exactly the same way that a For or While statement introduces the body of a loop. The End Sub/End Function statement is a cue to wrap the method up and get ready for something different - it might be another method, but it could also be a property, an event definition, a constant, or anything else, depending on what the IDE happens to have written out next.

The IDE generates lots of invisible source code like this, but these two lines are particularly well known because they weren’t always invisible. For years REALbasic used to show the Sub/Function and End Sub/End Function lines in the code editor along with the body of the method. You couldn’t edit these statements, though, only look at them, and someone eventually decided that was kind of confusing. It’s all still there under the hood, though.

If you’re curious about this stuff, take a look at XojoScript, which uses the same compiler frontend. The syntax you’d use to create methods, classes, interfaces, and the like in a XojoScript is exactly the same syntax the IDE uses when it renders your project down for the compiler.

Thanks for the extensive and clear explanation, Mars. I had originally thought that developers were using Sub/Function lines in their Method code to call subroutines within Methods, without having to clutter the IDE with Methods that were only needed here and there as subroutines within other Methods. I didn’t see how that could be done when I tried it.