- Forward


Parameters in C++
An Introduction for Java Programmers


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Terminology
Back SMYC Forward
  • Formal Parameters:
    • The parameters in the specification/declaration of the function
  • Actual Parameters:
    • The parameters in the execution/invocation of the function
Basics
Back SMYC Forward
  • Call by Value:
    • A copy of the variable is passed into the function.
    • The value of the copy can be changed but the changes are not "passed back to" the caller
  • Call by Reference:
    • A reference is passed to the function
    • Any changes made are made to the variable being referred to (and, hence, are effectively "passed back to" the caller)
Syntax
Back SMYC Forward
  • Specification/Declarations:
    • type name([type param]...) // Specification for "Call by Value"
    • type name([type &param]...) // Specification for "Call by Reference"
  • Executing/Invoking/Calling a Function:
    • name([param]...) // void return type
    • result = name([param]...) // non-void return type
Constant Arguments
Back SMYC Forward
  • Constant call by value:
    • A copy of the variable is passed into the function and the value of the copy can not be changed
    • Syntax: name(const type param)
  • Constant call by reference:
    • A reference is passed to the function and the value of the variable being "referred to" cannot be changed
    • This is preferred to using call by value when passing classes (and structs) because it is faster (i.e., the system does not have to make a copy of the object)
    • Syntax: name(const type &param)
Passing Pointers
Back SMYC Forward
  • An Observation:
    • It is possible to pass a pointer to an object by value
  • Implications:
    • A copy of the pointer is made and passed to the function
    • The function has access to the members of the object being pointed to but cannot change the pointer itself
Another Use of the const Qualifier
Back SMYC Forward
  • Syntax:
    • classname::function( ... ) const
  • Purpose:
    • Prevents the method from making changes to the attributes of the object
  • An Example:
    • void Account::printout() const { cout << "Actual Balance: " << balance; balance = 0.00; // Generates a compile-time error }
Copy Constructors Reconsidered
Back SMYC Forward
  • Recall:
    • A default implementation is provided by the compiler (but it will only work properly for simple classes)
  • An Observation:
    • A working copy constructor is necessary for an object to be passed by value
  • An Implication:
    • You should always include an implementation of a working copy constructor
The Return
Back SMYC Forward
  • Return by Value:
    • A copy of the value is made and returned
  • Return by Reference:
    • A reference is returned (which can lead to serious problems if a local variable is returned since the local variable goes out of scope immediately after the return and the reference may, as a result, not refer to anything)
There's Always More to Learn
Back -