Re: Myster Tour

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

------=_NextPart_000_0051_01C00389.D5AD9660
        charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hi Thomas

A function takes some code and puts it in a package so that it can be =
used many times within a program. Procedures do the same. The difference =
between functions and procedures is that you get a value back from a =
function, and you just do something with a procedure.

So in your example:
function simple_sort(sequence x)         =20
is, obviously the start of the function. The keyword "function" lets the =
system know that a function follows. The name simple_sort gives the name =
of the function and the stuff in brackets is the data that you want the =
function to work with. This data is generally called a parameter, or =
parameters if there are more than one. The sequence keyword says =
declares what kind of data the parameter is, and the x is the name of =
the data for use within the function.

Mud, right?

Maybe it clarifies a bit when you see how to use it.

Say you have a sequence with some data in it:
a=3D{1,3,2,3,6,5} that you want to sort into numerical order.

If you want to use the function simple_sort you just do this

sequence a, b
a=3D{1,3,2,3,6,5}=20
b=3Dsimple_sort(a) -- this is a function call - a goes to simple_sort =
and b recieves the sorted data returned by simple_sort.
A function call says (in polite English) please put the result of the =
sort operation done on "a" into "b"

Notice that the data that you send is called "a" from the calling =
program, but is called "x" within the function. That's because "X" is a =
local variable, that only exists for the time that the function is =
actually processing your data, and then disappears again.

The "return" that you asked about is just the command that tells the =
function what data to send back to the calling program.

The main difference between a function and a procedure is that the =
procedure doesn't have the "return". A procedure looks like a simple =
command, because it doesn't need to recieve a value back.

In polite english, a procedure just says - Please do something. Say you =
want to draw a dot on the screen. You make a procedure called Makedot. =
Makedot needs to know where on the screen, and what colour to use, so it =
needs to have 3 atoms as the parameters.=20

procedure Makedot(atom xposition, atom yposition, atom colour)
    pixel(colour, {xposition, yposition})
end procedure

See? - no return

To call it you just write

Makedot(100, 100, 5)

No recieving variable, and no equals sign - just an instruction to do =
something.

You ask:=20
"for i =3D 1 to length(x) + 1 do                -- is length(x) the =
legnth of simple_sort? if so, why not just for i=3D1 to =
length(simple_sort)"
A couple of points here:
Length is a function - you need to pass it data, which is contained in =
the sequence x. So "lenght(x)" is requesting the length of the data =
sequence x. simple_sort isn't data - it's a function. I'm not sure if =
it's useful for you to know how big the function is when you want to =
know the length of the data sequence that you passed to it.

But notice that there is no variable recieves the value returned by the =
function length - the value just gets plugged into the upper limit of =
your loop.=20

Say I have a function that doubles a number:

function dub(atom a)
    return (a+a)
end function

I can call it like this

atom a, b
a=3D1
b=3Ddub(a)

Guess what the value of b is.

But say you wanted to multiply by 4. Then you could write:

b=3Ddub(a)+dub(a)

or you could write

b=3Ddub(dub(a))

Enough for now - I'll write about sequence subscripts later.

Bye
Martin



  ----- Original Message -----=20
  From: Paul Kerslake=20
  To: EUPHORIA at LISTSERV.MUOHIO.EDU=20
  Sent: Friday, August 11, 2000 10:34 AM
  Subject: Myster Tour


  I think I've found out why I find programming so confusing. I =
understand the commands, the concepts of sequence and atom etc. It's =
those little numbers that get to me. Little numbers that palgue nearly =
all of the commands! Here's what I'm ranting about:

  THIS IS THE "SIMPLE.EX" FILE IN THE TUTORUAL

  -- how does one use function? and what does simple_sort(sequence x) =
mean?=20
  object temp
      for i =3D 1 to length(x) + 1 do                -- is length(x) the =
legnth of simple_sort? if so, why not just for i=3D1 to =
length(simple_sort)
          for j =3D i + 1 to length(x) do
              if compare(x[j],x[i]) < 0 then     --    \
                  -- swap x[j], x[i]                    --     \
                  temp =3D x[j]                --                   ---- =
  I need these ones explained, I've got no idea what they mean
                  x[j] =3D x[i]                    --                /
                  x[i] =3D temp                --                /
              end if
          end for
      end for
      return x                        -- whats return do?
  end function

  Well, there's another batch of problems.........get it? Batch? =
heheheh..............erm...
  Thanks

  -Thomas   (Yes, it is right to those who asked!)

------=_NextPart_000_0051_01C00389.D5AD9660
        charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 5.50.4134.600" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT size=3D2>Hi Thomas</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>A function takes some code and puts it in a package =
so that it=20
can be used many times within a program. Procedures do the same. The =
difference=20
between functions and procedures is that you get a value back from a =
function,=20
and you just do something with a procedure.</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>So in your example:</FONT></DIV>
<DIV><FONT size=3D2>function simple_sort(sequence=20
x)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></DIV>
<DIV><FONT size=3D2>is, obviously the start of the function. The keyword =

"function" lets the system know that a function follows. The name =
simple_sort=20
gives the name of the function and the stuff in brackets is the data =
that you=20
want the function to work with. This data is generally called a =
parameter, or=20
parameters if there are more than one. The sequence keyword says =
declares what=20
kind of data the parameter is, and the x is the name of the data for use =
within=20
the function.</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>Mud, right?</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>Maybe it clarifies a bit when you see how to use=20
it.</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>Say you have a sequence with some data in =
it:</FONT></DIV>
<DIV><FONT size=3D2>a=3D{1,3,2,3,6,5} that you want to sort into =
numerical=20
order.</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>If you want to use the function simple_sort you just =
do=20
this</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>sequence a, b</FONT></DIV>
<DIV><FONT size=3D2>a=3D{1,3,2,3,6,5} </FONT></DIV>
<DIV><FONT size=3D2>b=3Dsimple_sort(a) -- this is a function call - a =
goes to=20
simple_sort and b recieves the sorted data returned by =
simple_sort.</FONT></DIV>
<DIV><FONT size=3D2>A function call says (in polite English) please put =
the result=20
of the sort operation done on "a" into "b"</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>Notice that the data that you send is called "a" =
from the=20
calling program, but is called "x" within the function. That's because =
"X" is a=20
local variable, that only exists for the time that the function is =
actually=20
processing your data, and then disappears again.</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>The "return" that you asked about is just the =
command that=20
tells the function what data to send back to the calling =
program.</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>The main difference between a function and a =
procedure is that=20
the procedure doesn't have the "return". A procedure looks like a simple =

command, because it doesn't need to recieve a value back.</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>In polite english, a procedure just says - Please do =

something. Say you want to draw a dot on the screen. You make a =
procedure called=20
Makedot. Makedot needs to know where on the screen, and what colour to =
use, so=20
it needs to have 3 atoms as the parameters. </FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>procedure Makedot(atom xposition, atom yposition, =
atom=20
colour)</FONT></DIV>
<DIV><FONT size=3D2>&nbsp;&nbsp;&nbsp; pixel(colour, {xposition,=20
yposition})</FONT></DIV>
<DIV><FONT size=3D2>end procedure</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>See? - no return</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>To call it you just write</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>Makedot(100, 100, 5)</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>No recieving variable, and no equals sign - just an=20
instruction to do something.</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>You ask: </FONT></DIV>
<DIV><FONT size=3D2>"for i =3D 1 to length(x) + 1 do&nbsp;&nbsp;&nbsp;=20
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; -- is length(x) =
the=20
legnth of simple_sort? if so, why not just for i=3D1 to=20
length(simple_sort)"</FONT></DIV>
<DIV><FONT size=3D2>A couple of points here:</FONT></DIV>
<DIV><FONT size=3D2>Length is a function - you need to pass it data, =
which is=20
contained in the sequence x. So "lenght(x)" is requesting the length of =
the data=20
sequence x. simple_sort isn't data - it's a function. I'm not sure if =
it's=20
useful for you to know how big the function is when you want to know the =
length=20
of the data sequence that you passed to it.</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>But notice that there is no variable recieves the =
value=20
returned by the function length - the value just gets plugged into the =
upper=20
limit of your loop. </FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>Say I have a function that doubles a =
number:</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>function dub(atom a)</FONT></DIV>
<DIV><FONT size=3D2>&nbsp;&nbsp;&nbsp; return (a+a)</FONT></DIV>
<DIV><FONT size=3D2>end function</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>I can call it like this</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>atom a, b</FONT></DIV>
<DIV><FONT size=3D2>a=3D1</FONT></DIV>
<DIV><FONT size=3D2>b=3Ddub(a)</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>Guess what the value of b is.</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>But say you wanted to multiply by 4. Then you could=20
write:</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>b=3Ddub(a)+dub(a)</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>or you could write</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>b=3Ddub(dub(a))</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>Enough for now - I'll write about sequence =
subscripts=20
later.<BR></FONT></DIV>
<DIV><FONT size=3D2>Bye<BR>Martin<BR></DIV></FONT>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<BLOCKQUOTE dir=3Dltr=20
style=3D"PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; =
BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
  <DIV style=3D"FONT: 10pt arial">----- Original Message ----- </DIV>
  <DIV=20
  style=3D"BACKGROUND: #e4e4e4; FONT: 10pt arial; font-color: =
black"><B>From:</B>=20
  <A title=3Dpaulk at UNISERVE.COM href=3D"mailto:paulk at UNISERVE.COM">Paul =
Kerslake</A>=20
  </DIV>
  <DIV style=3D"FONT: 10pt arial"><B>To:</B> <A =
title=3DEUPHORIA at LISTSERV.MUOHIO.EDU=20
  =
</A>=20
  </DIV>
  <DIV style=3D"FONT: 10pt arial"><B>Sent:</B> Friday, August 11, 2000 =
10:34=20
  AM</DIV>
  <DIV style=3D"FONT: 10pt arial"><B>Subject:</B> Myster Tour</DIV>
  <DIV><BR></DIV>
  <DIV><FONT face=3DArial size=3D2>I think I've found out why I find =
programming so=20
  confusing. I understand the commands, the concepts of sequence and =
atom etc.=20
  It's those little numbers that get to me. Little numbers that palgue =
nearly=20
  all of the commands! Here's what I'm ranting about:</FONT></DIV>
  <DIV>&nbsp;</DIV>
  <DIV><FONT face=3DArial size=3D2>THIS IS THE "SIMPLE.EX" FILE IN THE=20
  TUTORUAL</FONT></DIV>
  <DIV>&nbsp;</DIV>
  <DIV><FONT face=3DArial size=3D2>-- how does&nbsp;one use function? =
and what does=20
  simple_sort(sequence x)&nbsp;mean? <BR>object =
temp<BR>&nbsp;&nbsp;&nbsp; for i=20
  =3D 1 to length(x) + 1 do&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;=20
  &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; -- is length(x) the legnth of=20
  simple_sort? if so, why not just for i=3D1 to=20
  length(simple_sort)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for =
j =3D i +=20
  1 to length(x)=20
  =
do<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
if=20
  compare(x[j],x[i]) &lt; 0 then&nbsp;&nbsp;&nbsp;&nbsp;=20
  =
  -- swap x[j], x[i]&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;=20
  &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;=20
  =
  temp =3D x[j]&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;=20
  &nbsp;&nbsp;&nbsp;=20
  =
  I&nbsp;need these ones explained, I've got no idea what they=20
  =
;&nbsp;&nbsp;&nbsp;&nbsp;=20
  x[j] =3D x[i]&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;=20
  &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; --&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;=20
  &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;=20
  =
bsp;&nbsp;&nbsp;&nbsp;=20
  x[i] =3D temp&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;=20
  &nbsp;&nbsp;&nbsp; --&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;=20
  &nbsp;&nbsp;&nbsp;=20
  =
/<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
end=20
  if<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end =
for<BR>&nbsp;&nbsp;&nbsp;=20
  end for<BR>&nbsp;&nbsp;&nbsp; return x&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;=20
  &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp; --=20
  whats return do?<BR>end function</FONT></DIV>
  <DIV>&nbsp;</DIV>
  <DIV><FONT face=3DArial size=3D2>Well, there's another batch of=20
  problems.........get it? Batch? =
heheheh..............erm...</FONT></DIV>
  <DIV><FONT face=3DArial size=3D2>Thanks</FONT></DIV>
  <DIV>&nbsp;</DIV>
  <DIV><FONT face=3DArial size=3D2>-Thomas&nbsp;&nbsp; (Yes, it is right =
to those=20

------=_NextPart_000_0051_01C00389.D5AD9660--

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

Search



Quick Links

User menu

Not signed in.

Misc Menu