Jump to content

Call a function: Difference between revisions

Lingo added
(Lingo added)
Line 1,523:
'impossible
</lang>
 
=={{header|Lingo}}==
 
*Calling a function that requires no arguments
<lang lingo>foo()
-- or alternatively:
call(#foo, _movie)</lang>
 
*Calling a function with a fixed number of arguments
<lang lingo>foo(1,2,3)
-- or alternatively:
call(#foo, _movie, 1, 2, 3)</lang>
 
*Calling a function with optional arguments
<lang lingo>on foo (a, b)
if voidP(b) then b = 1
return a * b
end</lang>
<lang lingo>put foo(23, 2)
-- 46
put foo(23)
-- 23</lang>
 
*Calling a function with a variable number of arguments
<lang lingo>on sum ()
res = 0
repeat with i = 1 to the paramCount
res = res + param(i)
end repeat
return res
end</lang>
<lang lingo>put sum (1,2,3)
-- 6</lang>
 
*Calling a function with named arguments
Not directly supported, but you can of course re-write any function to only accept a single property list (hash) as argument, which can make sense e.g. for functions that have a lot of optional aruments.
 
*Using a function in statement context
*Using a function in first-class context within an expression
Lingo has no first-class functions, but the call(...) syntax (see above) allows to identify and use functions specified as "symbols" (e.g. #foo). This allows some "first-class alike" features:
<lang lingo>----------------------------------------
-- One of the five native iterative methods defined in ECMAScript 5
-- @param {list} tList
-- @param {symbol} cbFunc
-- @param {object} [cbObj=_movie]
-- @return {list}
----------------------------------------
on map (tList, cbFunc, cbObj)
if voidP(cbObj) then cbObj = _movie
res = []
cnt = tList.count
repeat with i = 1 to cnt
-- tList[i] = call(cbFunc, cbObj, tList[i])
res[i] = call(cbFunc, cbObj, tList[i], i, tList)
end repeat
return res
end
 
on doubleInt (n)
return n*2
end</lang>
<lang lingo>l = [1,2,3]
put map(l, #doubleInt)
-- [2, 4, 6]</lang>
 
*Obtaining the return value of a function
<lang lingo>x = foo(1,2)</lang>
 
*Distinguishing built-in functions and user-defined functions
In Lingo all user-defined (global) functions are 'methods' of the _movie object, and there is AFAIK no direct way to distinguish those from _movie's built-in functions. But by iterating over of all movie scripts in all castlibs you can get a complete list of all user-defined (global) functions, and then any function not in this list is a built-in function:
<lang lingo>on getAllUserFunctions ()
res = []
repeat with i = 1 to _movie.castlib.count
c = _movie.castlib(i)
repeat with j = 1 to c.member.count
m = c.member[j]
if m.type<>#script then next repeat
if m.scripttype=#movie then
functions = m.script.handlers()
repeat with f in functions
res.append(f)
end repeat
end if
end repeat
end repeat
return res
end</lang>
<lang lingo>put getAllUserFunctions()
-- [#sum, #double, #getAllUserFunctions]</lang>
 
*Distinguishing subroutines and functions
In Lingo functions (also called "handlers") don't have to return anything, so there is no distinction. The return value of a function without a "return ..." line is VOID, and not distinguishable from a function with the explicit line "return VOID".
 
*Stating whether arguments are passed by value or by reference
In lingo 'objects' are always passed by reference, all other types (e.g. strings, integers, floats) by value. 'Objects' are e.g. lists (arrays), property lists (hashes), images and script instances. The built-in function objectP() returns TRUE (1) for objects and FALSE (0) for non-objects. To prevent the effects of call-by-reference, some object types (lists, property lists and images) support the method duplicate() to clone the object before passing it to a function:
<lang lingo>on double (someList)
cnt = someList.count
repeat with i = 1 to cnt
someList[i] = someList[i] * 2
end repeat
end</lang>
<lang lingo>l = [1,2,3]
double(l)
put l
-- [2, 4, 6]
 
l = [1,2,3]
double(l.duplicate())
put l
-- [1, 2, 3]</lang>
 
=={{header|Little}}==
 
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.