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

CS 461
Subroutines and Control Abstraction Parameter Passing Modes 2
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 (C++)
§ Call by result (Call by return, out parameter in Ada)
§ Call by value/result 9Call by value/return, in out parameter in Ada)
§ Call by sharing (essentially call by value, but the value for objects are references to objects in memory)
Carnegie Mellon
2

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
3
3

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
4
4

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
5
5

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
6
6

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
7
7

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

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
9
9

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
10
10

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
11
11

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
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;

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

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
14
14

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
15
15

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 parameters:
§ non-optional: value or const reference
§ optional: value or const pointer § output parameters:
§ non-optional: reference
§ optional: non-const pointers
Carnegie Mellon
16

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
17

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
18

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
19

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;
20

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;
21

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
22
22

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;
Tophat question after y:=foo(x) What is the value of x?
What is the value of y?
Carnegie Mellon
23

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
24

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
25

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
26
26

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
27

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
28

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
29

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
30

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
31

Call-By-Value/Call-By-Sharing in Python
¢ Python is similar to Java § Call by value
§ But all values are references(pointers) to objects § Primitive Objects/Immutable objects:
§ Not much difference between call-by-value vs. call-by-sharing.
§ Can not modify the objects even if you have its reference § Mutable Objects:
§ modify the object through the reference (affects caller)
§ vs.
§ modify the reference to refer to a different object (doesn’t affect the caller because reference is a value copied to the AR)
Carnegie Mellon
32

Python Example ¢ Example:
§ lst.sort() (list method that modifies the list through the refrence lst)
§ vs.
§ lst = sorted(lst) (build-in function that creates a new sorted list)
¢ Within a function
§ Operations that modify an object through the reference can impact
the caller
§ lst.sort()
§ lst.append(1)
§ Operations make the formal parameters refer to a newly created object doesn’t have effect on the caller
§ lst = sorted(lst) § lst = lst + [1]
§ …
33
Carnegie Mellon

Python Example — sorting list1.py list2.py
Carnegie Mellon
t = [1, 3, 2]
def sortlist(lst):
lst.sort()
return lst
t1 = sortlist(t)
t = [1, 3, 2]
def sortlist(lst):
lst = sorted(lst)
return lst
t1 = sortlist(t)
34

Python Example — appending list3.py list4.py
Carnegie Mellon
t = []
def appendlist(lst, v):
lst.append(v)
return lst
t1 = appendlist(t, 1)
t = []
def appendlist(lst, v):
lst = lst + [v]
return lst
t1 = appendlist(t, 1)
35

C and Arrays
¢ In C, arrays are passed to functions as the address to its 0th element.
§ Modify the content of the array through the pointer will affect the caller.
§ Modify the pointer to point to something new will not affect the caller.
Carnegie Mellon
36

C Array Example:
#include
void swap(int arr[]) {
int temp = arr[0];
arr[0] = arr[1];
arr[1] = temp;
}
void swap1(int arr[]) {
int *temp = malloc(sizeof(int) * 2);
temp[0] = arr[1];
temp[1] = arr[0];
arr = temp;
}
int main() {
int nums[2] = {1, 2};
swap(nums);
swap1(nums);
return 0;
}
Carnegie Mellon
37