Suggestion - Subroutines and Functions

JaunLukePicard

Member
Licensed User
Longtime User
As I am getting used to Basic4Android I have to say I love it. There are a couple of things I would like to ask about and possiblly get implimented in a future release...

Subroutines - right now as I understand it I can use a SUB as either a Function or Subroutine. I can opt to have it return a value or not. While this is great I also note that I can get confused easily if I happen to be doing other things for a while and then come back to my B4A project.

My first suggection is to offer that for a SUB the 'Call' statement can be optional. So for example. If you were calling a Subroutine you called 'mySub' in code you would see:

Call mySub

This would immediately tell you that you are calling a Subroutine. As code becomes more complex having the option of placing 'Call' before the Sub would make things much more clear for the developer.

One other possible suggestion I have is to have Subs broken down into Functions and Subroutines (Func/Sub).

Functions indicate that a special type of sub will be used in a formula and may have a return value (as subs do now in B4A).

A Subroutine would not return a value. It is called and actions are performed.

While subs now work well if a Developer works on several different types of projects (VB.NET, C#, Java, etc.) and then comes back to B4A wanting to make a change in code having the disctinction of Sub and Funct make re-aclimating yourself easier to working with the code.

So when you see "call mySub" you know it is a subroutine. If you see "ColumnWidth = myHeight() * 4" you know that you creayed a Function called myHeight.
 

Smee

Well-Known Member
Licensed User
Longtime User
A Subroutine would not return a value. It is called and actions are performed.

Would than not meaning a big re-write to existing code if it is changed and needs re-compiling?
 

stevel05

Expert
Licensed User
Longtime User
You could always leave a comment for yourself at the expense of one extra comment character,

i.e.
B4X:
mysub 'Call
especially if it's optional as if you forget to use call, you wouldn't know anyway.

and when you see an assignment such as
B4X:
ColumnWidth = myHeight * 4
you know it's a function.

The signature of a function is different to a sub in that you should specify the type of the returned value. i.e.
B4X:
 myFunc(val1 As Int, val2 As Int) As Int
So that would be another clue when you come back to it.
 
Last edited:

NeoTechni

Well-Known Member
Licensed User
Longtime User
Would than not meaning a big re-write to existing code if it is changed and needs re-compiling?

Yes.

I would suggest making the program simply replace function with sub. Either in the IDE or compiler
 

JaunLukePicard

Member
Licensed User
Longtime User
Would than not meaning a big re-write to existing code if it is changed and needs re-compiling?

Nope. NeoTechni mentioned that B4A could replace the desired "Sub" to "Function" either automatically or prompted as the project is loaded/converted. A comment before the line is another possibility I suppose too.

What I currently do at this point is to prefix the name of the Sub with either "fnc" or "sub" like:

subPopulateScrollView or fncPopulateScrollView

Basically if I am returning a value I use "fnc" and if I am not I use "sub" as a coding standard. I use VisualBasic.NET quite a bit so when I switch over to B4A and haven't worked on a project for a little while I have to "catch" myself and remember the changes. So now I use the same coding standard in both VB.NET and B4A.
 

stevel05

Expert
Licensed User
Longtime User
That sounds like a better solution all round.

Sent from my Hero using Tapatalk
 
Top