1. Functions? I'm Lost!!

Could someone give me an example of how to use a function.
I just can't seem to grasp the concept. I know they return
a value. I just can't find an example where anything for the
function is used any where else in the program. I'm just lost.
Thanks for your time.


Dave

new topic     » topic index » view message » categorize

2. Re: Functions? I'm Lost!!

On 17 May 2000, at 17:20, David Roach wrote:

Date sent:              Wed, 17 May 2000 17:20:58 -0400
Send reply to:          Euphoria Programming for MS-DOS <EUPHORIA at
LISTSERV.MUOHIO.EDU>
From:                   David Roach <roachd_76 at YAHOO.COM>
Subject:                Functions? I'm Lost!!
To:                     EUPHORIA at LISTSERV.MUOHIO.EDU

> Could someone give me an example of how to use a function.
> I just can't seem to grasp the concept. I know they return
> a value. I just can't find an example where anything for the
> function is used any where else in the program. I'm just lost.

>From the Eu help files:

functions
These are just like procedures, but they return a value, and can be used
in an expression, e.g.
        function max(atom a, atom b)
            if a >= b then
                return a
            else
                return b
            end if
        end function
Any Euphoria object can be returned. You can, in effect, have multiple
return values, by returning a sequence of objects. e.g.
        return {x_pos, y_pos}
We will use the general term "subroutine", or simply "routine" when a
remark is applicable to both procedures and functions.

new topic     » goto parent     » topic index » view message » categorize

3. Re: Functions? I'm Lost!!

------=_NextPart_000_0019_01BFC031.DC64FE80
        charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hi Dave,

    Most libs use functions to return a value to the calling program, =
since they can't directly put a value to the program variables.
IE. David Cuny's  winlib..
    when you create a control( a window or listbox or whatever), it =
returns a handle so you can access the control you created later.

example..

<CODE>

include win32lib.ew

constant
   =
,0),
                 -- ^^ this is a function. it returns a handle so winlib =
can manipulate it in later code
    Mnu_File=3Dcreate(Menu,"&File", Win_Main,0,1,0,0,0),
  -- ^^  the value returned by create..... ^^ this is the handle return =
by creating the window
       Mnu_Fexit=3Dcreate(MenuItem,"E&xit",Mnu_File,0,0,0,0,0)
-------------------------------------------------------------------------=
-----
--EVENTS
procedure onMnu_Fexit()
   closeWindow(Win_Main)
end procedure
-------------------------------------------------------------------------=
-----
--EVENTS

onClick [Mnu_Fexit] =3D routine_id("onMnu_Fexit")
                                -- ^^ this is a function too..

WinMain(Win_Main, Normal)

</CODE>

Chris

------=_NextPart_000_0019_01BFC031.DC64FE80
        charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content=3D"text/html; charset=3Diso-8859-1" =
http-equiv=3DContent-Type>
<META content=3D"MSHTML 5.00.2314.1000" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT color=3D#0000ff face=3D"Comic Sans MS">Hi Dave,</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT color=3D#0000ff face=3D"Comic Sans MS">&nbsp;&nbsp;&nbsp; =
Most libs use=20
functions to return a value to the calling program, since they can't =
directly=20
put a value to the program variables.</FONT></DIV>
<DIV><FONT color=3D#0000ff face=3D"Comic Sans MS">IE.&nbsp;David Cuny's  =

winlib..</FONT></DIV>
<DIV><FONT color=3D#0000ff face=3D"Comic Sans MS">&nbsp;&nbsp;&nbsp; =
when you create=20
a control( a window or listbox or whatever), it returns a handle so you =
can=20
access the control you created later.</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT color=3D#0000ff face=3D"Comic Sans MS">example..</FONT></DIV>
<DIV><FONT color=3D#0000ff face=3D"Comic Sans MS"></FONT>&nbsp;</DIV>
<DIV><FONT color=3D#0000ff face=3D"Comic Sans =
MS">&lt;CODE&gt;</FONT></DIV>
<DIV><FONT color=3D#0000ff face=3D"Comic Sans MS"></FONT>&nbsp;</DIV>
<DIV><FONT color=3D#0000ff face=3D"Comic Sans MS">include =
win32lib.ew</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT color=3D#0000ff face=3D"Comic Sans =
MS">constant<BR>&nbsp;&nbsp;=20
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
-- ^^ this is a function. it returns a handle so winlib can manipulate =
it in=20
later code</FONT></DIV>
<DIV><FONT color=3D#0000ff face=3D"Comic Sans MS">&nbsp;&nbsp;&nbsp;=20
Mnu_File=3Dcreate(Menu,"&amp;File", Win_Main,0,1,0,0,0),</FONT></DIV>
<DIV><FONT color=3D#0000ff=20
face=3D"Comic Sans MS">&nbsp;&nbsp;--&nbsp;^^&nbsp;&nbsp;the value =
returned by=20
create..... ^^ this is the handle return by creating the=20
window<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
NT=20
color=3D#0000ff=20
face=3D"Comic Sans =
MS">---------------------------------------------------------------------=
---------<BR>--EVENTS</FONT></DIV>
<DIV><FONT color=3D#0000ff face=3D"Comic Sans MS">procedure=20
onMnu_Fexit()<BR>&nbsp;&nbsp; closeWindow(Win_Main)<BR>end=20
procedure</FONT></DIV>
<DIV><FONT color=3D#0000ff=20
face=3D"Comic Sans =
MS">---------------------------------------------------------------------=
---------<BR>--EVENTS</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT color=3D#0000ff face=3D"Comic Sans MS">onClick [Mnu_Fexit] =
=3D=20
routine_id("onMnu_Fexit")</FONT></DIV>
<DIV><FONT color=3D#0000ff face=3D"Comic Sans MS">&nbsp;&nbsp;&nbsp;=20
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;=20
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; -- ^^ this is=20
a&nbsp;function too..<BR></DIV></FONT><FONT color=3D#0000ff=20
face=3D"Comic Sans MS"></FONT>
<DIV><FONT color=3D#0000ff face=3D"Comic Sans MS">WinMain(Win_Main,=20
Normal)</FONT></DIV>
<DIV><FONT color=3D#0000ff face=3D"Comic Sans MS"></FONT>&nbsp;</DIV>
<DIV><FONT color=3D#0000ff face=3D"Comic Sans =
MS">&lt;/CODE&gt;</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT color=3D#0000ff face=3D"Comic Sans =

------=_NextPart_000_0019_01BFC031.DC64FE80--

new topic     » goto parent     » topic index » view message » categorize

4. Re: Functions? I'm Lost!!

I guess I'm just confused on where and how to use them. I understand that
they return a value. ex.

function same(object a, object b)
     return not compare(a,b)
end function

I understand that this will return a value of 1 if true and a value 0 if
false(I believe). I have tried to use them and often get errors. Is there
anywhere that tells where and under what secumstances you can call a
function. I can only get them to work in an if...then statement. Are you
supposed to be able to use them anywhere else? Have I just missed something?
Or is that the only time you would actually need to use a function? I'm just
new and get confused, that's all.


Just a note:
This forum is great. I always get help with the problems that I have.
If it weren't for ya'll I'd have given up a long time ago. Thanks Bunches!!

Dave

new topic     » goto parent     » topic index » view message » categorize

5. Re: Functions? I'm Lost!!

On Wed, 17 May 2000 17:20:58 -0400, David Roach <roachd_76 at YAHOO.COM> wrote:

>Could someone give me an example of how to use a function.
>I just can't seem to grasp the concept. I know they return
>a value. I just can't find an example where anything for the
>function is used any where else in the program. I'm just lost.
>Thanks for your time.
>
>
>Dave

  Dave:

    Here is a simple example

-- Say I want to create a function that adds 2 numbers

function sum( atom no1, atom no2 )
  return no1 + no2
end function

-- to call the function and print the sum of 2 numbers

  ? sum( 15, 20 )

-- we could just write this

  ? 15+20

-- but the purpose of function ( which can be very complex ) is
-- to be defined only ONCE in your program and be called through out
-- your program to perform the task over and over again and return a
-- VALUE. Remember the big different is that a VALUE is return in a
-- function and NO VALUE is return from a procedure.

  Bernie

new topic     » goto parent     » topic index » view message » categorize

6. Re: Functions? I'm Lost!!

--Here's a function:

function foo( atom a, atom b)
    return (a+b)
end function

--Here's how it's used.
atom a

a=foo(2,2)   -- the function call
?(a)
a=foo(2,3)
?(a)
?(foo(3,3))

--A function returns a value, so it needs to have someplace for the value to
;

--A procedure doesn't return a value, it just does something

procedure printboo(atom number)
    for i = 1 to number do
        puts(1, "Boo")
    end for
end procedure

printboo(3)
printboo(4)
printboo(5)


Bye
Martin

----- Original Message -----
From: David Roach <roachd_76 at YAHOO.COM>
To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
Sent: Wednesday, May 17, 2000 2:20 PM
Subject: Functions? I'm Lost!!


> Could someone give me an example of how to use a function.
> I just can't seem to grasp the concept. I know they return
> a value. I just can't find an example where anything for the
> function is used any where else in the program. I'm just lost.
> Thanks for your time.
>
>
> Dave
>

new topic     » goto parent     » topic index » view message » categorize

7. Re: Functions? I'm Lost!!

The following code defines a function that returns the number 5 and uses
it:

integer a          -- Define an integer named "a"
function return5() -- Define the function "return5"
    return 5       -- Return 5
end function
a=return5()        -- Get return5() to return its value and assign it to "a"
? a                -- Print the integer "a", just to test it out

The following code defines a function that returns the absolute value of a
number and uses it:

function abs(atom number) -- Defines abs().
    return sqrt( number * number ) -- Behold the magic formula!
end function
? abs(-54)                -- Should say 54

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu