What is the variable name that is used by a function to receive passed value?

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

  • In this version of the example, the Twice function looks like this:
      void Twice(int& a, int& b)
      {
        a *= 2;
        b *= 2;
      }
    
    • Note that when it is run, the variables passed into Twice from the main() function DO get changed by the function
    • The parameters a and b are still local to the function, but they are reference variables (i.e. nicknames to the original variables passed in (x and y))

  • When reference variables are used as formal parameters, this is known as Pass By Reference
      void Func2(int& x, double& y)
      {
        x = 12;		// these WILL affect the original arguments
        y = 20.5;
      }
    
  • When a function expects strict reference types in the parameter list, an L-value (i.e. a variable, or storage location) must be passed in
      int num;
      double avg;
      Func2(num, avg);		// legal
      Func2(4, 10.6);		// NOT legal
      Func2(num + 6, avg - 10.6);	// NOT legal
    
  • Note: This also works the same for return types. A return by value means a copy will be made. A reference return type sends back a reference to the original.
      int Task1(int x, double y);	// uses return by value
      int& Task2(int x, double y);  // uses return by reference
    
    This is a trickier situation than reference parameters (which we will not see in detail right now).
  • A parameter is a named variable passed into a function. Parameter variables are used to import arguments into functions.

    For example:

    function example(parameter) {
      console.log(parameter); // Output = foo
    }
    
    const argument = "foo";
    
    example(argument);
    

    Note the difference between parameters and arguments:

    • Function parameters are the names listed in the function's definition.
    • Function arguments are the real values passed to the function.
    • Parameters are initialized to the values of the arguments supplied.

    Two kinds of parameters:

    input parameters

    the most common kind; they pass values into functions. Depending on the programming language, input parameters can be passed in several ways (e.g., call-by-value, call-by-address, call-by-reference).

    The sample function writeout.cpp asks the user for a number and where they want it written. Depending on their answer, main will pass to the writout function either cout, cerr, or an open file stream, and the function will write to the appropriate location without knowing the difference.

    Dave Braunschweig

    Overview

    A parameter is a special kind of variable used in a function to refer to one of the pieces of data provided as input to the function. These pieces of data are the values of the arguments with which the function is going to be called/invoked. An ordered list of parameters is usually included in the definition of a function, so that, each time the function is called, its arguments for that call are evaluated, and the resulting values can be assigned to the corresponding parameters.

    Discussion

    Recall that the modular programming approach separates the functionality of a program into independent modules. To separate the functionality of one function from another, each function is given its own unique input variables, called parameters. The parameter values, called arguments, are passed to the function when the function is called. Consider the following function pseudocode:

    Function CalculateCelsius (Real fahrenheit)
        Declare Real celsius
        
        Assign celsius = (fahrenheit - 32) * 5 / 9
    Return Real celsius
    

    If the CalculateCelsius function is called passing in the value 100, as in CalculateCelsius(100), the parameter is fahrenheit and the argument is 100. The terms parameter and argument are often used interchangeably. However, parameter refers to the variable identifier (fahrenheit) while argument refers to the variable value (100).

    Functions may have no parameters or multiple parameters. Consider the following function pseudocode:

    Function DisplayResult (Real fahrenheit, Real celsius)
        Output fahrenheit & "° Fahrenheit is " & celsius & "° Celsius"
    End
    

    If the DisplayResult function is called passing in the values 98.6 and 37.0, as in DisplayResults(98.6, 37.0), the argument or value for the fahrenheit parameter is 98.6 and the argument or value for the celsius parameter is 37.0. Note that the arguments are passed positionally. Calling DisplayResults(37.0, 98.6)would result in incorrect output, as the value of fahrenheit would be 37.0 and the value of celsius would be 98.6.

    Some programming languages, such as Python, support named parameters. When calling functions using named parameters, parameter names and values are used, and positions are ignored. When names are not used, arguments are identified by position. For example, any of the following function calls would be valid:

    When a variable is passed by value?

    Pass By Value: In Pass by value, function is called by directly passing the value of the variable as an argument. So any changes made inside the function does not affect the original value. In Pass by value, parameters passed as an arguments create its own copy.

    What type of data can be passed to a function by value?

    Explanation: pass by reference. It doesn't matter if the parameters are primitive types, arrays, or objects, either a copy is made or an address is stored. As noted elsewhere, when objects are copied, the copy constructor is called to do the copying.