LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

Function return Array

Root / FAQs / [.]

xXOscarXxCreated:
Can a function return a 1D Array? I would like to have a function who receive a Int and, depending on the length of the input number, return a list (1D array) of int. There en example of what I mean: input = 1234 output[0] -> 34 output[1] -> 12
DEF SPLIT V
 ' Convert the int into a str for length.
 V$=STR$(V) 
 ' Create the empty array for the return value.
 DIM SPLIT_ARR[0]

 FOR I=LEN(v$) TO 1 STEP -2
  I_STEP = I-2
  'I_STEP won't go below 0.
  IF I_STEP < 0 THEN I_STEP=0 
  'Extract the number from the str.
  SPLITED = VAL(MID$(V$,I_STEP,I))
  'Add that value to the array.
  PUSH SPLIT_ARR, SPLITED
  NEXT
 
 'Return the array.
 RETURN SPLIT_ARR
 END


Yeah, that works. You can return any type of variable/array from a function.

Thank you for your fast reply! :) I'v been reading the instruction list (http://smilebasic.com/en/reference/). It helped me on how to achieve what I wanted. So here what I've got so far:
@FUNCTION_DEF
DEF SPLIT(V,STP)

 V$=STR$(V) 
 V_LEN = LEN(V$)
 DIM SPLIT_ARR[0]

 IF NOT V_LEN MOD STP == 0 THEN
  INC V_LEN, 1
  V$=FORMAT$("%0"+STR$(V_LEN)+"D",V)

 FOR I=V_LEN TO 1 STEP -STP
  I_STP = I-STP
  SPLITED = VAL(MID$(V$,I_STP,STP))
  PUSH SPLIT_ARR, SPLITED
  NEXT
 
 RETURN SPLIT_ARR
 END

@MAIN
DIM A[0]
COPY A, SPLIT(1122,2)

PRINT A[0]
PRINT A[1]
Return: 22 11 Is there a more efficient way to extract the value from the function: I tried to just :
@MAIN
A = SPLIT(1122,2)

PRINT A[0]
PRINT A[1]
But this return : Type mismatch in 0:22

Try
DIM A[0]
A=SPLIT(1122,2)
EDIT: also, check that the behaviour of your code for negative numbers, and for zero, is acceptable for your purposes, if such numbers will ever happen (e.g. try -5, -15, -105... some of the results might surprise you).

Also, DEFs are compiled before the program starts, so you don't need a label before them that you can jump to. (It's actually best to put them after all your code so they can access outside variables)

I think you could do something like this instead:
DEF SPLIT NUMBER
 DIM LIST%[0]
 WHILE NUMBER 'end when NUMBER reaches 0
  PUSH LIST%,NUMBER MOD 100 'get the last two digits
  NUMBER=NUMBER DIV 100 'integer divide by 100
 WEND
 RETURN LIST%
END

Thank you so much 12Me21 and SquareFingers, you guys rock!! I just add a DIGIT parameter:
DEF SPLIT(NUMBER,DIGIT)
 DIM LIST%[0]
 WHILE NUMBER
  PUSH LIST%,NUMBER MOD POW(10,DIGIT) 
  NUMBER=NUMBER DIV POW(10,DIGIT) 
  WEND
 RETURN LIST%
END

DIM A[0]
A=SPLIT(1122,2)

PRINT A[0]
PRINT A[1]
Return: 22 11 And:
DIM A[0]
A=SPLIT(111222333,3)

PRINT A[0]
PRINT A[1]
PRINT A[2]
Return: 333 222 111 The function work now with negative value, but it return : -22 -11 I think it would be nice if the index 0 of the array was be reserve to the "positive / negative" attribute of the given number. 0 for negative and 1 for positive. For -1122, the SPLIT function will return: A[0]=0 A[1]=22 A[2]=11

I made it! Here it is:
DEF SPLIT(NUMBER,DIGIT)
 DIM LIST%[1]
 
 LIST%[0]=1
 IF NUMBER < 0 THEN
  NUMBER = ABS(NUMBER)
  INC LIST%[0],-1
  ENDIF
 
 WHILE NUMBER
  PUSH LIST%,NUMBER MOD POW(10,DIGIT) 
  NUMBER=NUMBER DIV POW(10,DIGIT) 
  WEND
 RETURN LIST%
END

You could use SGN to check if a number is positive or negative:
LIST%[0]=SGN(NUMBER)

My idea, by using the current syntax, is to use the functions in that kind of context: Return: Negative 34 12 For -1234 , 0 , 1234, the "SGN()" function return -1, 0, 1, which means True, False, True in boolean. If I store the result of the "SGN()" function into "List%[0]" the conditions will no longer work properly.

If you're going to use IF A[0] THEN, then A[0] is taken to be boolean - i.e. splitting up the set of numbers into two groups. So, you have to decide what those two groups are. You use PRINT "Positive" and PRINT "Negative", but that is not a way of splitting up all the numbers. Zero does not fall into either category. You can choose to say that zero is not an allowed value to feed to your code, or you can say that the zero is included in one of the two categories. More commonly, it is included with the 'positive' category, and 'positive or zero' is usually called 'nonnegative' - so you have negative vs. nonnegative, and this includes all the numbers. Or, you may want to split up numbers with zero on the other side, and call the two categories positive and nonpositive. But with just two categories 'positive' and 'negative', you're leaving a hole in your system, at zero. You're not doing anything wrong, it's just the kind of decision you have to make every now and again when programming.

Wow your explanations are so good! Thanks! 8D The idea of that function was to display the health or experience using this format. I didn't find a way to make a font 4 X 8 pixels. So I thought of sprites. In this example: 12 is a sprite 8 X 8 pixels just as 34, 56 ans 78. By defining 100 (00, 01, 02, [...]) sprites, I can convert any numbers, but they need to be split. I think the "negative vs. nonnegative" may be a better solution for that kind of purpose?

You could just define 10 4x8 sprites instead

Oh... Yea, true that ¬¬" Ahahah xD!!

There are characters in the SmileBasic character set, as shown on the page http://smilebasicsource.com/page?pid=173 that you can use on the console, if you'd prefer that to using sprites. EDIT: Although, looking at that selection of characters, it seems to support left-alignment rather than right-alignment, so that's a reason you may not want to use that option.

I will take a look at it 8D It was my idea initially, but, my knowledge is too limited for the moment to be able to do it xD

The public key has been removed :( I can try to make it i guess. I found the list for these character on the official basic web site: http://smilebasic.com/en/supplements/unicode09/ I guess I can use the code point to get the right symbol?

Looking at that page, it looks like you can do right-alignment, if you don't mind grey '0' padding on the left. So the table shows E10A corresponding to "10". So, CHR$(0xE10A) (EDIT: CHR$(&HE10A) - thanks, 12Me21) is the character for "10".

Looking at that page, it looks like you can do right-alignment, if you don't mind grey '0' padding on the left. So the table shows E10A corresponding to "10". So, CHR$(0xE10A) is the character for "10".
(SB uses &H, not 0x)