Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 09:09 01 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : CMM2: Function Returning a String

Author Message
vegipete

Guru

Joined: 29/01/2013
Location: Canada
Posts: 1132
Posted: 10:39pm 23 Jun 2020
Copy link to clipboard 
Print this post

The following code works just fine:
dim string c
c = GetLetter
print c
end

function GetLetter() as string length 1
 GetLetter = "123"
end function

But should it? Is that the desired behaviour? Can the size of the string returned by the function be specified?

Changing the first line above to
dim string length 1
generates an expected error.
Visit Vegipete's *Mite Library for cool programs.
 
KeepIS

Guru

Joined: 13/10/2014
Location: Australia
Posts: 1882
Posted: 10:55pm 23 Jun 2020
Copy link to clipboard 
Print this post

I've never used basic like that with a function returning a string of x length, and I would not expect it to function correctly. I'm sure the length parameter was not intended to be used in this way as there is likely a default mem allocation for a string. I think length parameter was mainly for declaring arrays etc.

But as usual I could be totally wrong in my assumptions
Edited 2020-06-24 08:56 by KeepIS
NANO Inverter: Full download - Only Hex Ver 8.1Ks
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3292
Posted: 05:18am 24 Jun 2020
Copy link to clipboard 
Print this post

First, when you call a function you need the brackets even if there is no argument:
c = GetLetter()


Second, the length qualifier is only used to tell MMBasic how much memory to allocate for the string.  This is only important in arrays.  To quote from page 58 of the manual:

  Quote  Strings will default to allocating 255 bytes (ie, characters) of memory for each element and this can quickly use up memory when defining arrays of strings.  In that case the LENGTH keyword can be used to specify the amount of memory to be allocated to each element and therefore the maximum length of the string that can be stored.  This allocation ('n') can be from 1 to 255 characters.

For example:  DIM STRING s(5, 10)  will declare a string array with 66 elements consuming 16,896 bytes of memory while:
DIM STRING s(5, 10) LENGTH 20
Will only consume 1,386 bytes of memory.  Note that the amount of memory allocated for each element is n + 1 as the extra byte is used to track the actual length of the string stored in each element.

If a string longer than 'n' is assigned to an element of the array an error will be produced.  Other than this, string arrays created with the LENGTH keyword act exactly the same as other string arrays.  This keyword can also be used with non array string variables but it will not save any memory.


You should use the LEFT$() function to get the first character of the string:

dim string c
c = GetLetter()
print c
end

function GetLetter() as string
  GetLetter = LEFT$("123", 1)
end function


Geoff
Geoff Graham - http://geoffg.net
 
vegipete

Guru

Joined: 29/01/2013
Location: Canada
Posts: 1132
Posted: 05:43am 24 Jun 2020
Copy link to clipboard 
Print this post

Thank you for your reply.

First issue, no brackets on function call, was a typo on my part. Apologies.

The second part came from some testing after an error.
Unfortunately, I didn't get the details. A running program halted with an error message along the lines of "insufficient string memory to assign a string" or some such. The indicated error line was a function definition returning a string. At first I thought perhaps the function had been called so many times as to fill up 'string memory' but some testing showed millions of calls to a function returning a string caused no problems.

Since my function only needed to return a single character, I next experimented with forcing the function string length to 1, which led to the above example.

Note that
dim string c length 1
(Oh phooie, another typo in my original post) does work, and creates a string named 'c' that can hold only a single character, regardless of memory consumed.
Visit Vegipete's *Mite Library for cool programs.
 
GregZone
Senior Member

Joined: 22/05/2020
Location: New Zealand
Posts: 114
Posted: 06:27am 24 Jun 2020
Copy link to clipboard 
Print this post

  Geoffg said  To quote from page 58 of the manual:

  Quote  ...
This keyword can also be used with non array string variables but it will not save any memory.


Geoff


Hi Geoff.  I'm a little unclear on your last sentence explaining LENGTH.

Is this because each assignment to a regular string variable deallocates/allocates new storage, thereby always consuming just the current length of the string (+1 byte)?

Or, are you saying that outside of an Array, a string allocation always consumes 256 bytes (length 255+1)?
ie. In effect, the LENGTH keyword, when used outside of an Array, is simply ignored?

Thanks
Greg
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 07:20am 24 Jun 2020
Copy link to clipboard 
Print this post

  GregZone said  
Hi Geoff.  I'm a little unclear on your last sentence explaining LENGTH.

Is this because each assignment to a regular string variable deallocates/allocates new storage, thereby always consuming just the current length of the string (+1 byte)?

Or, are you saying that outside of an Array, a string allocation always consumes 256 bytes (length 255+1)?
ie. In effect, the LENGTH keyword, when used outside of an Array, is simply ignored?

Thanks
Greg


Any string will be allocated 256 bytes.
If you limit to a short length, it will consume 256 bytes
It will also raise an error of you try and store more in a string than it's declared length

So
DIM c$ LENGTH 1
will use 255+1 bytes but trying c$ = "KK" will give an error.

Jim
VK7JH
MMedit
 
Chopperp

Guru

Joined: 03/01/2018
Location: Australia
Posts: 1097
Posted: 12:45pm 24 Jun 2020
Copy link to clipboard 
Print this post

@Jim

Not sure what you mean.

From the CMM2 Manual

Strings will default to allocating 255 bytes (ie, characters) of memory for
each element and this can quickly use up memory when defining arrays of
strings. In that case the LENGTH keyword can be used to specify the
amount of memory to be allocated to each element and therefore the
maximum length of the string that can be stored. This allocation ('n') can be
from 1 to 255 characters.

For example: DIM STRING s(5, 10) will declare a string array with
66 elements consuming 16,896 bytes of memory while:

DIM STRING s(5, 10) LENGTH 20
Will only consume 1,386 bytes of memory.


Using LENGTH should reduce the amount of memory used for the String.

Brian
ChopperP
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10310
Posted: 12:54pm 24 Jun 2020
Copy link to clipboard 
Print this post

  Quote  Using LENGTH should reduce the amount of memory used for the String.


The minimum unit of memory is 256 bytes. This applies to strings, string arrays and also integer and float arrays. Setting a length on a simple string will control how much data you can save in it but will not change the memory allocated which will always be 256 bytes. The only reason I can see for setting a length on a simple string is if you need to control it in order to copy to or from a string array with a fixed length.
 
Chopperp

Guru

Joined: 03/01/2018
Location: Australia
Posts: 1097
Posted: 01:40pm 24 Jun 2020
Copy link to clipboard 
Print this post

Should the manual be updated to reflect this?
ChopperP
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025