Getting function requirements in a function call

When calling a function that has requirements received in other functions, is it better or worse to have a function call for one of the requirements in a general function call?

I demonstrated this simple example:

int amountToMultiplyBy(int multiplyAmount)
{   
    int temp;
    std::cout << "how much do you want to multiply by: ";
    std::cin >> temp;
    multiplyAmount = temp;
    return multiplyAmount;
}
void sumOfNumbers(int numOne, int numTwo, int multiplyAmount)
{
    std::cout << "1: " << numOne * multiplyAmount << std::endl;
    std::cout << "2: " << numTwo * multiplyAmount << std::endl;
}

main version 1:

int main()
{
    int multiplyAmount;    
    sumOfNumbers(5, 10, amountToMultiplyBy(multiplyAmount));
    return 0;
}

main version 2:

int main()
{
    int multiplyAmount;
    multiplyAmount = amountToMultiplyBy(multiplyAmount);
    sumOfNumbers(5, 10, multiplyAmount);    
    return 0;
}

In version 1, the call amountToMultiplyByis within the call sumOfNumbers, receiving a value for multiplyAmountduring the call.

In version 2, it amountToMultiplyByis called first, giving multiplyAmountits value, which is then included in the call sumOfNumbers.

I'm just curious to know if this is good practice, bad practice or just the exact same?

+4
source share
4 answers

. , . , .

, , , .

+4

:

1:

00af 8B45FC         movl    -4(%rbp), %eax
00b2 89C7           movl    %eax, %edi
00b4 E8000000       call    _Z18amountToMultiplyByi
     00
00b9 8945FC         movl    %eax, -4(%rbp)
00bc 8B45FC         movl    -4(%rbp), %eax
00bf 89C2           movl    %eax, %edx
00c1 BE0A0000       movl    $10, %esi
     00
00c6 BF050000       movl    $5, %edi
     00
00cb E8000000       call    _Z12sumOfNumbersiii

2:

00af 8B45FC         movl    -4(%rbp), %eax
00b2 89C7           movl    %eax, %edi
00b4 E8000000       call    _Z18amountToMultiplyByi
     00
00b9 8945FC         movl    %eax, -4(%rbp)
00bc 8B45FC         movl    -4(%rbp), %eax
00bf 89C2           movl    %eax, %edx
00c1 BE0A0000       movl    $10, %esi
     00
00c6 BF050000       movl    $5, %edi
     00
00cb E8000000       call    _Z12sumOfNumbersiii

, .

+1
int main()
{
    int multiplyAmount = amountToMultiplyBy();
    sumOfNumbers(5, 10, multiplyAmount);    
    return 0;
}

int main()
{
    sumOfNumbers(5, 10, amountToMultiplyBy());    
    return 0;
}

, . .

. , . , undefined.

0

amountToMultiplyBy , multiplyAmount , ? :

int amountToMultiplyBy()
{   
    int temp;
    std::cout << "how much do you want to multiply by: ";
    std::cin >> temp;
    return temp;
}

:

sumOfNumbers(5, 10, amountToMultiplyBy());

since you do not need a new variable to store your data

PS

int multiplyAmount = amountToMultiplyBy(multiplyAmount)

better than

int multiplyAmount;
multiplyAmount = amountToMultiplyBy(multiplyAmount)

better initialize the variable in the declaration

0
source

All Articles