Menu

Question regarding function parameters

Help
2015-06-20
2015-06-21
  • nachosrule1988

    nachosrule1988 - 2015-06-20

    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.

     
  • Anobium

    Anobium - 2015-06-21

    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). :-)

     
  • nachosrule1988

    nachosrule1988 - 2015-06-21

    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.

     
  • Anobium

    Anobium - 2015-06-21

    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
  • nachosrule1988

    nachosrule1988 - 2015-06-21

    Thank you for the help Anobium, it's much appreciated.

     
  • nachosrule1988

    nachosrule1988 - 2015-06-21

    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)?

     
  • Anobium

    Anobium - 2015-06-21

    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.

     
  • nachosrule1988

    nachosrule1988 - 2015-06-21

    Thank you sir.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.