Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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?

share|improve this question
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 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 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 at 13:37
add comment

2 Answers

up vote 7 down vote accepted

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.

share|improve this answer
@peter-horsboll-moller might have to check my mapbasic knowledge there ;) –  Nathan W Feb 2 at 13:04
You passed, Nathan. Have you been reading your MapBasic reference guide again lately? :-) –  Peter Horsbøll Møller Feb 2 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 at 18:46
add comment

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.

share|improve this answer
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 at 18:49
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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