8.6. Call by Value
You may be wondering what happens to a parameter passed to a handler if you assign a value to it inside the handler. (Okay, so you're probably not wondering what happens, but follow along anyway!) The next Try It Out defines two simple handlers to illustrate some important points.
8.6.1.
8.6.1.1. Try It Out: Understanding Call by Value
In this program, you see how to call a parameter by value.
Type the following program into Script Editor:
-- Illustrating Call by Value on testHandler(x) set x to 100 end testHandler on testHandler2(L1, L2) set end of L1 to 4 set L2 to {100, 200, 300} end testHandler2 set y to 50 testHandler(y) log y set list1 to {1, 2, 3} set list2 to {4, 5, 6} testHandler2(list1, list2) log list1 log list2
Click the Event Log tab and run the program. Here's what should be logged:
(*50*) (*1, 2, 3, 4*) (*4, 5, 6*)
8.6.1.2. How it Works
As mentioned, these handlers are very simple. The first one simply sets the value of its argument to 100:
on testHandler(x) set x to 100 end testHandler
Of course, the question is what happens to the value of a variable passed to this handler. Will its value get changed as well? This question is easily answered by assigning a value to a variable, calling the handler, and then examining the variable's value after the handler returns:
set y to 50 testHandler(y) log y
If you look at the log, you see that the value of y did not change. Its value remains at 50 after the handler returns. That's because arguments are ...
Get Beginning AppleScript® now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.