Clever Geek Handbook
📜 ⬆️ ⬇️

Parameter (programming)

A parameter in programming is an argument accepted by a function. The term "argument" implies what exactly and what specific function was transferred, and the parameter - in what quality the function applied this accepted. That is, the calling code passes the argument to the parameter that is defined in the function specification member.

Content

  • 1 Formal and actual parameters
  • 2 Using options
  • 3 Examples
  • 4 See also
  • 5 Literature
  • 6 References

Formal and Actual Parameters

It is important to distinguish between:

  • formal parameter - an argument specified when declaring or defining a function . [1] [2]
  • the actual parameter is the argument passed to the function when it is called;

C example:

  // Description of the function.  int a - formal parameter; parameter name may be absent.
 int myfunction ( int a );

 // Definition of the function.  int b is a formal parameter, the parameter name may not coincide with that specified when declaring the function.
 int myfunction ( int b ) 
 {
    return 0 ;
 }

 int main ()
 {
     int c = 0 ;
     myfunction ( c );  // Call the function.  c is the actual parameter.
     return 0 ;
 }

Using Options

The semantics of the use of formal and actual parameters is called a calculation strategy . The given calculation strategy dictates when the arguments of the function (method, operation, relation) should be calculated, and what values ​​should be transmitted. There are quite a few diverse calculation strategies.

Note - the use of the term “ parameter transfer ” common in the community of imperative programming for many programming languages ​​is not quite correct - for example, if called if necessary , used in the Haskell language , the parameter can be used in the body of the function, but it has never been passed for everything cases of its call, and even completely excluded from the resulting machine code.

The most frequently mentioned calculation strategies are call by value and call by reference , however, in reality, the use of these terms is not always appropriate. For example, in the Java community they say “ Java uses a call by value, where“ value ”is a reference to an object ”, in the Ruby community they say “ Ruby uses a call by reference ”, however, in reality both of these languages ​​use a call-sharing strategy . call-by-sharing ) [3] [4] . Many languages, such as C , do not have a call-by-reference mechanism, but allow it to be simulated within the call-by-value semantics using reference types , in particular pointers . In the latter case, communities of such languages ​​often say “the language supports two calculation strategies ”, as well as “ call by pointer ” or “ call by address ”.

In practice, the computational model of many industrial languages ​​( Java , C # ) comes down to a call-at-mention / pass-by-reference strategy. Some older languages, especially unsafe languages, such as C ++ , combine several different call models, including exotic ones, such as call-by-reference-to- constant . Historically, a call by value and a call by name date back to Algol-60 , created in the late 1950s . Only pure functional languages, such as Clean and Haskell , use call-by-need , which is often identified (which is also not entirely correct) with lazy calculations .

Examples

Passing a parameter by reference means that it is not the value itself that is copied , but the address of the source variable (as in the case of passing the parameter to the address), however, the syntax is used so that the programmer does not have to use the dereference operation and he could deal directly with the value stored at this address (as in the case of passing a parameter by value).

Passing by reference allows you to avoid copying all the information that describes the state of the object (and this can be significantly larger than sizeof (int)) and is necessary for the copy constructor .

If a function returns a value by reference (for example, in the form of "return * this;"), then its call can be used to the left of the assignment operator (see also L-expression ).

If passing by reference is used precisely as a means of increasing performance, but changing a parameter is undesirable, you can use passing by reference a constant object.

C ++ example:
  #include <iostream> 
 using namespace std ;  // to use cout

 void f ( int x )
 {
     // pass parameter by value
     cout << x ;
     x is 1 ;
     cout << x ;
 }

 void g ( int * x )
 { 
     // pass parameter to address
     cout << * x ;
     * x = 2 ;
     cout << * x ;
 }

 void h ( int & x )
 { 
     // pass parameter by reference
     cout << x ;
     x is 3 ;
     cout << x ;
 }

 void i ( const int & x )
 { 
     // pass an immutable parameter by reference
     cout << x ;
     x = 4 ;  // Error due to which the code will not be compiled
     cout << x ;
 }

 int main ()
 {
     int x = 0 ;    
     cout << x ;
     f ( x );
     cout << x << "" ;
     g ( & x );
     cout << x << "" ;
     h ( x );
     cout << x << "" ;
     i ( x );
     cout << x ;
     return 0 ;
 }

Thus, we can expect that the sample program will print (if you comment out the error line) "0010 022 233 333".

Some languages ​​(or their dialects) do not support passing by reference, some vice versa - passing parameters exclusively by reference, which creates the risk of inadvertently changing the context of the calling function.

Fortran language implies passing parameters exclusively by reference:

Fortran example:

Program:

  PROGRAM PARAMS
       IMPLICIT NONE
        INTEGER A , B , C
      
       A = 7.0
       B = 9.0
       C = 0.0
      
 100 FORMAT ( 'A =' , I2 , ', B =' , I2 , ', C =' , I3 )
      
       WRITE ( * , 100 ) A , B , C
       CALL MUL ( A , B , C )
       WRITE ( * , 100 ) A , B , C
      
       End program

 SUBROUTINE MUL ( A , B , C )
       INTEGER A , B , C
       C = A * B
       END SUBROUTINE

Will print:

  A = 7, B = 9, C = 0
 A = 7, B = 9, C = 63

See also

  • Calculation strategy
  • Link
  • Pointer
  • Assignment

Literature

  • V.V. Faronov. 8.2.2. Parameters // 8.2. Description of the subroutine // Chapter 8. Procedures and functions // Illustrated tutorial on Turbo Pascal .

Links

  1. ↑ The GNU C Programming - Actual parameters and formal parameters .
  2. ↑ Definition and call of functions (unspecified) .
  3. ↑ Fredrik Lundh. <span lang = " (English) " xml: lang = " (English) "> Call By Object (unspecified) . effbot.org .
  4. ↑ Iota Language Definition (neopr.) . CS 412/413 Introduction to Compilers . Cornell University (2001).
Source - https://ru.wikipedia.org/w/index.php?title=Parameter_ ( programming )&oldid = 102347392


More articles:

  • Ibero-American Institute
  • Naryshkutan
  • Zagorsky, Konstantin Ivanovich
  • Wallfish, Benjamin
  • Nandronga Navosa Province
  • Rodionova, Irina Gavrilovna
  • Kuptsov, Artyom Vladimirovich
  • England National League 2017/2018
  • H2-histamine receptor
  • Bernhard, Crown Prince of Baden

All articles

Clever Geek | 2019