Subprograms Akim Demaille, Etienne Renault, Roland Levillain April - - PowerPoint PPT Presentation

subprograms
SMART_READER_LITE
LIVE PREVIEW

Subprograms Akim Demaille, Etienne Renault, Roland Levillain April - - PowerPoint PPT Presentation

Subprograms Akim Demaille, Etienne Renault, Roland Levillain April 6, 2020 TYLA Subprograms April 6, 2020 1 / 61 Table of Contents Routines 1 Evaluation strategy (Argument Passing) 2 Return Statement 3 Fonctions as Values 4 TYLA


slide-1
SLIDE 1

Subprograms

Akim Demaille, Etienne Renault, Roland Levillain April 6, 2020

TYLA Subprograms April 6, 2020 1 / 61

slide-2
SLIDE 2

Table of Contents

1

Routines

2

Evaluation strategy (Argument Passing)

3

Return Statement

4

Fonctions as Values

TYLA Subprograms April 6, 2020 2 / 61

slide-3
SLIDE 3

Subprograms

At the origin, snippets copied and pasted from other sources

◮ Impact on memory management; ◮ Impact on separated compilation; ◮ Modular programming: first level of interface/abstraction.

First impact on Software Engineering: “top-down” conception, by refinements. Generalizations: modules and/or objects.

TYLA Subprograms April 6, 2020 3 / 61

slide-4
SLIDE 4

Table of Contents

1

Routines Procedures vs. Functions Hybridation: Procedure/Functions Default values and named Arguments

2

Evaluation strategy (Argument Passing)

3

Return Statement

4

Fonctions as Values

TYLA Subprograms April 6, 2020 4 / 61

slide-5
SLIDE 5

Procedures vs. Functions

Procedure Subprograms with no return value. Procedures have side effects Function Subprograms that return something. (Pure) Functions do not have side effects Ada, Pascal, . . . have two reserved keywords procedure and function BUT function generally describe subprograms with return values, while procedures do not return values Distinction sometimes blurred by the language: (e.g., using void ALGOL, C, Tiger...).

TYLA Subprograms April 6, 2020 5 / 61

slide-6
SLIDE 6

Procedures vs. Functions

Function Add(A, B : Integer) : Integer; Begin Add := A + B; End; Functions in Pascal Procedure finish(name: String ); Begin WriteLn(’Goodbye’, name ); End; Procedures in Pascal

TYLA Subprograms April 6, 2020 6 / 61

slide-7
SLIDE 7

Vocabulary

Formal Argument Arguments of a subprogram declaration. let function sum (x: int , y: int): int = x + y

TYLA Subprograms April 6, 2020 7 / 61

slide-8
SLIDE 8

Vocabulary

Formal Argument Arguments of a subprogram declaration. let function sum (x: int , y: int): int = x + y Effective Argument Arguments of a call to a subprogram. sum (40, 12)

TYLA Subprograms April 6, 2020 7 / 61

slide-9
SLIDE 9

Vocabulary

Formal Argument Arguments of a subprogram declaration. let function sum (x: int , y: int): int = x + y Effective Argument Arguments of a call to a subprogram. sum (40, 12) Parameter Please reserve it for templates.

TYLA Subprograms April 6, 2020 7 / 61

slide-10
SLIDE 10

Table of Contents

1

Routines Procedures vs. Functions Hybridation: Procedure/Functions Default values and named Arguments

2

Evaluation strategy (Argument Passing)

3

Return Statement

4

Fonctions as Values

TYLA Subprograms April 6, 2020 8 / 61

slide-11
SLIDE 11

Functions: Side effects

Using functions with side effects is very dangerous. For instance: foo = getc () + getc () * getc (); is undefined (= nondeterministic). On purpose!

TYLA Subprograms April 6, 2020 9 / 61

slide-12
SLIDE 12

Table of Contents

1

Routines Procedures vs. Functions Hybridation: Procedure/Functions Default values and named Arguments

2

Evaluation strategy (Argument Passing)

3

Return Statement

4

Fonctions as Values

TYLA Subprograms April 6, 2020 10 / 61

slide-13
SLIDE 13

Default arguments

int sum(int a, int b = 21, int c = 42, int d = 42){ return a + b + c + d; } Default Arguments in C++ sum(1, 2, 3, 4) is fine sum(1, 2) is also fine But what if we want to call sum (b = 1, a = 2) with c’s and d’s default value?

TYLA Subprograms April 6, 2020 11 / 61

slide-14
SLIDE 14

Named Argument (Some sugar)

In Ada, named arguments and/or default values: put (number : in float; before : in integer := 2; after : in integer := 2; exponent : in integer := 2) ... Some Ada function declaration put (pi , 1, 2, 3); put (pi , 1); put (pi , 2, 2, 4); put (pi , before => 2, after => 2, exponent => 4); put (pi , exponent => 4); Possible invocations

TYLA Subprograms April 6, 2020 12 / 61

slide-15
SLIDE 15

Named Arguments

Named parameters are availables in many languages: Perl, Python, C#, Fortran95, Go, Haskell, Lua, Ocaml, Lisp, Scala, Swift/ObjectiveC (fixed order of named parameters!), . . . No need to remember the order of parameters No need to guess specific default’s values More Flexible Clarity

TYLA Subprograms April 6, 2020 13 / 61

slide-16
SLIDE 16

Simulate Named Argument

Can we simulate named arguments in C++ or Java? Yes : Named parameter idiom uses a proxy object for passing the parameters.

TYLA Subprograms April 6, 2020 14 / 61

slide-17
SLIDE 17

Named Parameter Idiom 1/2

class foo_param{ private: int a = 0, b = 0; foo_param () = default; // make it private public: foo_param& with_a(int provided ){ a = provided; return *this; } foo_param& with_b(int provided ){ b = provided; return *this; } static foo_param create (){ return foo_param (); } };

TYLA Subprograms April 6, 2020 15 / 61

slide-18
SLIDE 18

Named Parameter Idiom 2/2

void foo(foo_param& f) { // ... } foo(foo_param :: create (). with_b (1) .with_a (2));

TYLA Subprograms April 6, 2020 16 / 61

slide-19
SLIDE 19

Named Parameter Idiom 2/2

void foo(foo_param& f) { // ... } foo(foo_param :: create (). with_b (1) .with_a (2)); Works ... but require one specific class per function For C++, Boost::Parameter library also offer a generic implementation

TYLA Subprograms April 6, 2020 16 / 61

slide-20
SLIDE 20

Table of Contents

1

Routines

2

Evaluation strategy (Argument Passing)

3

Return Statement

4

Fonctions as Values

TYLA Subprograms April 6, 2020 17 / 61

slide-21
SLIDE 21

Argument passing

From a naive point of view (and for strict evaluation), three possible modes: in, out, in-out. But there are different flavors. Val ValConst RefConst Res Ref ValRes Name ALGOL 60 * * Fortran ? ? PL/1 ? ? ALGOL 68 * * Pascal * * C * ? ? Modula 2 * ? Ada (simple types) * * * Ada (others) ? ? ? ? ? Alphard * * *

TYLA Subprograms April 6, 2020 18 / 61

slide-22
SLIDE 22

Table of Contents

1

Routines

2

Evaluation strategy (Argument Passing) Call by Value Call by Reference Call by Value-Result Call by Name Call by Need Summary A note on Call by sharing

3

Return Statement

4

Fonctions as Values

TYLA Subprograms April 6, 2020 19 / 61

slide-23
SLIDE 23

Call by Value – definition

Passing arguments to a function copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument. def foo(val): val = 1 i = 12 print (i) Call by value in Python – output: 12

TYLA Subprograms April 6, 2020 20 / 61

slide-24
SLIDE 24

Pros & Cons

Safer: variables cannot be accidentally modified Copy: variables are copied into formal parameter even for huge data Evaluation before call: resolution of formal parameters must be done before a call

◮ Left-to-right: Java, Common Lisp, Effeil, C#, Forth ◮ Right-to-left: Caml, Pascal ◮ Unspecified: C, C++, Delphi, , Ruby TYLA Subprograms April 6, 2020 21 / 61

slide-25
SLIDE 25

Table of Contents

1

Routines

2

Evaluation strategy (Argument Passing) Call by Value Call by Reference Call by Value-Result Call by Name Call by Need Summary A note on Call by sharing

3

Return Statement

4

Fonctions as Values

TYLA Subprograms April 6, 2020 22 / 61

slide-26
SLIDE 26

Call by Reference – definition

Passing arguments to a function copies the actual address of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function will have effect on the argument. void swap(int &x, int &y) { int aux = x; x = y; y = aux; } int main () { int x = 2, y = 3; swap(a, b); printf("%d,%d\n", x, y); } Call by reference in C++ – output: 3 2

TYLA Subprograms April 6, 2020 23 / 61

slide-27
SLIDE 27

Pros & Cons

Faster than call-by-value if data structure have a large size. Readability & Undesirable behavior: a special attention may be considered when doing operations on multiple references since they can all refer to the same object void xor_swap(int &x, int &y) { x = x ^ y; y = x ^ y; x = x ^ y; } Call by reference in C++ may lead to undesirable behavior when x and y refers the same object (zeroing x and y)

TYLA Subprograms April 6, 2020 24 / 61

slide-28
SLIDE 28

Notes on call-by-reference

swap(foo, foo) is forbidden in Pascal but what about swap(foo[bar], foo[baz]) ...

TYLA Subprograms April 6, 2020 25 / 61

slide-29
SLIDE 29

Table of Contents

1

Routines

2

Evaluation strategy (Argument Passing) Call by Value Call by Reference Call by Value-Result Call by Name Call by Need Summary A note on Call by sharing

3

Return Statement

4

Fonctions as Values

TYLA Subprograms April 6, 2020 26 / 61

slide-30
SLIDE 30

Call by Value-Result – definition

Passing arguments to a function copies the argument into the formal parameter of the function. The values are then copied back when exiting the function In this case, changes made to the parameter inside the function will

  • nly reflect on the argument at the end of the function.

TYLA Subprograms April 6, 2020 27 / 61

slide-31
SLIDE 31

Call by Value-Result – Example

procedure Tryit is procedure swap (i1 , i2: in out integer) is tmp: integer; begin tmp := i1; i1 := i2; i2 := tmp; end swap; a : integer := 1; b : integer := 2; begin swap(a, b); Put_Line(Integer ’Image (a) & "" & Integer ’Image (b)) ; end Tryit; Call by Value-result in Ada – output: 2 1

TYLA Subprograms April 6, 2020 28 / 61

slide-32
SLIDE 32

Notes on call-by-value-result

Pros & Cons Safety other thread will only see consistent values since changes made will not show up until after the end of the function. Local copies: but they can be sometimes avoided by the compiler

TYLA Subprograms April 6, 2020 29 / 61

slide-33
SLIDE 33

Notes on call-by-value-result

Pros & Cons Safety other thread will only see consistent values since changes made will not show up until after the end of the function. Local copies: but they can be sometimes avoided by the compiler Remarks:

TYLA Subprograms April 6, 2020 29 / 61

slide-34
SLIDE 34

Notes on call-by-value-result

Pros & Cons Safety other thread will only see consistent values since changes made will not show up until after the end of the function. Local copies: but they can be sometimes avoided by the compiler Remarks: Also called: Call by copy-restore, Call by copy-in copy-out

TYLA Subprograms April 6, 2020 29 / 61

slide-35
SLIDE 35

Notes on call-by-value-result

Pros & Cons Safety other thread will only see consistent values since changes made will not show up until after the end of the function. Local copies: but they can be sometimes avoided by the compiler Remarks: Also called: Call by copy-restore, Call by copy-in copy-out If the reference is passed to the callee uninitialized, this evaluation strategy is called call by result.

TYLA Subprograms April 6, 2020 29 / 61

slide-36
SLIDE 36

Notes on call-by-value-result

Pros & Cons Safety other thread will only see consistent values since changes made will not show up until after the end of the function. Local copies: but they can be sometimes avoided by the compiler Remarks: Also called: Call by copy-restore, Call by copy-in copy-out If the reference is passed to the callee uninitialized, this evaluation strategy is called call by result. Used in multiprocessing contexts.

TYLA Subprograms April 6, 2020 29 / 61

slide-37
SLIDE 37

Notes on call-by-value-result

Pros & Cons Safety other thread will only see consistent values since changes made will not show up until after the end of the function. Local copies: but they can be sometimes avoided by the compiler Remarks: Also called: Call by copy-restore, Call by copy-in copy-out If the reference is passed to the callee uninitialized, this evaluation strategy is called call by result. Used in multiprocessing contexts. Multiple interpretations:

◮ Ada: Evaluates arguments once, during function call ◮ AlgolW: Evaluates arguments during call AND when exiting the

function

TYLA Subprograms April 6, 2020 29 / 61

slide-38
SLIDE 38

Table of Contents

1

Routines

2

Evaluation strategy (Argument Passing) Call by Value Call by Reference Call by Value-Result Call by Name Call by Need Summary A note on Call by sharing

3

Return Statement

4

Fonctions as Values

TYLA Subprograms April 6, 2020 30 / 61

slide-39
SLIDE 39

An outsider: call by name

(In ALGOL 60) It behaves as a macro would, including with name captures: the argument is evaluated at each use. Try to write some code which results in a completely different result had SWAP been a function. #define SWAP(Foo , Bar) \ do { \ int tmp_ = (Foo); \ (Foo) = (Bar); \ (Bar) = tmp_; \ } while (0) In ALGOL 60, a compiled language, “thunks” were introduced: snippets of code that return the l-value when evaluated.

TYLA Subprograms April 6, 2020 31 / 61

slide-40
SLIDE 40

An application of call by name: Jensen’s Device

General computation of a sum of a series u

k=l ak:

real procedure Sum(k, l, u, ak) value l, u; integer k, l, u; real ak; comment ‘k’ and ‘ak ’ are passed by name; begin real s; s := 0; for k := l step 1 until u do s := s + ak; Sum := s end;

Computing the first 100 terms of a real array V[]:

Sum(i, 1, 100, V[i])

TYLA Subprograms April 6, 2020 32 / 61

slide-41
SLIDE 41

Table of Contents

1

Routines

2

Evaluation strategy (Argument Passing) Call by Value Call by Reference Call by Value-Result Call by Name Call by Need Summary A note on Call by sharing

3

Return Statement

4

Fonctions as Values

TYLA Subprograms April 6, 2020 33 / 61

slide-42
SLIDE 42

Call by Need

Call by need is a memoized variant of call by name where, if the function argument is evaluated, that value is stored for subsequent uses. The argument is then evaluated only once, during its first use. What if y = 0 in the following code?

let function loop (z: int):int = if z > 0 then z else loop (z) function f (x: int):int = if y > 8 then x else -y in /* ≡ ‘if y > 8 then loop (y) else -y’ ? */ f (loop (y)) end

TYLA Subprograms April 6, 2020 34 / 61

slide-43
SLIDE 43

Call by name Don’t pass the evaluation of the expression, but a “thunk” computing it: let var a := 5 + 7 in a + 10 end ==> let function a () := 5 + 7 in a () + 10 end Call by need The thunk is evaluated once and only once. Add a “memo” field.

TYLA Subprograms April 6, 2020 35 / 61

slide-44
SLIDE 44

Lazy evaluation 1

easydiff f x h = (f (x + h) - f (x)) / h repeat f a = a : repeat f (f a) halve x = x / 2 differentiate h0 f x = map (easydiff f x) (repeat halve within eps (a : b : rest) | abs (b - a) <= eps = b | otherwise = within eps (b : rest) relative eps (a : b : rest )) = | abs (b - a) <= eps * abs b = b | otherwise = relative eps (b : rest) within eps (differentiate h0 f x)

Slow convergence. . . Suppose the existence of an error term:

a (i) = A + B * (2 ** n) * (h ** n) a (i + 1) = A + B * (h ** n)

TYLA Subprograms April 6, 2020 36 / 61

slide-45
SLIDE 45

Table of Contents

1

Routines

2

Evaluation strategy (Argument Passing) Call by Value Call by Reference Call by Value-Result Call by Name Call by Need Summary A note on Call by sharing

3

Return Statement

4

Fonctions as Values

TYLA Subprograms April 6, 2020 37 / 61

slide-46
SLIDE 46

Exhibit the differences (Explicit lyrics. . . )

var t : integer foo: array [1..2] of integer; procedure shoot_my (x : Mode integer ); begin foo [1] := 6; t := 2; x := x + 3; end; begin foo [1] := 1; foo [2] := 2; t := 1; shoot_my (foo[t]); end.

TYLA Subprograms April 6, 2020 38 / 61

slide-47
SLIDE 47

Exhibit the differences (Explicit lyrics. . . )

var t : integer foo: array [1..2] of integer; procedure shoot_my (x : Mode integer ); begin foo [1] := 6; t := 2; x := x + 3; end; begin foo [1] := 1; foo [2] := 2; t := 1; shoot_my (foo[t]); end.

Mode

foo[1] foo[2] t Val

TYLA Subprograms April 6, 2020 38 / 61

slide-48
SLIDE 48

Exhibit the differences (Explicit lyrics. . . )

var t : integer foo: array [1..2] of integer; procedure shoot_my (x : Mode integer ); begin foo [1] := 6; t := 2; x := x + 3; end; begin foo [1] := 1; foo [2] := 2; t := 1; shoot_my (foo[t]); end.

Mode

foo[1] foo[2] t Val 6 2 2 Val-Res (ALGOL W)

TYLA Subprograms April 6, 2020 38 / 61

slide-49
SLIDE 49

Exhibit the differences (Explicit lyrics. . . )

var t : integer foo: array [1..2] of integer; procedure shoot_my (x : Mode integer ); begin foo [1] := 6; t := 2; x := x + 3; end; begin foo [1] := 1; foo [2] := 2; t := 1; shoot_my (foo[t]); end.

Mode

foo[1] foo[2] t Val 6 2 2 Val-Res (ALGOL W) 6 4 2 Val-Res (Ada)

TYLA Subprograms April 6, 2020 38 / 61

slide-50
SLIDE 50

Exhibit the differences (Explicit lyrics. . . )

var t : integer foo: array [1..2] of integer; procedure shoot_my (x : Mode integer ); begin foo [1] := 6; t := 2; x := x + 3; end; begin foo [1] := 1; foo [2] := 2; t := 1; shoot_my (foo[t]); end.

Mode

foo[1] foo[2] t Val 6 2 2 Val-Res (ALGOL W) 6 4 2 Val-Res (Ada) 4 2 2 Ref

TYLA Subprograms April 6, 2020 38 / 61

slide-51
SLIDE 51

Exhibit the differences (Explicit lyrics. . . )

var t : integer foo: array [1..2] of integer; procedure shoot_my (x : Mode integer ); begin foo [1] := 6; t := 2; x := x + 3; end; begin foo [1] := 1; foo [2] := 2; t := 1; shoot_my (foo[t]); end.

Mode

foo[1] foo[2] t Val 6 2 2 Val-Res (ALGOL W) 6 4 2 Val-Res (Ada) 4 2 2 Ref 9 2 2 Name

TYLA Subprograms April 6, 2020 38 / 61

slide-52
SLIDE 52

Exhibit the differences (Explicit lyrics. . . )

var t : integer foo: array [1..2] of integer; procedure shoot_my (x : Mode integer ); begin foo [1] := 6; t := 2; x := x + 3; end; begin foo [1] := 1; foo [2] := 2; t := 1; shoot_my (foo[t]); end.

Mode

foo[1] foo[2] t Val 6 2 2 Val-Res (ALGOL W) 6 4 2 Val-Res (Ada) 4 2 2 Ref 9 2 2 Name 6 5 2

TYLA Subprograms April 6, 2020 38 / 61

slide-53
SLIDE 53

Table of Contents

1

Routines

2

Evaluation strategy (Argument Passing) Call by Value Call by Reference Call by Value-Result Call by Name Call by Need Summary A note on Call by sharing

3

Return Statement

4

Fonctions as Values

TYLA Subprograms April 6, 2020 39 / 61

slide-54
SLIDE 54

Call by Sharing – definition

Call by sharing implies that values in the language are based on

  • bjects rather than primitive types, i.e. that all values are ”boxed”

Differs from both call-by-value and call-by-reference. def f(list ): list.append (1) m = [] f(m) print(m) Call by sharing in Python –

  • utput: [1]

def f(list ): list = [1] m = [] f(m) print(m) Call by sharing in Python –

  • utput: []

TYLA Subprograms April 6, 2020 40 / 61

slide-55
SLIDE 55

Notes on Call-by-sharing

Mutations of arguments perforsmall by the called routine will be visible to the caller.

TYLA Subprograms April 6, 2020 41 / 61

slide-56
SLIDE 56

Notes on Call-by-sharing

Mutations of arguments perforsmall by the called routine will be visible to the caller. Access is not given to the variables of the caller, but merely to certain objects

TYLA Subprograms April 6, 2020 41 / 61

slide-57
SLIDE 57

Notes on Call-by-sharing

Mutations of arguments perforsmall by the called routine will be visible to the caller. Access is not given to the variables of the caller, but merely to certain objects Can be seen as ”call by value” in the case where the value is an

  • bject reference

TYLA Subprograms April 6, 2020 41 / 61

slide-58
SLIDE 58

Notes on Call-by-sharing

Mutations of arguments perforsmall by the called routine will be visible to the caller. Access is not given to the variables of the caller, but merely to certain objects Can be seen as ”call by value” in the case where the value is an

  • bject reference

First introduced by Barbara Liskov for CLU language (1974) Widely used by: Python, Java, Ruby, JavaScript, Scheme, OCaml, AppleScript, ... call by sharing is not in common use; the terminology is inconsistent across different sources.

TYLA Subprograms April 6, 2020 41 / 61

slide-59
SLIDE 59

Table of Contents

1

Routines

2

Evaluation strategy (Argument Passing)

3

Return Statement

4

Fonctions as Values

TYLA Subprograms April 6, 2020 42 / 61

slide-60
SLIDE 60

Return Statement

What is the purpose of the return statement? Is there a best way to return something? Is there a best way to return something?

TYLA Subprograms April 6, 2020 43 / 61

slide-61
SLIDE 61

Table of Contents

1

Routines

2

Evaluation strategy (Argument Passing)

3

Return Statement return via dedicated keyword return via function’s name return via specific variable return the last computed value named return values

4

Fonctions as Values

TYLA Subprograms April 6, 2020 44 / 61

slide-62
SLIDE 62

Return via a dedicated keyword 1/2

int compute(int a, int b) { int res = a+b; // Some computation return res; } C’s return statement uses the return keyword int compute(int a, int b) { int r_val = a+b; // Some computation return r_val; } Java’s return statement also uses the return keyword

TYLA Subprograms April 6, 2020 45 / 61

slide-63
SLIDE 63

Return via a dedicated keyword 2/2

The return statement breaks the current fonction (also for C++, Java, Ada, Modula2). Clarity Complexify the code

◮ No naming convention ◮ No homogeneous return inside a given fonction ◮ Blur the comprehension via initialisation, intermediate

computation, . . .

TYLA Subprograms April 6, 2020 46 / 61

slide-64
SLIDE 64

Table of Contents

1

Routines

2

Evaluation strategy (Argument Passing)

3

Return Statement return via dedicated keyword return via function’s name return via specific variable return the last computed value named return values

4

Fonctions as Values

TYLA Subprograms April 6, 2020 47 / 61

slide-65
SLIDE 65

Return via function’s name

function sum (a, b: integer ): integer; begin sum := a + b; end; Pascal’s return statement uses the name of the function

TYLA Subprograms April 6, 2020 48 / 61

slide-66
SLIDE 66

Return via function’s name

function sum (a, b: integer ): integer; begin sum := a + b; end; Pascal’s return statement uses the name of the function The name of the function is treated as a variable name (also for Fortran, ALGOL, ALGOL68, Simula) The “return” may not be the latest statement Ambiguous

◮ For recursion sum denotes a variable AND a function ◮ Is somevar := sum legal? (Yes for Pascal, No for Fortan) TYLA Subprograms April 6, 2020 48 / 61

slide-67
SLIDE 67

Table of Contents

1

Routines

2

Evaluation strategy (Argument Passing)

3

Return Statement return via dedicated keyword return via function’s name return via specific variable return the last computed value named return values

4

Fonctions as Values

TYLA Subprograms April 6, 2020 49 / 61

slide-68
SLIDE 68

Return via a specific variable (1/2)

always_true : BOOLEAN do Result := true end always_one : INTEGER do Result := 1 end always_bar : STRING do Result := "bar" end Effeil’s return statement uses the keyword Result

TYLA Subprograms April 6, 2020 50 / 61

slide-69
SLIDE 69

Return via a specific variable (2/2)

The value returned by a function is whatever value is in Result when the function ends. The return value of a feature is set by assigning it to the Result variable (initialised automatically to a default value). Unlike other languages, the return statement does not exist. Only in Effeil (to my knowledge) Clarity Ambiguous if the langage support nested fonctions

TYLA Subprograms April 6, 2020 51 / 61

slide-70
SLIDE 70

Table of Contents

1

Routines

2

Evaluation strategy (Argument Passing)

3

Return Statement return via dedicated keyword return via function’s name return via specific variable return the last computed value named return values

4

Fonctions as Values

TYLA Subprograms April 6, 2020 52 / 61

slide-71
SLIDE 71

Return the last computed value 1 /2

(defun double (x) (* x 2)) Lisp’s return value is the last computed value fn is_divisible_by(lhs: u32, rhs: u32) -> bool { if rhs == 0 { return false; } // The ‘return ‘ keyword isn ’t necessary lhs % rhs == 0 } For expressions, Rust’s return value is the last computed value

TYLA Subprograms April 6, 2020 53 / 61

slide-72
SLIDE 72

Return the last computed value 2 /2

In expression-oriented programming language (also Lisp, Perl, Javascript and Ruby) the return statement can omitted. Instead that the last evaluated expression is the return value. A ”last expression” is mandatory in Rust If no “return” Python returns None and Javascript undefined

TYLA Subprograms April 6, 2020 54 / 61

slide-73
SLIDE 73

Table of Contents

1

Routines

2

Evaluation strategy (Argument Passing)

3

Return Statement return via dedicated keyword return via function’s name return via specific variable return the last computed value named return values

4

Fonctions as Values

TYLA Subprograms April 6, 2020 55 / 61

slide-74
SLIDE 74

Named return values and Naked return

func make(r int, i int) (re int, im int) { re = r im = i return } Go combines Named returns values and naked return

TYLA Subprograms April 6, 2020 56 / 61

slide-75
SLIDE 75

Named return values and Naked return

func make(r int, i int) (re int, im int) { re = r im = i return } Go combines Named returns values and naked return No declaration/initialisation in the body of the function It serves as documentation. Functions that return multiple values are hard to name clearly GetUsernameAndPassword The signature of the function is slightly more difficult to read

TYLA Subprograms April 6, 2020 56 / 61

slide-76
SLIDE 76

Table of Contents

1

Routines

2

Evaluation strategy (Argument Passing)

3

Return Statement

4

Fonctions as Values

TYLA Subprograms April 6, 2020 57 / 61

slide-77
SLIDE 77

Subprograms as arguments

function diff (f(x: real ): real , x, h: real) : real; begin if h = 0 then slope := 0 else slope := (f (x + h) - f (x)) / h; diff := slope end begin ... diff (sin , 1, 0.01); ... end Typing difficulties ignored in ALGOL 60, Fortran,

  • riginal Pascal and C: the

function-argument was not typed. Today function types are available in most languages (except in some

  • ol).

Doesn’t exist in Ada. Simulated by a function parametrized routine. But you have to instantiate. . .

TYLA Subprograms April 6, 2020 58 / 61

slide-78
SLIDE 78

Anonymous subprograms

In all the functional languages, but not only (see automake). . .

use Getopt :: Long; Getopt :: Long :: config ("bundling", "pass_through"); Getopt :: Long :: GetOptions ( ’version ’ => &version , ’help ’ => &usage , ’libdir:s’ => $libdir , ’gnu’ => sub { set_strictness (’gnu’); }, ’gnits ’ => sub { set_strictness (’gnits ’); } ’cygnus ’ => $cygnus_mode , ’foreign ’ => sub { set_strictness (’foreign ’); ’include -deps ’ => sub { $use_dependencies = 1; }, ’i|ignore -deps ’ => sub { $use_dependencies = 0; }, ’no -force ’ => sub { $force_generation = 0; }, ’o|output -dir:s’ => $output_directory , ’v|verbose ’ => $verbose , )

  • r exit 1;

TYLA Subprograms April 6, 2020 59 / 61

slide-79
SLIDE 79

Environment capture

Functional languages with block structure. let type intfun = int -> int function add (n: int) : intfun = let function res (m: int): int = n + m in var addFive : intfun := add (5) var addTen := add (10) var twenty := addTen (addFive (5)) in twenty = 20 end Create closures: a pointer to the (runtime) environment in addition to a pointer to the code. Somewhat hard to implement [Chap. 15](appel.98.modern).

TYLA Subprograms April 6, 2020 60 / 61

slide-80
SLIDE 80

TYLA Subprograms April 6, 2020 61 / 61