g(x+1) is not obviously legitimate in CBR- sub g {
- $_[0] = $_[0] + 1;
- }
- sub f {
- my $x = 1;
- g ($x);
- print ("x = $x\n");
- }
- f ();
- $ perl ./cbr.pl
- x = 2
- sub g {
- $_[0] = $_[0] + 1;
- }
- sub f {
- my $x = 1;
- g ($x + 1);
- print ("x = $x\n");
- }
- f ();
- $ perl ./cbr.pl
- x = 1
- sub g {
- my ($y) = @_;
- $y = $y + 1;
- }
- sub f {
- my $x = 1;
- g ($x);
- print ("x = $x\n");
- }
- f ();
- # x=1
- void g (int *p) {
- *p = *p + 1;
- }
- int main () {
- int x = 1;
- g (&x);
- printf ("x = %d\n", x);
- return 0;
- }
- // x=2
int& (unlike int*, creates references implicitly)- void g(int& y) {
- y = y + 1;
- }
- int main() {
- int x = 1;
- g(x);
- // x==2
- cout << "x = " << x << endl;
- return 0;
- }
- void g(int y) { // vs. void g(int& y)
- y = y + 1;
- }
- int main() {
- int x = 1;
- g(x);
- // x==1 vs. x==2 when void g(int& y)
- cout << "x = " << x << endl;
- return 0;
- }
ref int- class Test {
- static void g (ref int y) {
- y = y + 1;
- }
- static void Main () {
- int x = 1;
- g (ref x);
- // x == 2
- Console.WriteLine("{0}", x);
- }
- }
int& must also be used by caller- void g(int& y) {
- y = y + 1;
- }
- int main() {
- int x = 1;
- g(x + 1);
- cout << "x = " << x << endl;
- return 0;
- }
- $ g++ -o reference reference.cpp
- reference.cpp: In function ‘int main()’:
- referencs.cpp:11:8: error: invalid initialization of non-const
- reference of type ‘int&’ from an rvalue of type ‘int’
- g (x + 1);
- ^
- references.cpp:5:6: error: in passing argument 1 of ‘void g(int&)’
- void g (int& y) {
- ^
& operator- class IntRef { int n; }
- public class Ref {
- static void g(IntRef r) { r.n = r.n + 1; }
- public static void main(String[] args) {
- IntRef s = new IntRef (); s.n = 1;
- g(s);
- // s.n == 2
- System.out.println (s.n);
- }
- }
- def f(x: Double) : Double =
- val x1 = x
- val x2 = x
- x1 - x2
- end f
- println("f= " + f (Math.random())) // prints 0 (difference of same random Double)
- def g(x: () => Double) : Double =
- val x1 = x()
- val x2 = x()
- x1 - x2
- end g
- println ("g= " + g (() => Math.random())) // prints difference of 2 random doubles
- def h(x: => Double) : Double =
- val x1 = x
- val x2 = x
- x1 - x2
- end h
- println ("h= " + h (Math.random())) // prints difference of 2 random doubles
- def myWhile (cond: => Boolean) (body: => Unit) : Unit =
- if (cond)
- body
- myWhile (cond) (body)
- end myWhile
{} or parentheses () are required)- var i = 3
- myWhile (i > 0) {
- println ("i= " + i)
- i = i - 1
- }
Call-by-value
Call-by-reference
Call-by-name
# <span class="fa-stack"><i class="fa-solid fa-circle fa-stack-2x"></i><i class="fa-solid fa-cubes fa-stack-1x fa-inverse"></i></span> Formal Semantics of Function Calls - Example: ```scala def f(x) = 2*x f(3) ``` * With stores $\xi=\{\}$, $\phi=\{\mathtt{f} \mapsto (\mathtt{x,2*x})\}$, $\rho=\{\}$ * Deduction tree <div class="flex justify-center text-sm"> <div data-marpit-fragment class="absolute bg-white z-0"> $$ \begin{prooftree} \AXC{} \UIC{} \AXC{} \AXC{} \UIC{} \AXC{} \UIC{} \BIC{} \TIC{$\langle \mathtt{f(3)},\xi,\phi,\rho \rangle \Downarrow \langle 6,\xi,\phi,\rho\rangle$} \end{prooftree} $$ </div> <div data-marpit-fragment class="absolute bg-white z-1"> $$ \begin{prooftree} \AXC{} \UIC{$\langle \mathtt{3},\xi,\phi,\rho \rangle \Downarrow \langle 3,\xi,\phi,\rho\rangle$} \AXC{$\phi(\mathtt{f})=(\mathtt{x,2*x})$} \AXC{} \UIC{} \AXC{} \UIC{} \BIC{$\langle \mathtt{2*x},\xi,\phi,\{\mathtt{x} \mapsto 3\} \rangle \Downarrow \langle 6,\xi,\phi,\rho \rangle$} \RL{(Fun)} \TIC{$\langle \mathtt{f(3)},\xi,\phi,\rho \rangle \Downarrow \langle 6,\xi,\phi,\rho\rangle$} \end{prooftree} $$ </div> <div data-marpit-fragment class="absolute bg-white z-2"> $$ \begin{prooftree} \AXC{} \RL{(Num)} \UIC{$\langle \mathtt{3},\xi,\phi,\rho \rangle \Downarrow \langle 3,\xi,\phi,\rho\rangle$} \AXC{$\phi(\mathtt{f})=(\mathtt{x,2*x})$} \AXC{} \RL{(Num)} \UIC{$\langle \mathtt{2},\xi,\phi,\{\mathtt{x} \mapsto 3\} \rangle \Downarrow \langle 2,\xi,\phi,\{\mathtt{x} \mapsto 3\} \rangle$} \AXC{} \RL{(Var)} \UIC{$\langle \mathtt{x},\xi,\phi,\{\mathtt{x} \mapsto 3\} \rangle \Downarrow \langle 3,\xi,\phi,\{\mathtt{x} \mapsto 3\} \rangle$} \RL{(Mul)} \BIC{$\langle \mathtt{2*x},\xi,\phi,\{\mathtt{x} \mapsto 3\} \rangle \Downarrow \langle 6,\xi,\phi,\rho \rangle$} \RL{(Fun)} \TIC{$\langle \mathtt{f(3)},\xi,\phi,\rho \rangle \Downarrow \langle 6,\xi,\phi,\rho\rangle$} \end{prooftree} $$ </div> </div> ---