CS计算机代考程序代写 c++ Java Lambda Calculus CS 461

CS 461
Subroutines and Control Abstraction Parameter Passing Modes
Yanling Wang
Computer Science and Engineering Penn State University
Carnegie Mellon
1

This lecture – Parameter Passing
¢ Parameters vs. Arguments
§ Most subroutines are parameterized
§ Parameter names in function declaration – Formal Parameters
§ Variables and expressions that are passed to a subroutine in a call – Actual Parameters/ Arguments
¢ Parameter-Passing modes § Call by values
§ Call by references § Call by result
§ Call by value/result
Carnegie Mellon
2

Where we are in the Big Picture ¢ Understanding A Programming Language
§ Syntax (chapter 1, chapter 2) – What is the right form of a program?
§ Semantics (chapter 3, chapter 9, chapter 10, chapter 4, 6, 7, 8) – What is the meaning of a program?
§ chapter 3 – naming/scope/binding
§ chapter 9 – functions (control abstraction) and how it works
§ chapter 10 – object-oriented programming (data abstraction) and how it works
§ chapter 7/8 – type checking of a program
§ chapter 4/6 – other aspects of a program’s meaning § Language Paradigm
§ chapter 11 – Lambda Calculus and functional programming languages
Carnegie Mellon
3

Parameter Passing
How caller communicates with callee
Formal parameters: names in the declaration of a function
Actual parameters (Arguments): variables/expressions passed to a function
formal
Carnegie Mellon
foo (int x) {…}
foo (3+5);
actual
4
4

Parameter Modes
Call-By-Value (C, Ada: in parameter) Call-By-Reference (C++ with &) Call-By-Result (Ada: out parameter) Call-By-Value/Reult (Ada: in out parameter)
Carnegie Mellon
5
5

Call-By-Value
Calling mechanism
¢ Arguments are evaluated to their values
¢ Memory or registers allocated for arguments on AR
¢ Argument values copied to AR
¢ AR destroyed when callee returns
Carnegie Mellon
6
6

Call-By-Value
int x=1;
int foo (int a) {
x = 2;
a = 5;
return x+a;
}
int main() {
int y = foo(x); return 0;

foo
stack
main
Carnegie Mellon
para a=1

x=1
}
static area
7
7

Call-By-Value
int x=1;
int foo (int a) {
x = 2;
a = 5;
return x+a;
}
int main() {
int y = foo(x); return 0;

foo
stack
main
Carnegie Mellon
para a=5

x=2
}
static area
8
8

Call-By-Value
int x=1;
int foo (int a) {
x = 2;
a = 5;
return x+a;
}
int main() {
int y = foo(x); return 0;

stack
main
Carnegie Mellon
x=2
}
static area
9
9

Call-By-Value
Formal & Actual parameters have separate memory: their values may diverge
Characteristics
¢ Actual parameters may not directly be changed in callee (unless pass in pointers)
¢ Arguments can be complex expressions ¢ Simple and intuitive (less error-prone)
Carnegie Mellon
10
10

Call-By-Value: Performance
Primitive types: cost per parameter is small
Arrays, records, structures: copying value could be slow
Carnegie Mellon
11
11

Call-By-Reference
Calling mechanism
¢ Arguments are evaluated to their values
¢ Memory or registers allocated for arguments on AR
¢ Argument address stored in AR
¢ AR destroyed when callee returns
Carnegie Mellon
12
12

Call-By-Reference
int x=1;
int foo (int &a) {
x = 2;
a = 5;
return x+a;
}
int main() {
int y = foo(x);
return 0;

foo
stack
main
Carnegie Mellon
para a

x=1
}
static area
13
13

Call-By-Reference
int x=1;
int foo (int &a) {
x = 2;
a = 5;
return x+a; }
int main() {
int y = foo(x);
return 0;

foo
stack
main
Carnegie Mellon
para a

x=2
}
static area
14
14

Call-By-Reference
int x=1;
int foo (int &a) {
x = 2;
a = 5;
return x+a; }
int main() {
int y = foo(x);
return 0;

foo
stack
main
Carnegie Mellon
para a

x=5
}
static area
15
15

Call-By-Reference
int x=1;
int foo (int &a) {
x = 2;
a = 5;
return x+a;
}
int main() {
int y = foo(x);
return 0;

stack
main
Carnegie Mellon
x=5
}
static area
16
16

Call-By-Reference
Formal parameter is an alias of actual parameter: their values are the same
Characteristics
¢ Actual parameters may directly be changed in callee
¢ Some language disallows complex expressions as arguments (in C++, only l-values expression could be used)
¢ Programs are harder to understand (more error- prone)
§ in C++ combine with const to make the reference not modifiable to avoid errors.
Carnegie Mellon
17
17

Call-By-Reference: Performance Avoids the cost of memory copy
Indirect memory access: addressàvalue C: call-by-value is the default mode
C++: you can add & to formal parameter to indicate call-by-reference
Carnegie Mellon
18
18

Google C++ Style Guide
¢ https://google.github.io/styleguide/cppguide.html#Input s_and_Outputs
§ output: through return value (preferred) and/or output parameter § input paramters:
§ non-optional: value or const reference
§ optional: value or const pointer § output parameters:
§ non-optional: reference
§ optional: non-const pointers
Carnegie Mellon
19

Ada’s parameter modes
¢ in parameter (read only) – Call-By-Value
¢ out (not initialized but can be written to and then be read) – Call-By-Result
¢ in out parameter (can be read and written) – Call-By- Value/Result
Carnegie Mellon
20

Ada’s in parameter
procedure g1 is
function set(base:Integer) return Integer is begin
return base + 1;
end set;
result:Integer;
begin
result := set(10);
end g1;
Can’t modify base in function set.
Carnegie Mellon
21

Ada’s out parameter
procedure g2 is
function set(base:Integer; result:out Integer) return Integer is
begin
result := 100;
return result + base;
end set;
x,y:Integer;
begin
x := 10;
y := set(x, x);
end g2;
Carnegie Mellon
22

Ada’s in out parameter
procedure g3 is
procedure swap(a, b:in out Integer) is
temp:Integer;
begin
temp := a;
a := b;
b := temp;
end swap;
x,y:Integer;
begin
x := 10;
y := 100;
swap(x, y);
Carnegie Mellon
Behaves in similar way as Call-By-Reference
end g3;
23

Ada’s in out parameter
procedure g3 is
procedure swap(a, b:in out Integer) is
temp:Integer;
begin
temp := a;
a := b;
b := temp;
end swap;
x,y:Integer;
begin
x := 10;
y := 100;dk
swap(x, x);
Carnegie Mellon
Behaves in similar way as Call-By-Reference in absence of aliasing.
Disallow obvious aliasing.
end g3;
24

Call-By-Value-Return (In-Out)
Calling mechanism
¢ Arguments are evaluated to their values
¢ Memory or registers allocated for arguments on AR
¢ Argument values stored in AR
¢ Before callee returns, AR values copied back to
actual arguments
¢ AR destroyed when callee returns
Carnegie Mellon
25
25

Call-By-Value-Return (with aliasing)
procedure g4 is
x:Integer := 1;
function foo(a:in out Integer) return
Integer is
begin
x := 2;
a := 5;
return x+a;
end foo;
y: Integer;
begin
y := foo(x);
end g4;
Carnegie Mellon
26

Call-By-Value-Return (with aliasing)
with Ada.Integer_Text_IO;
procedure f2 is
function set(I1, I2:in out Integer) return Integer is
begin
I1 := I1 + 1;
I2 := I2 + 2;
return I1 + I2;
end set;
nums: Array (0..1) of Integer := (0, 10);
i1, i2:Integer;
y:Integer;
begin
Ada.Integer_Text_IO.Get(i1);
Ada.Integer_Text_IO.Get(i2);
y := set(nums(i1), nums(i2));
end f2;
Carnegie Mellon
27

Call-By-Reference (with aliasing in C++)
#include
int set(int &I1, int &I2) {
I1 = I1 + 1;
I2 = I2 + 2;
return I1 + I2;
}
int main() {
int nums[2] = {0, 10};
int i1;
int i2;
std::cin >> i1;
std::cin >> i2;
int y = set(nums[i1], nums[i2]);
return 0;
}
Carnegie Mellon
28

Call-By-Value-Return Characteristics
¢ Mostly identical to call-by-value, except an extra step of copying values back to actual parameter
¢ In absence of aliasing, behaves similarly to call-by- reference.
¢ With aliasing, different from call-by-reference.
§ Inside the function, each argument has its own storage. The copy-out only happens at the end.
Carnegie Mellon
29
29

C: Use pointer to simulate call-by- reference
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
return; }
int main() {
int x = 3;
int y = 4;
swap(&x, &y);
return 0;
}
Carnegie Mellon
30

Java: Call-By-Value / Call-By-Sharing
¢ Java uses call-by-value
§ Primitive values are always call-by-value
§ Primitive.java
§ Objects are call-by-value but the value is essentially a pointer
(reference) to the objects.
§ You can’t change the pointer to point to a different object location. (WithObject2.java)
§ You can use this pointer to change the content of the object it is pointing to. (WithObject.java)
Carnegie Mellon
31

Primitive.java – Call-By-Value
public class Primitive {
public static void main(String[] args) {
int x = 3;
int y = 4;
swap(x, y);
}
public static void swap(int a, int b) {
} }
int temp = a;
a = b;
b = temp;
Doesn’t swap!
Carnegie Mellon
32

WithObject.java – Call-By-Sharing (Value is a pointer/reference)
public class WithObject {
public static void main(String[] args) {
Wrapper x = new Wrapper(3);
Wrapper y = new Wrapper(4);
swap(x, y);
}
public static void swap(Wrapper a, Wrapper b) {
} }
class Wrapper {
public int value;
}
int temp = a.value;
a.value = b.value;
b.value = temp;
Does swap!
public Wrapper(int value){ this.value = value;}
Carnegie Mellon
33

WithObject2.java – Call-By-Sharing (Value is a pointer/reference)
public class WithObject2 {
public static void main(String[] args) {
Wrapper x = new Wrapper(3);
Wrapper y = new Wrapper(4);
swap(x, y);
}
public static void swap(Wrapper a, Wrapper b) {
} }
class Wrapper {
public int value;
}
Wrapper temp = a;
a = b;
b = temp;
Doesn’t swap!
public Wrapper(int value){ this.value = value;}
Carnegie Mellon
34