How to use the return value of a function with parameters inside another function

后端 未结 6 1322
孤城傲影
孤城傲影 2021-01-29 15:59

All these functions are outside the int main():

int func1(int x) {

    int v1 = 6 * x;
    return v1; // the input argument will be 2, so v1 = 12
}

int func2()         


        
6条回答
  •  执笔经年
    2021-01-29 16:38

    You get this error because you have to give the function all the arguments it needs. You are not giving an argument to a function so it will not work. sum() doesnt receive any arguments so it has to be called like this

    sum()
    

    func1() takes one argument so either change it to a function that takes no arguments

    int func1() {
    
        int v1 = 6 * 2;
        return v1; // the input argument will be 2, so v1 = 12
    }
    

    but it shows that you havent thought about what you are doing and its kinda useless

    or call it like this without changing the function

    int v2 = func1(2) / 4;
    

    EDIT
    So you can change func2 to also get one argument and pass it to func1

    int func2(int x){
        int v2 = func1(x) / 4;
     }
    

    and in main do this

    int x;
    cin>>x;
    func2(x);
    

    also small thing your func2() should return int but you dont have a return statement

提交回复
热议问题