Re: Dumb question by a beginner

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

Jerry Shirly wrote:

>  Here's one I'm sure everyone understands but me.  smile  I don't seem able
to
>find any directions, or examples that I can fathom, of how to pass
>information back and forth between functions, or from the main program to a
>function and back again.


The Euphoria documentation assumes that you have some grasp on basic
programming. Your questions would seem to indicate that you don't. I don't
mean that to be insulting - everyone has to start somewhere. It's probably
best to get your hands on a good introduction to programming book - Basic,
C, Pascal - whatever language, it shouldn't matter too much - and go through
the first couple of chapters.

>  As I understand it, a EU procedure is when you don't need any information
> returned to the rest of the program, while a function is used when you do.

Make that "any information returned to the *caller*", since procedures and
functions can call other procedures and function as well.

>I'm not clear on what the uses there are for a procedure if all you can do
is
>pass it a value, have it work on the information, then get nothing back
>(which makes me question my understanding of it).

Let's say you want to make a beeping noise for 10 seconds with the
(imaginary) 'beep' procedure:

    beep( 10 )

There's no information for 'beep' to return (other than perhaps that it
failed). Or you want to set a pixel on the screen with the (imaginary)
routine setPixel:

    setPixel( 100, 100, GREEN )

Or let's say you wanted to print a line of text with the (imaginary)
printText procedure:

    printText( "Hi there!" )

Again, there is no need for the procedure to return information. These are
typical procedures.

>  But regardless, my experiments with functions have only yielded me
errors.

What sort of errors? Posting an example with an explanation of what you
intended the code to do might be helpful.

>When I look at other programs, I don't quite see how the main program is
>passing information to the function, nor do I see how the main program is
>receiving this information back - where does it store the data, etc..

As Jerry mentioned, there is no "main" procedure in Euphoria. Code is
executed as it is read. For example, if your file was:

    puts( 1, "example program\n" )
    for i = 1 to 3 do
        ? i
    end for

Then when you ran the file, you would get the output:

    example program
    1
    2
    3

Unlike C, there is no 'main' in sight.

>then, how do I correctly pass this sort of information back and forth
between
>procedures/functions?

OK, here is an example program:

    -- example program

    -- example of a procedure
    procedure proc( integer n )
        printf( 1, "the number %d was passed to proc().\n", {n} )
    end procedure

    -- example of a function
    function func( integer n )
        -- returns n+1 back
        printf( 1, "the number %d was passed to func().\n", {n} )
        return n+1
    end function

    -- define a variable to store the result sent back by the function
    integer result

    -- set up a loop
    for i = 1 to 10 do

        -- call the procedure
        proc( i )

        -- call the function, storing the result
        result = func( i )

        -- display the result returned from the function
        printf( 1, "the number %d was returned by func().\n", {result} )

        -- display a blank line
        puts( 1, "\n" )

    end for

Of course, if you wanted a 'main' routine, try this:


    -- example program with a 'main'

    -- example of a procedure
    procedure proc( integer n )
        printf( 1, "the number %d was passed to proc().\n", {n} )
    end procedure

    -- example of a function
    function func( integer n )
        -- returns n+1 back
        printf( 1, "the number %d was passed to func().\n", {n} )
        return n+1
    end function

    -- main routine
    procedure main()

        -- define a variable to store the result sent back by the function
        integer result

        -- set up a loop
        for i = 1 to 10 do

            -- call the procedure
            proc( i )

            -- call the function, storing the result
            result = func( i )

            -- display the result returned from the function
            printf( 1, "the number %d was returned by func().\n", {result} )

            -- display a blank line
            puts( 1, "\n" )

        end for

    end procedure

    -- call the main procedure
    main()


That example is essentially the same as the prior example.

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