Instructor: James Riely
def f (x:String, y:Int) = x * y
f ("hello", 10)
x
, y
- formal parameters (or parameters)
"hello"
, 10
- actual parameters (or arguments)
f
print?
void g (int y) {
y = y + 1;
}
void f () {
int x = 1;
g (x);
print (x);
}
g (e)
e
to a value v
v
to g
v
not visible to caller
g(x+1)
with x = 5
x+1
evaluates to 6
6
is given to g
g(x)
with x = 5
x
evaluates to 5
5
is given to g
x
!
x=1
after call to g
void g (int y) {
y = y + 1;
}
void f () {
int x = 1;
g (x);
print (x);
}
g (e)
e
to an l-value r
r
to g
r
are visible to caller
g(x)
with x = 5
x
evaluates to the location of x
x
) is given to g
g
has an alias of x
void g (int y) {
y = y + 1;
}
void f () {
int x = 1;
g (x);
print (x);
}
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 ();
$ perl ./cbr.pl
x = 1
void g (int *p) {
*p = *p + 1;
}
int main () {
int x = 1;
int *q = &x;
g (q);
printf ("x = %d\n", x);
return 0;
}
$ gcc -o pointer pointer.c
$ ./pointer
x = 2
int&
int*
, creates references (aliases) implicitly
#include <iostream>
using namespace std;
void g (int& y) {
y = y + 1;
}
int main () {
int x = 1;
g (x);
cout << "x = " << x << endl;
return 0;
}
$ g++ -o reference reference.cpp
$ ./reference
x = 2
ref int
int&
must also be used by caller
using System;
class Test {
static void g (ref int y) {
y = y + 1;
}
static void Main () {
int x = 1;
g (ref x);
Console.WriteLine("{0}", x);
}
}
$ mcs cbr9.cs
$ mono cbr9.exe
2
#include <iostream>
using namespace std;
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) {
^
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);
System.out.println (s.n);
}
}
$ javac Ref.java
$ java Ref
2
using System;
class SharingVersusCopyInCopyOut {
static void callByReference (ref int x, ref int y) {
x = x + 1;
y = y + 1;
}
static void Main() {
int a = 1;
callByReference (ref a, ref a);
Console.WriteLine("a = {0}", a);
}
}
$ mcs cbr8.cs
$ mono cbr8.exe
3
def f (x: Double) : Double =
val x1 = x
val x2 = x
x1 - x2
println ("f= " + f (Math.random()))
def g (x: () => Double) : Double =
val x1 = x()
val x2 = x()
x1 - x2
println ("g= " + g (() => Math.random()))
def h (x: =>Double) : Double =
val x1 = x
val x2 = x
x1 - x2
println ("h= " + h (Math.random()))
def myWhile (cond : => Boolean) (body : => Unit) : Unit =
if cond then
body
myWhile (cond) (body)
var i = 3
myWhile (i > 0):
println ("i= " + i)
i = i - 1
Brackets are required here
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int f (int i) { return i - i; }
#define h(i) (i)-(i)
int main () {
srand(time(NULL));
printf ("f=%d\n", f(rand()));
printf ("h=%d\n", h(rand()));
}
$ gcc -o cbn4 cbn4.c
$ ./cbn4
f=0
h=-955465947