Re: Hi I am back and some ?'s

new topic     » goto parent     » topic index » view thread      » older message » newer message

David Mosley asked:

>(1) Are procedures like gosubs( they do a certian thing then go from there)
>like this
>
>procedure (screen_print,sequnce t)-- if you put seq t here then when the
>procedure ends then ends the seq t varable
>t=({hi})
>puts (1,t)
>end procedure
>then you can call on sreen_print any time you want it.

Well, you are right in that a subroutine encapsulates a chunk of code so
that it can be called any time you want it. But a GOSUB is the wrong analogy
for a number of reasons:

    - a GOSUB is basically a GOTO with a RETURN. Euphorioa has no GOTOs.

    - You can't pass a local parameter list with a GOSUB, like:

        GOSUB screen_print, "Print me!"

    - A GOSUB routine can't have local (private) variables

A procedure is akin to a subroutine. For example:

QBasic:

    SUB screen_print( x AS INTEGER, y AS INTEGER, text AS STRING )
        LOCATE( x, y )
        print text
    END SUB

Euphoria:

    procedure screen_position( integer x, integer y, sequence text )
        position( x, y )
        puts( 1, text )
    end procedure

>(2) Are include files like calling a dll?

Sort of - on a very high conceptual level. The main differences between a
DLL and include file are:

1. A DLL is a compiled set of libraries (read: in binary). An include file
is just plain text, like your program.

2. Attaching a DLL routine means linking the DLL, defining a prototype to
the DLL function, and writing a wrapper for that prototype. With a routine
from a include file, you just call it - much less effort.

3. Only the code and variables defined as 'global' can be accessed from the
include file If you don't declare variables and constants within the include
file as global, can only be seen (and used) by code in the include file.

> I know that when I make a file called data.e then make it like this
>
>globle procedure (data,seq q1,q2,q3)
>q1=({time})
>q2=({for})
>q3=({lunch})
>end procedure
>then I can call data in the program and it will return the seq to the
>program is this right.
>

Euphoria can only pass parameters *by value*. That means that a *copy* of
the value is passed to the routine, not the actual variable. Altering a
value of a parameter does not change the value for the caller.

For example:

    global procedure data( sequence q1 )
        q1 = "time for lunch"
    end procedure

    sequence q
    q = "time for breakfast"
    data( q )
    puts( 1, q )

would print "time for breakfast", *not* "time for lunch". If you want to get
data back from a routine, you need to define a function, not a procedure.
Euphoria routines can only return a *single* value, You can get around this
by having a function return a sequence filled with values, but then you have
to unpack the sequence. For example:

    global function data()
        return { "time", "for", lunch" }
    end function

    sequence result, q1, q2, q3
    result = data()
    q1 = result[1]
    q2 = result[2]
    q3 = result[3]


=== WISH LIST ITEM ===

It would look a lot cleaner if you could write:

    { q1, q2, q3 } = data()

instead of:

    result = data()
    q1 = result[1]
    q2 = result[2]
    q3 = result[3]

but Euphoria doesn't support it (yet).

=== END WISH LIST ITEM ===

Hope this helps!

-- David Cuny

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu