This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
script:walkthrough:functions [2022-09-27 04:20] – skyjake | script:walkthrough:functions [2022-09-27 04:24] (current) – skyjake | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | <- Records ^ ^ Scopes -> | ||
+ | |||
+ | ====== Functions ====== | ||
+ | |||
+ | A function is defined in the current scope using a '' | ||
+ | |||
+ | def func1(): pass | ||
+ | | ||
+ | def func2() | ||
+ | end | ||
+ | |||
+ | The '' | ||
+ | |||
+ | def func3(): return ' | ||
+ | print ' | ||
+ | |||
+ | |||
+ | ===== Arguments ===== | ||
+ | |||
+ | Functions can be defined with a list of untyped arguments. | ||
+ | |||
+ | def func5(a, b, c) | ||
+ | |||
+ | Default values can be included. | ||
+ | |||
+ | def func5(a, b=' | ||
+ | |||
+ | When calling a function, all arguments must have values. Argument names can always be included in the function call, regardless of whether they were defined with default values or not. When using names, the order can also be changed. | ||
+ | |||
+ | func5(c=5, a=100, b=' | ||
+ | |||
+ | |||
+ | ===== Records as arguments ===== | ||
+ | |||
+ | Records are passed by non-owned reference in arguments. | ||
+ | |||
+ | record myrec | ||
+ | myrec.val = ' | ||
+ | print myrec.val | ||
+ | def modify(r) | ||
+ | r.val = ' | ||
+ | end | ||
+ | modify(myrec) | ||
+ | print myrec.val | ||
+ | |||
+ | Output: | ||
+ | |||
+ | Original | ||
+ | Changed | ||
+ | |||
+ | Note that records are always passed by reference, even when specifically passing in an owned reference as argument. In the example below, the created record exists only for the duration of the function call. The function' | ||
+ | |||
+ | def do_init(rec) | ||
+ | rec.val = ' | ||
+ | return rec | ||
+ | end | ||
+ | # The following should be avoided: | ||
+ | a = do_init(Record()) | ||
+ | |||
+ | < | ||
+ | |||
+ | ===== Records as return value ===== | ||
+ | |||
+ | Records are passed by non-owned reference in the return value. This means that if a record is created as a local variable, it will be deleted even when it used as a return value (!). | ||
+ | |||
+ | def func4() | ||
+ | record a # ' | ||
+ | a.value = " | ||
+ | return a # non-owned reference returned | ||
+ | end # scope of ' | ||
+ | print func4().value | ||
+ | |||
+ | In this case, one could instead use the '' | ||
+ | |||
+ | return Record(a) | ||
+ | |||
+ | ===== Defining into a record ===== | ||
+ | |||
+ | The '' | ||
+ | |||
+ | def myrec.func5(a, | ||
+ | return a + b | ||
+ | end | ||
+ | |||
+ | In fact, '' | ||
+ | - Look up //myrec//. | ||
+ | - Create a variable called //func5// within //myrec//. | ||
+ | - Create a function object taking arguments (a, b) and assign it as the value of the variable // | ||
+ | |||
+ | Since functions are actually variables that reference function objects, function definitions can be made within the scope of a function. | ||
+ | |||
+ | def func6() | ||
+ | def func7(): return 'local function' | ||
+ | end | ||
+ | |||
+ | //func7// is not visible outside //func6//, just like any other local variable. However, one could return //func7// out of //func6// and then it could be used just like a regular top-level function. | ||
+ | |||
+ | Functions defined inside another function do not have visibility to variables in the outer function. Instead, each function has its own local namespace and may access the module' | ||
+ | |||
+ | a = ' | ||
+ | def outer() | ||
+ | a = 100 | ||
+ | print ' | ||
+ | def inner() | ||
+ | print ' | ||
+ | end | ||
+ | inner() | ||
+ | end | ||
+ | outer() | ||
+ | |||
+ | Output: | ||
+ | |||
+ | outer: a = 100 | ||
+ | inner: a = global scope | ||
+ | |||
+ | |||
+ | ===== Function variables ===== | ||
+ | |||
+ | Since functions are just variables referencing function objects, one can trivially make an alias for a function with assignment. Unlike records, function objects are reference-counted, | ||
+ | |||
+ | $ def someFunc(): return ' | ||
+ | | ||
+ | $ other = someFunc | ||
+ | | ||
+ | $ print someFunc(), other() | ||
+ | works works | ||
+ | |||
+ | Functions can be passed as arguments. | ||
+ | |||
+ | def doCallback(a, | ||
+ | return cb(a, b) | ||
+ | end | ||
+ | doCallback(10, | ||