Passing multiple variables into a functon

Is it posible to pass more than 1 variable into a function that returns a value?

I can pass in many variable into a handle that does not return anyhing, but I have not found a way to pass multiple variable into a function.

Put (“ImageName”, 10) into Variable

I can send in a list as exampled above, but what I want to do is something like below.

Script.Function(“Start”, 10)

It treats the above as:

Script.Function(“Start”) and ignores the 10.

Any thoughts.

Actually, this code:

Script.Function("Start", 10)

is working or rather it would be if you were calling the function correctly. The problem with this line (and I just made the same mistake myself when throwing together some test code) is that you’re calling a function, but you’re not doing anything with the return value; a function must always be called as part of a larger statement so that the return value is handled. This line could be fixed by simply printing the return value:

put Script.Function("Start", 10)

Then you could access the second parameter by putting the following line in the function:

put param(2)

which should output the “10” that you passed in (param(0) returns the name of the handler.) All parameters are optional, and you can actually pass any number of parameters to a handler and access them dynamically with code like:

repeat with each item of the parameterlist
     put it
     // do whatever you need to with the parameter
end repeat

// or you might do something conditionally based on whether a parameter is populated
if param(3) is not empty then
     set the CaseSensitive to YES
end if

However, if you know how many parameters you want to pass and what you’re going to be doing with them, the easier approach is to declare them:

function doStuff these, that, those // you can use almost anything as variable names
    Click these
    MoveTo that
    return those times 2 
end doStuff

Hope that helps.

Thank you for the response. I was using the function calls in If statemens and the function was returning true / false to support the if statement.

When I used the following:

If Script.Function(“Start”, 10)

The structure of the function was

Function Var1, Var2
Do stuff
Return True or False conditional
end Function

I found that the following call will work with the previous Function setup:

If Script.Function (“Start”), (10)

Another question:

I perform a lot of loops in my scripts and I get tired of incremnting and decrementing my loop counters.

Put Counter+1 into Counter

Is there any inc / dec functions in the language?

Inc(Counter) or Dec(Counter) or Counter+1 or Counter-1 or Counter++ or Counter–

These functions are in most higher level languages, but I have not found them in Eggplant.

This should work and is the correct syntax:

if Script.Function("Start", 10)

This may “work”, but is not the correct syntax for a function call:

If Script.Function ("Start"), (10)

The second example is demonstrably incorrect and would work only in special situations, if at all. I created the following function:

function doIt
    put "the parameterList is:" && the parameterlist
    put "param 2 is:" && param(2)
    return the number of characters in param(1) is param(2)
end doIt

When it is called using the correct syntax:

put doIt ("parakeet", 8)

The output from the script is:

Thu, 10/7/10 6:32:53 AM    START        Running funcTest.script
the parameterList is: (parakeet,8)
param 2 is: 8
Thu, 10/7/10 6:32:53 AM    SUCCESS        Execution Time 0:00:00 funcTest.script

When I run it using this syntax:

put doIt ("parakeet"),(8)

the output is:

Thu, 10/7/10 6:39:39 AM    START        Running funcTest.script
the parameterList is: (parakeet)
param 2 is: 
Thu, 10/7/10 6:39:40 AM    SUCCESS        Execution Time 0:00:00 funcTest.script

This only works at all because it’s treating the comma as the concatenation operator and doing a put of the return value of the function with a single argument with the value 8 concatenated to that output. As the above demonstrates, only the first value is being passed to the function. If I use the same syntax in an “if” statement it doesn’t even parse. Declaring the function parameters makes no difference.

On the question of incrementing and decrementing counters, I think eggPlant goes most languages one better – it has a built-in counter function: repeatIndex() (or “the repeatIndex”). This code:

repeat 2 times
    put "outer loop:" && repeatIndex()
    repeat 3 times
        put "inner loop:" && the repeatIndex
    end repeat
end repeat

produces the following output:

Thu, 10/7/10 7:01:08 AM    START        Running Selection from funcTest.script
outer loop: 1
inner loop: 1
inner loop: 2
inner loop: 3
outer loop: 2
inner loop: 1
inner loop: 2
inner loop: 3
Selection Executed in 0:00:00

So in most instances you don’t need to create or increment your own counter. There is no decrement equivalent, although you could always subtract the repeatIndex from a starting value:

put 239 into theCount
     // do something
     if theCount minus the repeatIndex is 0 then exit repeat
end repeat

You are correct that there is no increment or decrement shorthand; we would generally handle that with the following code:

add 1 to theCount
subtract 1 from theCount
//the following would also work
add -1 to theCount

I hope this information is helpful to you.

I wanted to add one other tip: You can have a repeat loop count down by doing something like this:

put ("cat", "dog", "fish", "bird", "snake", "lizard", "ferret", "buffalo") into myPets
repeat with n = the number of items in myPets down to 1
    put item(n) of myPets
end repeat