Docsity

Parameter Passing Methods: Understanding Formal vs Actual Parameters and Semantics, Lecture notes of Programming Paradigms

Toggle sidebar
bg1
Parameter Passing Methods
Iqbal Mohomed
Formal vs Actual Parameters
void write(int f)
{
// Blah
}
void main()
{
int a = 3;
write (a);
}
• We call a the actual paramater
• We call f the formal parameter
Semantic Models of Parameter
Passing
• in -mode: FP can receive data from AP
• out-mode: FP can transmit data to AP
• inout-mode: Do both
Conceptual Model of Data Transfer
• Transmission of actual value (copy)
• Transmission of access path (alias)
pf3
pf4

Partial preview of the text

Download Parameter Passing Methods: Understanding Formal vs Actual Parameters and Semantics and more Lecture notes Programming Paradigms in PDF only on Docsity!

Parameter Passing Methods

Iqbal Mohomed

Formal vs Actual Parameters

void

write(int

f)

{

//

Blah

} void

main()

{

int

a^

=^

3;

write

(a);

} •^

We call a the actual paramater

-^

We call f the formal parameter

Semantic Models of Parameter

Passing

  • in -mode: FP can receive data from AP• out-mode: FP can transmit data to AP• inout-mode: Do both

Conceptual Model of Data Transfer• Transmission of actual value (copy)• Transmission of access path (alias)

Pass-by-Value

  • Implements in-mode semantics• Formal parameter is a new local variable

that exists within the scope of theprocedure/function/subprogram

  • Value of actual parameter is used to

initialize the formal parameter

  • Changes made to formal parameter DO

NOT get transmitted back to the callerthrough parameter passing!

Pass-by-Value (contd)

  • Typically implemented by copy
    • Inefficiency in storage allocation– inefficiency in copying value– For objects and arrays, the copy semantics

are costly!

  • If access-path were transmitted ...
    • Need to write-protect cell, which is tricky

Pass-by-Result

  • Implements out-mode semantics• Formal parameter is a new local variable

that exists within the scope of the function

  • No value is transmitted to the function• Just before control is transferred back to

the caller, the value of the formal paramteris transmitted back to the actual paramater

Pass-by-Result (contd)

-^

Actual parameter MUST be a variable

-^

Parameter collisons can occur!– write(p1,p1); .. if the two formal parameters in write had different

names, which value should go into p1?

  • Order in which actual parameters are copied determines their

value!

-^

Typically implemented by copy– Inefficiency in storage allocation– inefficiency in copying value– For objects and arrays, the copy semantics are costly!– The added problem is that we don’t want to use the value of the

actual parameter as the initial value of the formal parameter

Pass-by-Reference (contd)

  • Efficient in both time and space!• Disadvantage is aliasing
    • Many potential scenarios can occur– Harmful to program readability and thus to

reliability

Pass-by-Name

  • Implements inout-mode semantics• The actual parameter is textually

substituted for the corresponding formalparameter in all its occurances within thefunction

  • Based on the concept of late-binding
    • Lazy evaluation is also based on the idea of

late-binding ... short-circuit evaluation is anexample of lazy evaluation

Parameter Passing in Major

Languages

-^

C and Java both use pass-by-value semantics.

-^

People keep saying objects are treated as pass-by-reference in Java. Technically ... that isWRONG!

-^

C++ uses pass-by-value as the defaultconvention but allows pass-by-referenceparameters as well– the & operator is used to indicate pass-by-reference– e.g. void write(int& p){ ///}