I'm new to microcontroller programming and just discovered GCBasic. It looks great so I decided to use it for my first Atmega168 project. Wandering on the forum for code samples I found a construct that is not explained in the manual:
Dim First, Second As String * 10
Function MyFunc(In First(), In Second()) As String * 10
;Code here
End Function
Could you tell me why the First and Second parameters have brackets after them? Wouldn't the following code do the same thing:
Dim First, Second As String * 10
Function MyFunc(In First, In Second) As String * 10
;Code here
End Function
Another quick question. When declaring a function, do the parameters need to be declared separately as in the example above?
Thank you for a wonderful programming language.
BR.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Regarding the passing of arrays and strings. The use/definition of braces in the sub or function will give optimised code. It is good practice to pass the arrays and strings in this manner. Compiling your two methods and reviewing the ASM will show the optimisation. Essentially, arrays and strings are the same construct in memory and using the () will pass these always as arrays and not strings (this is the optimisation).
I am not sure I understand the second question - but, you approach is correct. Declare the passing of parameters as you have shown above (using braces). :-)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thank you for the answer. I will try to always use the braces for string parameters.
Regarding my second question, let me clarify it a bit. What I've noticed in other languages (including C) is that when declaring a function, the parameters are automatically defined by the language from the declaration itself and they are local for the function. For example consider this:
void test(int a, int b)
{
a = 5;
b = 10;
}
In this case a and b are defined automatically as int. In GCBasic, in one of the codes I found, the parameters a and b were declared manually with Dim, like this:
Dim str1, str2 As String * 10
Function test(In str1, In str2) As String * 10
;Code here
End Function
Is this the standard practice in GCBasic? Or the whole code above could be written like this:
Function test(In str1 As String, In str2 As String) As String * 10
;Code here
End Function
(e.g. omitting the vars with Dim separately).
Thanks.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Byte variables are defined upon use. It can be useful however to define any variables including bytes to mimimise memory usage. This is not the same as other languages but it really makes things simple.
Last edit: Anobium 2015-06-21
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
When working with strings, I saw in one code that the length of the string can be obtained from the first element of the string. For example the function Val() is using this:
Function Val(SysInString As String) As Word
...
'Get input length
SysCharCount = SysInString(0)
...
But then, as the first position in the string buffer is automatically occupied and the contents actually start at position 1, don't I need to make the buffer bigger with one more byte? To clarify, doesn't the following code create a buffer underrun in MyString?
Dim MyString As String * 3
MyString = "abc"
Or when defining "String * 3" GCBasic internally allocates 4 characters (to cover for the first one used for the length)?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi everyone,
I'm new to microcontroller programming and just discovered GCBasic. It looks great so I decided to use it for my first Atmega168 project. Wandering on the forum for code samples I found a construct that is not explained in the manual:
Dim First, Second As String * 10
Function MyFunc(In First(), In Second()) As String * 10
;Code here
End Function
Could you tell me why the First and Second parameters have brackets after them? Wouldn't the following code do the same thing:
Dim First, Second As String * 10
Function MyFunc(In First, In Second) As String * 10
;Code here
End Function
Another quick question. When declaring a function, do the parameters need to be declared separately as in the example above?
Thank you for a wonderful programming language.
BR.
Welcome. Hope we can help you.
Regarding the passing of arrays and strings. The use/definition of braces in the sub or function will give optimised code. It is good practice to pass the arrays and strings in this manner. Compiling your two methods and reviewing the ASM will show the optimisation. Essentially, arrays and strings are the same construct in memory and using the () will pass these always as arrays and not strings (this is the optimisation).
I am not sure I understand the second question - but, you approach is correct. Declare the passing of parameters as you have shown above (using braces). :-)
Thank you for the answer. I will try to always use the braces for string parameters.
Regarding my second question, let me clarify it a bit. What I've noticed in other languages (including C) is that when declaring a function, the parameters are automatically defined by the language from the declaration itself and they are local for the function. For example consider this:
void test(int a, int b)
{
a = 5;
b = 10;
}
In this case a and b are defined automatically as int. In GCBasic, in one of the codes I found, the parameters a and b were declared manually with Dim, like this:
Dim str1, str2 As String * 10
Function test(In str1, In str2) As String * 10
;Code here
End Function
Is this the standard practice in GCBasic? Or the whole code above could be written like this:
Function test(In str1 As String, In str2 As String) As String * 10
;Code here
End Function
(e.g. omitting the vars with Dim separately).
Thanks.
Byte variables are defined upon use. It can be useful however to define any variables including bytes to mimimise memory usage. This is not the same as other languages but it really makes things simple.
Last edit: Anobium 2015-06-21
Also see https://sourceforge.net/p/gcbasic/discussion/579125/thread/0db03f8b/
Thank you for the help Anobium, it's much appreciated.
Back with one more question if you don't mind :).
When working with strings, I saw in one code that the length of the string can be obtained from the first element of the string. For example the function Val() is using this:
Function Val(SysInString As String) As Word
...
'Get input length
SysCharCount = SysInString(0)
...
But then, as the first position in the string buffer is automatically occupied and the contents actually start at position 1, don't I need to make the buffer bigger with one more byte? To clarify, doesn't the following code create a buffer underrun in MyString?
Dim MyString As String * 3
MyString = "abc"
Or when defining "String * 3" GCBasic internally allocates 4 characters (to cover for the first one used for the length)?
The latter is true. GCBasic internally allocates 4 characters to cover for the first one used for the length.
For example of a serial buffer have a look at the Help file. I wrote one a few years ago.
Thank you sir.