If the reasoning of the function is an index or a reference, it is mandatory to return it from the function



  • The function could be:
    - argument
    - Reference to the argument
    - Indicator for argument (for variable)

    Now the value of the return. ♪ For example, the function needs to change the variable
    - if I'm putting the argument in the function, you need to get back, that's understandable.

    You're going to have to get a modified value from the function if I referred it to the variable or indicator?
    Isn't that the way it's gonna change by reference or indicator?

    Final version of the use of indicators in the function

        #include "stdafx.h"
        #include <iostream>
        using namespace std;
        //
        // определения функций (!)
        //
        int mul(int );  // с обычным аргументом
        void div(int &); // с сылкой на аргумент
        void sqr(int *); // с указателем на аргумент
    
    int main(void)
    {
        int a = 6;
        mul(a); /* передача аргумента (копии) */
        cout&lt;&lt;"6 * 2 = "&lt;&lt;a&lt;&lt;endl;
        a = 6;
        div(a);  /* передача аргумента (функция принемает ссылку на аргумент) */
        cout&lt;&lt;"6 * 2 = "&lt;&lt;a&lt;&lt;endl;
        a = 6;
        sqr(&amp;a);  /*  передача указателя с адресом на значение аргумента */
        cout&lt;&lt;"6 * 6 = "&lt;&lt;a&lt;&lt;endl;
        system("pause");
     return 0;
    }
    
    //
    // Ф У Н К Ц И И (!)
    //
    int mul(int b){ /*прием аргумента A*/ 
        b *= 2;
        return b; /*для конечного изменения значения, необходимо  вернуть аргумент*/ 
    }
    void div(int &amp;b){ /*прием ссылки на аргумент A*/ 
        b /= 2;
    /*для конечного изменения значения, возвращать аргумент не требуется*/ 
    }
    void sqr(int *b){ /*прием указателя с адресом на значение аргумента A*/ 
        *b *= *b;
    /*для конечного изменения значения, возвращать аргумент не требуется*/ 
    }
    



  • You have a little misconception about arguments. The index is part of the type of variable, the reference indicates that we have another name for the same variable (one and the same area of memory). The function may take the meaning or the reference as an argument, the index is quite specific. Now let's see what's going on in three of your options.

    First option - int mul(int ); - Transfer of arguments on meaning. When we call a function, we have our variable. aa copy of this variable will be created when the function is transferred, and all actions in it will be copied, which will not change the reference value.

    Second option - int div(int &); - The referral. When we call a function, we have our variable. a, it will be transferred to the function, i.e. if we change the function, then the reference value will change.

    Third option - int sqr(int *); - It's the same transmission of the argument on meaning (as in option 1), only this time we're commemorating the storage of the indicator, recording the address of our variable. aand that's the address. The most variable a We don't do anything in this case until we turn to the index.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2