Call a function: Difference between revisions

no edit summary
m (Ol associative array syntax fix)
No edit summary
Line 4,387:
 
* Call-by-name parameters use a function in first-class context. The function [http://seed7.sourceforge.net/examples/map.htm doMap] from the examples section of the Seed7 homepage uses a given expression to modify the elements of an array:<lang seed7>seq := doMap([](1, 2, 4, 6, 10, 12, 16), x, succ(x));</lang>
 
=={{header|SenseTalk}}==
<lang sensetalk>// If no variable is specified, `put` prints the variable to stdout
put zeroArgsFn()
 
// Running a function requires a keyword such as `put; if no variable is return, put into e.g. _
put TwoArgFn("a variable", (3, 4)) into _
 
// The parameter list is flexible, allowing any amount of variable to be passed in.
// These can be accessed with the keyword `the params`
// The specified parameters only limits to named parameters
// The first item of `the params` is the function name
put TwoArgFn("a variable", (3, 4), "hello") into _
 
// A parameter is set to '' if nothing is specified
put ThreeArgFn("a variable", (3, 4)) into _
 
// Using this, default parameter values can be set up if a check if done at the start of the function
put OneArgFn() into _
put OneArgFn(10) into _ --
 
// All variables are, by default, passed by value
put 3 into a
put AddOne(a) into _
put "Value of a = " & a
// Value of a = 3
 
// If the argument prefixed by 'container', the variable is passed by reference
put 5 into b
put AddOne(container b) into _
put "Value of b = " & b
// Value of b = 6
 
function zeroArgsFn
put "This function was run with zero arguments."
return "Return value from zero argument function"
end zeroArgsFn
 
function OneArgFn arg1
if arg1 is ""
set arg1 to 5
end if
put "One agument function; arg1 = " & arg1
end OneArgFn
 
function TwoArgFn arg1, arg2
put "2 argument function: arg1 = " & arg1 & "; arg2 = " & arg2
put "Parameters = " & the params
end TwoArgFn
 
function ThreeArgFn arg1, arg2, arg3
put "3 argument function: arg1 = " & arg1 & "; arg2 = " & arg2 & "; arg3 = " & arg3
end ThreeArgFn
 
function AddOne n
add 1 to n
end AddOne</lang>
 
=={{header|Sidef}}==