In MapBasic it seems like Functions can have side effects so I don't really see the difference between using the following:

Declare Sub MyProcedure (myParameter As String) and then use myParameter after Call MyProcedure(myParameter)


Declare Function MyProcedure () As string and do something like myParameter = MyProcedure()

Am I missing something?

  • What side-effects? Otherwise yes, the 2 setups do the same. But using a function call is cleaner imho, in this case. – Uffe Kousgaard Feb 2 '14 at 13:27
  • @UffeKousgaard a MapBasic function can modify state (of a global variable for instance) other than the return value. As described on wikipedia on side effects – steenhulthin Feb 2 '14 at 13:32
  • OK, but so can Sub's. Global variables are truly global in Mapbasic. Not very modern in any sense. – Uffe Kousgaard Feb 2 '14 at 13:37

A Function always returns a value where as a Sub does not. All methods in MAPBASIC can have side effects in the true term because it's not a functional language.

If you are expecting a return value you should use a Function that is what they are there for.

Can you modify the value of myParameter inside a Sub and then just use it after. Yes. But this is messy and generally not good programming

Sometimes modifying a input variable is a way to have multiple outputs in languages that support multiple return types (in something like Python you can return as many things as you want)

An example might be:

Declare Function Convert(passed as Boolean) As string

Function Convert(passed as Boolean) As string
   passed = True
   return "Converted"
End Function

Then you can check passed after running the function.

| improve this answer | |
  • @peter-horsboll-moller might have to check my mapbasic knowledge there ;) – Nathan W Feb 2 '14 at 13:04
  • You passed, Nathan. Have you been reading your MapBasic reference guide again lately? :-) – Peter Horsbøll Møller Feb 2 '14 at 18:45
  • And not that if you don't want your procedure to pass back a modified paramter, you can use the ByVal keyword – Peter Horsbøll Møller Feb 2 '14 at 18:46

Reading a bit more up on MapBasic I can see that one difference is that it is not possible to pause execution of a Function with Stop according to the MapBasic user Guide. But this restriction does not apply to Subs.

| improve this answer | |
  • 3
    Well, yeah. You are touching some of the not so good stuff in the MapBasic language - Debugging. I have been coding MapBasic applications for years and never ever used the debugging in MapBasic. I have basically learned to live with my own debug methods thru Print and Note statements – Peter Horsbøll Møller Feb 2 '14 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.