如何将值返回到另一个函数

How to return a value to another function

本文关键字:另一个 函数 返回      更新时间:2023-10-16

我似乎无法弄清楚如何做这项工作,随着我几天的尝试,任何帮助都将不胜感激:(

#include <iostream>
#include <math.h>
using namespace std;
int input;
int sum;
int number1;
int number2;
int number3;
void isArmstrong (int input, int sum)
{
    if (input == sum)
        cout << input << " is an Armstrong number" << endl;
    if (input != sum)
        cout << input << " is not an Armstrong number" << endl;
}

cubeofdigits不返回输入并总和到isarmstrong,(返回输入,sum)错误如下:表达结果未使用[-wunused-value]

int cubeOfDigits (int input, int sum, int number1, int number2, int number3)
{
    cout << "Enter an integer between 0-999" << endl;
    cin >> input;
    number1 = input / 100;
    number2 = input % 100;
    number3 = number2 % 10;
    sum = pow(number1, 3) + pow(number2, 3) + pow(number3, 3);
    isArmstrong(input, sum);
    return input,sum;
}

主呼叫CubeofDigits

int main(void)
{
    cout << "Welcome" << endl;
    cubeOfDigits(input, sum, number1, number2, number3);
    return 0;
}

我认为您的意图是..

#include <iostream>
#include <math.h>
using namespace std;
int input;
int sum;
int number1;
int number2;
int number3;
void isArmstrong(int input, int sum){
if(input == sum)
cout << input << " is an Armstrong number" << endl;
if(input != sum)
cout << input << " is not an Armstrong number" << endl;
}
int cubeOfDigits (int input)
{

number1 = input/100;
number2 = input % 100;
number3 = number2 % 10;
number2 = number2/10;
sum = pow(number1,3) + pow(number2,3) + pow(number3,3);

return sum;
}
int main(void){
cout << "Welcome" << endl;
cout << "Enter an integer between 0-999" << endl;
cin >> input;
isArmstrong(input,cubeOfDigits(input));
return 0;
}

将CubeOfDigits函数从(int)CubeofDigits更改为(void)CubeofDigits,然后从CubeofDigits函数末尾删除返回语句。>