Page 1 of 1

Parameters vs. functions -- I don't get it

Posted: Wed Nov 01, 2006 7:14 pm
by Timothy
Maybe a few kind souls will educate me just a tad, or suggest a source for a concise explanation.

From my self-taught hyperCard days, I learned a little about parameters. If a handler called another handler... for example

On thisHandler
put something into x
put something else into y
put anotherthing into x
thatHandler x,y,y
end thisHandler

I could send variables x,y and z to another handler, without having to bother with declaring x y and z as globals and then cleaning them up when I was done with them.

That's all I know about parameters. Apparently there's more to parameters than this, though. For example,

On linkClicked theText
blah
blah
blah
end linkClicked

I've learned by doing that "theText" is an arbitrary variable name. Apparently, "on linkClicked thetext" means:

on linkClicked
get some something that gets sent along with the command "linkClicked"
put it into a variable named theText
use that variable as a parameter with the linkClicked command
end linkClicked

This also suggests that there is no essential difference between a command and a handler. A command seems simply to be a handler defined by RR, instead of defined by the user. Is that true?

Apparently, many standard commands send some kind of parameter -- possibly all commands. I dunno. I've learned this by trial and error, also

If I make a button with the script

on mouseUp anyOldParameter
answer anyOldParameter
end mouseup

I get an answer of "1" when I click on the mouse. I didn't know what this parameter is supposed to tell me until one minute ago, when I found it in the documentation under mouseUp. Surprise!

But then there are functions.

If I put "mary had a little lamb" into a field, lock the field and put

on mouseUp aParameter
answer aParameter
end mouseup

and click on the field, my answer is "1"

On the other hand, if the script says

on mouseUp
answer the mouseChunk
end mouseUp

My answer is something like "char 12 to 14 of field 48"

I guess the mouseChunk is a function. I don't see any mention of the mouseChunk function in the documentation for mouseUp. I can look it up in the docs, if I know it exists. Otherwise, how would I know about it? I guess I could read about every native function, but it seems like there are hundreds.

So, it seems like one or more functions and/or one or more parameters are associated with at least some commands, and possibly every command.

I've known about "the target". I guess that's a function. I've just used it by rote.

Was hyperCard the same way, in all these respects?

I've learned a few things in the process of composing this query. But I have no big-picture idea what parameters and functions are assocated with what commands. Maybe the documentation does mention the parameter or parameters sent with every command. I hadn't noticed before.

I could spend a long, long time looking up parameters sent with every command I know, and even more time looking up functions associated with every command I know. And I wouldn't remember all of that.

Is there some kind of chart that summarizes all of this? You know -- the command in column 1, parameter(s) sent with command in column 2, all functions associated with that command in column three? Something like that? I guess it would have to be hyperlinked so I could find out what information the parameters and functions contain, by clicking on them. That would be quite handy, if it exists.

I still don't get the difference between a parameter and a function when associated with a standard command. Why is the number of the mouse button clicked a parameter, whereas the mouseChunk is a function? It seems like it could just as easily be the other way around.

I'm going to look again at Danny Goodman's HC book and Dan's RR book. Maybe it's all in there and I just didn't absorb it when I studied them.

The reader is probably scratching his/her head, wondering how in the world to reply to this messy comment/query. Miscellaneous comments, attempts to educate, and directions to sources, even if minimal, would all be appreciated.

I wonder how highly skilled RR users learn all of this. I thought I knew a few things about scripting. Turns out I don't know hardly nothin.

Tim
(feeling foolish at the moment)

Re: Parameters vs. functions -- I don't get it

Posted: Fri Nov 03, 2006 6:27 pm
by DarScott
Timothy wrote:Turns out I don't know hardly nothin.
Don't we all!

The .pdf file that comes with recent versions of Rev should be helpful here. Open the documentation (from the Help menu or the Documentation icon) and click on User Guide. Click again to open the .pdf file.

You can jump to section 5.4 or maybe print out a copy and read the first sections while eating lunch or hiding in the closet waiting for the museum to close.

You can also skim over the right section of your old Hypercard reference or get Dan Shafer's Revolution book.

Quick summary:

The values passed to a command, including a custom command you define, are called parameters (or arguments or actual parameters). The variables declared in the first line of an 'on' handler, where the above values will be copied, are called parameter variables (or parameters or formal parameters). So, in the body of the defined handler, you access parameters by these variables. When a custom command is used, the values are copied to the parameter variables in order. (We Revolutionaries often use the word "parameter" for both the value passed and the variable declared in the handler definition, so watch out.)

You can use all builtin commands and functions in any handler. You can also use your custom commands (if found in the message path). These are not specific to any handler. Some functions, such as mouseLoc(), tell you something about what is going on and some, such as sin(), help you compute.

For 'mouseUp' the button number is passed as a parameter (value). That value is copied to the parameter variable declared in your 'mouseUp' handler before the body of the handler is executed.

So, use the parameter variable to get what the caller gave the handler, and use a function to get info about what is going on.

Posted: Fri Nov 03, 2006 11:16 pm
by Timothy
Thanks. Good advice. I'll do it.

Tim

Re: Parameters vs. functions -- I don't get it

Posted: Tue Nov 07, 2006 10:39 am
by marielle
I still don't get the difference between a parameter and a function when associated with a standard command. Why is the number of the mouse button clicked a parameter, whereas the mouseChunk is a function? It seems like it could just as easily be the other way around.
Two things there: (1) function vs. command... when to use them? and (2) parameters vs result (or data that are given to influence the processing of a function or handler vs data that are the final result of the processing of a function)

function -> used when it is required to return a value (mousechunk returns the chunk). The basic idea of a function is that it performs some transformation of data. Some input is received, this input is being processed and transformed within the function, an output is returned.

command/handler (on) -> The names are helpful to understand the functionality. When you give a command, you don't expect the person to discuss your instructions, only to obtemperate (promptly obey). A handler is therefore used when no more is needed than process the current event/call. mousedown -> the part of the script that called to the mousedown doesn't need to receive any information back from the handler.

In other words:

Code: Select all

function doSomething input_data
  -- doSomething
  return output_data  -- to the part of the script 
                               -- that made a call to me
end doSomething
------
on doSomethingElse input_data
  -- doSomethingElse
end doSomethingElse
Because of their differences in functionality, functions and handlers are triggered differently.

Code: Select all

put doSomething("data to process") into result_of_data_transformation
---
get doSomething("data to process") 
put it into result_of_data_transformation
but

Code: Select all

doSomethingElse "data to process"

Re: Parameters vs. functions -- I don't get it

Posted: Mon Nov 27, 2006 3:13 pm
by Mark Smith
Timothy wrote:I guess the mouseChunk is a function. I don't see any mention of the mouseChunk function in the documentation for mouseUp. I can look it up in the docs, if I know it exists. Otherwise, how would I know about it? I guess I could read about every native function, but it seems like there are hundreds.
You're right. It's a pretty big language, and learning each and every built in function and command is a tall order.

What I have sometimes done in cases like the above, is just type "mouse" into the dictionary and have a look through various entries, noting which ones seem relevant to the task at hand. Over the years, I've got familiar with a lot of it, but still find new (to me) things regularly, either in the docs, on the list or here in the forums.

Best,

Mark