Lect 2.3 Parameter passing - Lecture2.3...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture 2.3 Parameter passing in C++, Java and C#
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Parameter passing Formal parameter versus actual parameter Formal parameter is the parameter name in the function definition. E.g. void doSomething(int x) { …. } x is the formal parameter. 2
Background image of page 2
Parameter passing Actual parameter is the “actual” parameter passed into the function. E.g. int main() { int a = 5; doSomething(a); } a is the actual parameter. Actual parameters can be constants. 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Parameter passing There are a few different ways of passing parameters to methods. Here, we would discuss two most commonly used ways Pass by value Pass by reference 4
Background image of page 4
Pass by value In pass by value, a copy of the actual parameter is made and assigned to the formal parameter. This means that the original actual parameter cannot be modified within the function body. Pass by value is not encouraged with variables that occupy large amounts of memory. C++, Java and C# support pass by value. 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
In pass by reference, you can think of the actual parameter itself as being assigned to the formal parameter. This means that the original actual parameter can be modified within the function body. The implementation of pass by reference is up to the compiler, and in many cases, it is the memory location of the variable that is passed. C++ and C# support pass by reference. Java does not support pass
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 20

Lect 2.3 Parameter passing - Lecture2.3...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online