FUNCTIONS need a type to return?

New to FreeBASIC? Post your questions here.
Post Reply
bulrush
Posts: 61
Joined: Nov 02, 2010 12:56
Location: Michigan

FUNCTIONS need a type to return?

Post by bulrush »

I'm using #lang "fb".

Me again. Do functions need to be defined to return a certain data type? Like this:
Function foo(a as integer) as integer

If my function does not have a data type, will it be defined as the default data type?
What is the default data type in FB?

Thanks again.
RayBritton
Posts: 306
Joined: Jun 02, 2005 7:11
Contact:

Post by RayBritton »

There are two method types subs and functions, functions return a value, subs don't. Both accept parameters.

Code: Select all

declare sub MethodA(value as integer)
declare function MethodB(value as integer) as integer
I don't know what you mean by default data type.
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Post by MichaelW »

Try it, the answer is in the error message.

Code: Select all

function xxx()
end function
error 136: Default types or suffixes are only valid in -lang deprecated or fblite or qb in 'function xxx()'
kiyotewolf
Posts: 1009
Joined: Oct 11, 2008 7:42
Location: ABQ, NM
Contact:

Post by kiyotewolf »

In lang FB, you have to put AS INTEGER or AS STRING after the final ) of the function.

FreeBasic is pretty strict, about wanting to know what you wanna do with your data.

I call it, the most Turbo Pascal'ish version of BASIC out yet.

You have to declare every single variable, and you have to declare exactly what data types you wanna use.

:.:

When you try to leave a function, like so..

FUNCTION LLAMACOOKIE(inputllama as integer)

and don't put this..

FUNCTION LLAMACOOKIE(inputllama as integer) as STRING

then FB thinks you wanna go back to Qbasic-like syntax.

If you're purposely using lang FB, then you must apply yourself to use the strict coding methods of FB.

Every FUNCTION must have a particular TYPE of variable, it is supposed to return.

Even a function with no particular input variable.

FUNCTION SOMETHINGCUTE() as string

See how it has opening and closing parenthasissietes.. but it too, has an AS SOMETHING, telling it, that you want it to be SOMETHING.



~Kiyote!

Yes, to get it to work in lang FB, you must put as AS STRING, AS INTEGER, AS LONG, AS LONGINT, AS UBYTE, whatever you want, just something.
kiyotewolf
Posts: 1009
Joined: Oct 11, 2008 7:42
Location: ABQ, NM
Contact:

Post by kiyotewolf »

In QBasic, the default type, was whatever you did like so.

DEFINT A-Z

That mean't, that each of those randomly created variables you made, should automatically, without a doubt, be integers.

In FreeBasic, lang FB, there is no default data type.

You must say so, for each routine, and each variable.

A huge ain n he ss, I know.

But, it teaches you better coding methods.

It shows you, just how many variables you actually use.



~Kiyote!

I use generic names for some variables.

DIM REUSABLE AS INTEGER
DIM WOLFCODE1 AS STRING, WOLFCODE2 AS INTEGER
DIM LLAMACODE1 AS INTEGER
kiyotewolf
Posts: 1009
Joined: Oct 11, 2008 7:42
Location: ABQ, NM
Contact:

Post by kiyotewolf »

Code: Select all


declare function blink() as integer

function blink() as integer

static blinkvalue as integer

blinkvalue = 1 - blinkvalue

blink = blinkvalue

end function

Code: Select all


declare function blink() as integer

function blink() as integer

static blinkvalue as integer

blinkvalue = 1 - blinkvalue


return blinkvalue

print "This PRINT statement will not be executed."

end function
Notice, you put a RETURN command.

The return command does two things.

One, it passes on the final answer of the routine, and also, it terminates the routine, the same as EXIT FUNCTION does.

If you wish to modify your return value often, then use a dummy variable, then assign the final return value at the very end.



~Kiyote![/code]
Post Reply