Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Returning a type
#1
After learning that you could return types in FB .14, I decided to try it out. Unfortunately my program doesn't work, and I can't figure out why. (I know its not my compiler, because rdc's fractal aggregation program compiles fine)

It doesn't return an error nor a warning. The box at the bottom of FB IDE pops up, but its completely blank....

Any and all help would be appreciated...Thanks alot. Smile


Code:
Type CoordType
    X As Integer
    Y As Integer
End Type

Declare Function MoveUp ( argCoord As CoordType ) As CoordType

screen 14, 32
Dim Shared Coord( 99 ) As CoordType
Dim TempCoord As CoordType

Function MoveUp ( argCoord As CoordType ) As CoordType
    Dim TempCoord As CoordType
    
    TempCoord.Y = argCoord.Y - 1
    If TempCoord.Y < 0 Then TempCoord.Y = 0
    
    Return TempCoord
End Function

Function Moving ()
    Coord( 0 ) = MoveUp( Coord( 0 ) )
End Function


Coord( 0 ).X = 8
Coord( 0 ).Y = 2

Print "Before MoveUp"
Print Coord( 0 ).X
Print Coord( 0 ).Y

Coord( 0 ) = MoveUp( Coord( 0 ) ) ' <- IF I COMMENT OUT THIS LINE IT WORKS FINE, BUT I'M NOT RETURNING ANYTHING....

Print "After MoveUp"
Print Coord( 0 ).X
Print Coord( 0 ).Y

Sleep

End

Edit: I marked the line that I can comment out and my program will work. It seemingly doesn't like it when I try to actually USE the function to return a type....It doesn't mind that I have such a function. :???: :???:
[Image: freebasic.png]
Reply
#2
My FB 0.14 crashes. The problem is this line:
Code:
Declare Function MoveUp ( argCoord As CoordType ) As CoordType
The reason why this causes a problem is because of the "As CoordType" on the end (no clue why. . .)

As for your problem, when I use the following code:
Code:
Type CoordType
    X As Integer
    Y As Integer
End Type

Declare Function MoveUp (argCoord As Integer) As Integer

Screen 14, 32
Dim Shared Coord(99) As CoordType
Dim TempCoord As CoordType

Coord(0).X = 8
Coord(0).Y = 2

Print "Before MoveUp"
Print Coord(0).X
Print Coord(0).Y

Coord(0).Y = MoveUp(Coord(0).Y) ' <- IF I COMMENT OUT THIS LINE IT WORKS FINE, BUT I'M NOT RETURNING ANYTHING....

Print "After MoveUp"
Print Coord(0).X
Print Coord(0).Y

Sleep

End

Function MoveUp (argCoord As Integer) As Integer
    Dim TempCoord As Integer
  
    TempCoord = argCoord - 1
    If TempCoord < 0 Then TempCoord = 0
  
    Return TempCoord
End Function
That manages to compile and it works. Unfortunately, that defeats the purpose of returning a UDT. . .
974277320612072617420666C61696C21 (Hexadecimal for those who don't know)
Reply
#3
thats odd, just yesterday i wrote a func to return an OPENFILENAME struct for API programming, so i know it normally works.

nothing i tried could get that code to compile (functionally )without fb crashing

btw, post this on freebasic.net too, i dunno if v1c looks here all the time, and i think he only comes online once a day these days..
Reply
#4
odd...
Code:
type type1
    a as integer
    b as string
    c as integer
end type

dim bah as type1
dim pie as type1
declare function func(pie as type1) as type1

function func(pie as type1) as type1
    dim tmp as type1
    tmp.a=pie.a+1
    tmp.b=pie.b+"a"
    tmp.c=pie.c+2
    return tmp
end function

pie.a=1:pie.b="moo":pie.c=32
bah = func(pie)
print bah.a, bah.b, bah.c
sleep

that works fine.

maybe the arrays...



Code:
type type1
    a as integer
    b as string
    c as integer
end type

dim bah(2)as type1

declare function func(pie as type1) as type1

function func(pie as type1) as type1
    dim tmp as type1
    tmp.a=pie.a+1
    tmp.b=pie.b+"a"
    tmp.c=pie.c+2
    return tmp
end function

bah(0).a=1:bah(0).b="moo":bah(0).c=32
bah(1) = func(bah(0))
print bah(1).a, bah(1).b, bah(1).c
sleep
no well that works fine.


Code:
Type CoordType
    X As Integer
    Y As Integer
End Type

Dim Coord( 2 ) As CoordType

Declare Function MoveUp ( argCoord As CoordType ) As CoordType

Function MoveUp ( argCoord As CoordType ) As CoordType
    Dim TempCoord As CoordType
  
    TempCoord.Y = argCoord.Y - 1
    'If TempCoord.Y < 0 Then TempCoord.Y = 0
  
    Return TempCoord
End Function

Coord(0).X=8:Coord(0).Y=2
Coord(0) = MoveUp(Coord(0))

Print Coord( 0 ).X, Coord( 0 ).Y
Sleep
doesn't work! arrrg!


OKAY I got it

Code:
Type CoordType
    X As Integer
    Y As Integer
    c as integer
End Type

Declare Function MoveUp ( argCoord As CoordType ) As CoordType

screen 14, 32
Dim Shared Coord( 99 ) As CoordType
Dim TempCoord As CoordType

Function MoveUp ( argCoord As CoordType ) As CoordType
    Dim TempCoord As CoordType
  
    TempCoord.Y = argCoord.Y - 1
    If TempCoord.Y < 0 Then TempCoord.Y = 0
  
    Return TempCoord
End Function

Function Moving ()
    Coord( 0 ) = MoveUp( Coord( 0 ) )
End Function


Coord( 0 ).X = 8
Coord( 0 ).Y = 2

Print "Before MoveUp"
Print Coord( 0 ).X
Print Coord( 0 ).Y

Coord( 0 ) = MoveUp( Coord( 0 ) ) ' <- IF I COMMENT OUT THIS LINE IT WORKS FINE, BUT I'M NOT RETURNING ANYTHING....

Print "After MoveUp"
Print Coord( 0 ).X
Print Coord( 0 ).Y

Sleep

End

Added "c as integer" to the type.
Dunno why it works now o_o
[Image: sig.php]
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Reply
#5
You should post this at...

http://www.freebasic.net/forum/index.php
Reply
#6
It is fixed in CVS. Please wait some hours for the anonymous CVS update.

Regards,
Mark
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)