• No results found

Evaluation. Next: different ways values are passed into function calls

N/A
N/A
Protected

Academic year: 2021

Share "Evaluation. Next: different ways values are passed into function calls"

Copied!
50
0
0

Loading.... (view fulltext now)

Full text

(1)

Evaluation

Next: different ways values are passed into function calls

You might think that when you see a function definition and call like

int f(int p, int q) { ...p...q... } ...

z = f(x+y, x-y);

(2)

Evaluation

Next: different ways values are passed into function calls You might think that when you see a function definition and call like

int f(int p, int q) { ...p...q... } ...

z = f(x+y, x-y);

(3)

Aside

Before we start, some words that are often mis-used

• variable: a symbol that refers to a memory location, e.g.,x

andcos

• expression: a combination of variables, operators and the like that describe a computation, e.g.,42andx + cos(y)

• value: some instance of a datatype, e.g.,42and"hello world"

• parameters (of a function): the variables in the function definition, e.g.,pandqabove

• arguments (of a function call): the values passed in when calling a function, e.g., in the above the values ofx+yand

(4)

Aside

Before we start, some words that are often mis-used

• variable: a symbol that refers to a memory location, e.g.,x

andcos

• expression: a combination of variables, operators and the like that describe a computation, e.g.,42andx + cos(y)

• value: some instance of a datatype, e.g.,42and"hello world"

• parameters (of a function): the variables in the function definition, e.g.,pandqabove

• arguments (of a function call): the values passed in when calling a function, e.g., in the above the values ofx+yand

(5)

Aside

Before we start, some words that are often mis-used

• variable: a symbol that refers to a memory location, e.g.,x

andcos

• expression: a combination of variables, operators and the like that describe a computation, e.g.,42andx + cos(y)

• value: some instance of a datatype, e.g.,42and"hello world"

• parameters (of a function): the variables in the function definition, e.g.,pandqabove

• arguments (of a function call): the values passed in when calling a function, e.g., in the above the values ofx+yand

(6)

Aside

Before we start, some words that are often mis-used

• variable: a symbol that refers to a memory location, e.g.,x

andcos

• expression: a combination of variables, operators and the like that describe a computation, e.g.,42andx + cos(y)

• value: some instance of a datatype, e.g.,42and"hello world"

• parameters (of a function): the variables in the function definition, e.g.,pandqabove

• arguments (of a function call): the values passed in when calling a function, e.g., in the above the values ofx+yand

(7)

Aside

Before we start, some words that are often mis-used

• variable: a symbol that refers to a memory location, e.g.,x

andcos

• expression: a combination of variables, operators and the like that describe a computation, e.g.,42andx + cos(y)

• value: some instance of a datatype, e.g.,42and"hello world"

• parameters (of a function): the variables in the function definition, e.g.,pandqabove

• arguments (of a function call): the values passed in when calling a function, e.g., in the above the values ofx+yand

(8)

Aside

Before we start, some words that are often mis-used

• variable: a symbol that refers to a memory location, e.g.,x

andcos

• expression: a combination of variables, operators and the like that describe a computation, e.g.,42andx + cos(y)

• value: some instance of a datatype, e.g.,42and"hello world"

• parameters (of a function): the variables in the function definition, e.g.,pandqabove

• arguments (of a function call): the values passed in when calling a function, e.g., in the above the values ofx+yand

(9)

Aside

• declaration: where we indicate a symbol is a variable, often combined with a type declaration, e.g.,var y;orint x;

orint inc(int x);

• definition: the first time we give a variable a value, mostly referring to functions (for non-functions we tend to say initialisation). Often combined with a declaration, e.g.,

(10)

Aside

• declaration: where we indicate a symbol is a variable, often combined with a type declaration, e.g.,var y;orint x;

orint inc(int x);

• definition: the first time we give a variable a value, mostly referring to functions (for non-functions we tend to say initialisation). Often combined with a declaration, e.g.,

(11)

Aside

In particular, be clear on the difference between variables and values, even though we often use lazy language, e.g., “xis 0” rather than the more correct “the variablexhas the value 0”

Also distinguish between function and function call

cosis a (variable that names a) function (some complicated bit of code); whilecos(1.0)is a function call (that will be

evaluated to produce a value)

Hint. A good way to spot bad programmers at an interview is when they confuse these concepts

(12)

Aside

In particular, be clear on the difference between variables and values, even though we often use lazy language, e.g., “xis 0” rather than the more correct “the variablexhas the value 0” Also distinguish between function and function call

cosis a (variable that names a) function (some complicated bit of code); whilecos(1.0)is a function call (that will be

evaluated to produce a value)

Hint. A good way to spot bad programmers at an interview is when they confuse these concepts

(13)

Aside

In particular, be clear on the difference between variables and values, even though we often use lazy language, e.g., “xis 0” rather than the more correct “the variablexhas the value 0” Also distinguish between function and function call

cosis a (variable that names a) function (some complicated bit of code); whilecos(1.0)is a function call (that will be

evaluated to produce a value)

Hint. A good way to spot bad programmers at an interview is when they confuse these concepts

(14)

Aside

In particular, be clear on the difference between variables and values, even though we often use lazy language, e.g., “xis 0” rather than the more correct “the variablexhas the value 0” Also distinguish between function and function call

cosis a (variable that names a) function (some complicated bit of code); whilecos(1.0)is a function call (that will be

evaluated to produce a value)

Hint. A good way to spot bad programmers at an interview is when they confuse these concepts

(15)

Aside

In particular, be clear on the difference between variables and values, even though we often use lazy language, e.g., “xis 0” rather than the more correct “the variablexhas the value 0” Also distinguish between function and function call

cosis a (variable that names a) function (some complicated bit of code); whilecos(1.0)is a function call (that will be

evaluated to produce a value)

Hint. A good way to spot bad programmers at an interview is when they confuse these concepts

(16)

Aside

Exercise. In

int f(int p, int q) { ...p...q... } ...

z = f(x+y, x-y);

identify the variables, expressions, parameters, arguments, declarations, definitions, functions and function calls

(17)

Evaluation

So we have the code:

int f(int p, int q) { ...p...q... } ...

z = f(x+y, x-y);

(18)

Evaluation

Call by Value

In most languages you are familiar with you expect it to:

• evaluate the argumentsx+yand thex-y(in some order. . . )

• pass those values intofas the values of its parametersp

andq

• execute the body offwithpandqhaving those values This is call by value, where the values of the argument expressions are passed to the function call

(19)

Evaluation

Call by Value

In most languages you are familiar with you expect it to:

• evaluate the argumentsx+yand thex-y(in some order. . . )

• pass those values intofas the values of its parametersp

andq

• execute the body offwithpandqhaving those values This is call by value, where the values of the argument expressions are passed to the function call

(20)

Evaluation

Call by Value

In most languages you are familiar with you expect it to:

• evaluate the argumentsx+yand thex-y(in some order. . . )

• pass those values intofas the values of its parametersp

andq

• execute the body offwithpandqhaving those values This is call by value, where the values of the argument expressions are passed to the function call

(21)

Evaluation

Call by Value

In most languages you are familiar with you expect it to:

• evaluate the argumentsx+yand thex-y(in some order. . . )

• pass those values intofas the values of its parametersp

andq

• execute the body offwithpandqhaving those values

This is call by value, where the values of the argument expressions are passed to the function call

(22)

Evaluation

Call by Value

In most languages you are familiar with you expect it to:

• evaluate the argumentsx+yand thex-y(in some order. . . )

• pass those values intofas the values of its parametersp

andq

• execute the body offwithpandqhaving those values This is call by value, where the values of the argument expressions are passed to the function call

(23)

Evaluation

Call by Value

This is so very common that everyone thinks this is how it is always done

And computer hardware is built in the expectation this is how it is done (stacks, etc.)

(24)

Evaluation

Call by Value

This is so very common that everyone thinks this is how it is always done

And computer hardware is built in the expectation this is how it is done (stacks, etc.)

(25)

Evaluation

Call by Value

This is so very common that everyone thinks this is how it is always done

And computer hardware is built in the expectation this is how it is done (stacks, etc.)

(26)

Evaluation

Call by Reference

Some languages can do things very differently: in C++, for example, we can write

void inc(int &n) { n++; } ... int m = 1; inc(m);

and the value ofmis incremented

The argument declaration is read as “intreferencen” or “int

(27)

Evaluation

Call by Reference

Some languages can do things very differently: in C++, for example, we can write

void inc(int &n) { n++; } ... int m = 1; inc(m);

and the value ofmis incremented

The argument declaration is read as “intreferencen” or “int

(28)

Evaluation

Call by Reference

This is a call by reference

It’s not the value ofmthat gets passed into the function, but (a reference to) the variablemitself

Meaning, within the function, operations onnare “really” operations onm

(29)

Evaluation

Call by Reference

This is a call by reference

It’s not the value ofmthat gets passed into the function, but (a reference to) the variablemitself

Meaning, within the function, operations onnare “really” operations onm

(30)

Evaluation

Call by Reference

This is a call by reference

It’s not the value ofmthat gets passed into the function, but (a reference to) the variablemitself

Meaning, within the function, operations onnare “really” operations onm

(31)

Evaluation

Call by Reference

This is a call by reference

It’s not the value ofmthat gets passed into the function, but (a reference to) the variablemitself

Meaning, within the function, operations onnare “really” operations onm

(32)

Evaluation

Call by Reference

So the body ofincis effectively evaluated as

{ m++; }

Though, in practice, it is implemented in a somewhat different way

(33)

Evaluation

Call by Reference

C++ allows both call by value and call by reference, with by value the default

Call by reference allows simple looking code like the above that manipulates variables out of the scope of the function body Used wisely, it makes for simpler code, potentially more efficient when than call by value, when those values are large structures that are slow to copy around

Used unwisely, it is a source of subtle bugs

Note it is generally regarded as bad practice for code to affect non-local state, e.g., non-local variables, and CBR makes this easy to do by accident

(34)

Evaluation

Call by Reference

C++ allows both call by value and call by reference, with by value the default

Call by reference allows simple looking code like the above that manipulates variables out of the scope of the function body

Used wisely, it makes for simpler code, potentially more efficient when than call by value, when those values are large structures that are slow to copy around

Used unwisely, it is a source of subtle bugs

Note it is generally regarded as bad practice for code to affect non-local state, e.g., non-local variables, and CBR makes this easy to do by accident

(35)

Evaluation

Call by Reference

C++ allows both call by value and call by reference, with by value the default

Call by reference allows simple looking code like the above that manipulates variables out of the scope of the function body Used wisely, it makes for simpler code, potentially more efficient when than call by value, when those values are large structures that are slow to copy around

Used unwisely, it is a source of subtle bugs

Note it is generally regarded as bad practice for code to affect non-local state, e.g., non-local variables, and CBR makes this easy to do by accident

(36)

Evaluation

Call by Reference

C++ allows both call by value and call by reference, with by value the default

Call by reference allows simple looking code like the above that manipulates variables out of the scope of the function body Used wisely, it makes for simpler code, potentially more efficient when than call by value, when those values are large structures that are slow to copy around

Used unwisely, it is a source of subtle bugs

Note it is generally regarded as bad practice for code to affect non-local state, e.g., non-local variables, and CBR makes this easy to do by accident

(37)

Evaluation

Call by Reference

C++ allows both call by value and call by reference, with by value the default

Call by reference allows simple looking code like the above that manipulates variables out of the scope of the function body Used wisely, it makes for simpler code, potentially more efficient when than call by value, when those values are large structures that are slow to copy around

Used unwisely, it is a source of subtle bugs

Note it is generally regarded as bad practice for code to affect non-local state, e.g., non-local variables, and CBR makes this easy to do by accident

(38)

Evaluation

Call by Reference

In the example above calling

inc(a[3]);

is fine asa[3]refers to a memory location; nownin the function is simply a reference toa[3]

But

inc(2*m);

is a code bug, and will not compile! Here,2*mis an expression, not a reference to a memory location, which is whatinc

(39)

Evaluation

Call by Reference

In the example above calling

inc(a[3]);

is fine asa[3]refers to a memory location; nownin the function is simply a reference toa[3]

But

inc(2*m);

is a code bug, and will not compile! Here,2*mis an expression, not a reference to a memory location, which is whatinc

(40)

Evaluation

Call by Reference

Some terminology: expressions likexanda[n+1]that refer to memory locations are called lvalues, as you can put them on the left side of an assignment: a[n+1] = 42;

An lvalue is associated with a memory address Lvalues are occasionally called locator values

A non-lvalue is an rvalue, as you only find them on the right of an assignment

(41)

Evaluation

Call by Reference

Some terminology: expressions likexanda[n+1]that refer to memory locations are called lvalues, as you can put them on the left side of an assignment: a[n+1] = 42;

An lvalue is associated with a memory address

Lvalues are occasionally called locator values

A non-lvalue is an rvalue, as you only find them on the right of an assignment

(42)

Evaluation

Call by Reference

Some terminology: expressions likexanda[n+1]that refer to memory locations are called lvalues, as you can put them on the left side of an assignment: a[n+1] = 42;

An lvalue is associated with a memory address Lvalues are occasionally called locator values

A non-lvalue is an rvalue, as you only find them on the right of an assignment

(43)

Evaluation

Call by Reference

Some terminology: expressions likexanda[n+1]that refer to memory locations are called lvalues, as you can put them on the left side of an assignment: a[n+1] = 42;

An lvalue is associated with a memory address Lvalues are occasionally called locator values

A non-lvalue is an rvalue, as you only find them on the right of an assignment

(44)

Evaluation

Call by Reference

An rvalue does not have an associated memory address

So things like42 = n;and(2*m)++;do not make sense Call by reference works on lvalues only

(45)

Evaluation

Call by Reference

An rvalue does not have an associated memory address So things like42 = n;and(2*m)++;do not make sense

(46)

Evaluation

Call by Reference

An rvalue does not have an associated memory address So things like42 = n;and(2*m)++;do not make sense Call by reference works on lvalues only

(47)

Evaluation

Behind the Scenes

In reality, call by reference is implemented by use of pointers, thus if you wrote code like

void f(int& a) { a = a + 3; }

and called it withf(n)this is transformed behind the scenes by the compiler to the equivalent of

void f(int *a) { *a = *a + 3; }

(48)

Evaluation

Behind the Scenes

The advantage is that you write the simpler code without the proliferation of*s and&s, and the compiler does the pointer chasing for you

Note that the call by referencefis not assigning pointers, but the values behind those pointers

(49)

Evaluation

Behind the Scenes

The advantage is that you write the simpler code without the proliferation of*s and&s, and the compiler does the pointer chasing for you

Note that the call by referencefis not assigning pointers, but the values behind those pointers

(50)

Evaluation

Exercise. Fortran is call by reference, but does allow things like

inc(2*m). Find out what is happening here

Advanced Exercise. Read about how lvalues are implicitly coerced/dereferenced/converted to rvalues on the right of an assignment

Advanced Exercise. Read about Algol 68

References

Related documents

Further, by showing that v τ is a modular unit over Z we give a new proof of the fact that the singular values of v τ are units at all imaginary quadratic arguments and obtain

During this research we were very struck by people’s eagerness in all seven countries to talk about experiences and discontents in relation to combining paid work with other parts

• building bridges between civil society and public organisations as well as acting as a lobbying body for the Muslim charity sector; • and promoting humanitarian principles

I like The Lord of the Rings because it’s really exciting?. It’s longer than most films, but the actors

premonitory :: serving to warn prognathous :: having projecting jaws prophylactic :: used to prevent disease provincial :: pertaining to a province; limited. psychopathic

of wheat production in Iran: A case study from Ardabil province. Eco-efficiency analysis by BASF: The method. Life Cycle Assess. Energy flow and ecological sustainability in

A consequence of this perspective of woman directors being appointed as part of the symbolic management of the independence of the board is that, when these directors lose

U odnosu na tip dijalize prije transplantacije nije bilo statistički značajne razlike u preživljenju bolesnika (p=.09), preživljenju bubrega (p=.19) te u preživljenju