Cs 110 Parameters

Overview of Class Topics

  • Importance of Understanding Class by Reference and Class by Value

    • Essential for writing proper sub functions.

    • Necessary knowledge for working with arrays and other data structures.

    • Fundamental topics that will be used throughout a computer science degree.

Sub Functions

  • Two categories of sub functions:

    • Value Returning: Sub functions that return a value after execution.

    • Void: Sub functions that do not return a value.

  • Passing Information to Sub Functions:

    • Information can be passed through:

      • Parameters: The variables defined in the sub function.

      • Arguments: The actual values passed when calling the function.

  • It's crucial to differentiate between parameters (in the sub function) and arguments (in the calling function).

Passing Information

  • Methods of Passing Variables:

    • Pass by Value:

      • The function receives a copy of the argument's value.

      • Changes made to the parameter within the function do not affect the original argument.

      • Use case: If no change to the original variable is needed.

    • Pass by Reference:

      • The function receives a reference to the original variable's memory address.

      • Changes made to the parameter within the function affect the original argument.

      • Use case: If changes to the original variable are required.

  • Example Analogy:

    • Passing by value relates to sending a copy of one’s house.

    • Passing by reference relates to giving someone the physical house itself, allowing them to make changes directly.

Decision Criteria for Passing Parameters

  • Decide on passing method for each variable individually:

    • Pass by value if you want the sub function to only use the information.

    • Pass by reference if you want the sub function to modify the information.

Practical Examples

  1. Pass by Value Example: Calculating pay.

    • Only needs information (pay rate, hours worked) to compute the pay.

    • Pass these values by value to ensure they remain unchanged after computation.

  2. Pass by Reference Example: Getting age from the user.

    • Requires modifying the original variable.

    • Pass age by reference so changes in the sub function update the original variable.

Implementation in C++

  • In C++, to pass by reference, use an ampersand (&) after the data type in the function prototype.

  • Example function prototype for passing a variable by reference:

    • void getAge(int &age)

  • When calling the function, ensure the variable passed matches the expected type and is not a literal value.

Example Code Structure

  • Main Function:

    • Declare variables.

    • Call sub functions with variables passed appropriately.

  • Sub Functions:

    • Implement changes based on whether they are receiving parameters by value or by reference.

    • Clearly differentiate how each method modifies the variable.

Conclusion and Review

  • Ensure understanding of pass by value and pass by reference.

  • Practice by creating examples, running code, and witnessing firsthand how each method operates.

  • Prepare for upcoming midterms by reviewing these concepts and running sample programs.