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

后端 未结 6 1267
孤城傲影
孤城傲影 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:34

    EDITED ANWSER

    If you need the variable to be a user input, then try this:

    int main() {
        int x = 0;
        cin >> x;
        int y = func1(x); // send user input to func1, then to func2
        int e = func2(y); // return result of above to int e
        cout << e << "\n";
    }
    

    And then your functions should look like this:

    int func1(int x) {
        int v1 = 6 * x;
        return v1; // the input argument will be 2, so v1 = 12
    }
    
    int func2(int x){
        int v2 = func1(x) / 4; // It's suppose to be 12 / 4
     }
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-01-29 16:41

    I may be misunderstanding your question, but this works for me:

    $ cat test.c
    #include <stdio.h>
    #include <stdlib.h>
    
    int func1(int x)
    {
        int v1 = 6 * x;
        return v1; // the input argument will be 2, so v1 = 12
    }
    
    int func2(int x)
    {
      int v2 = func1(x) / 4; // It's suppose to be 12 / 4
      return v2;
    }
    
    int main( int argc, char *argv[] )
    {
      printf( "%i\n", func2( 2 ) );
      return 0;
    }
    
    $ gcc test.c
    
    $ ./a.out
    3
    
    0 讨论(0)
  • 2021-01-29 16:43

    Could you clarify your question? From my understanding you're doing what you want correctly.

    double add_5(double x) {
        return x + 5;
    }
    double nested_method(double x) {
        double myVariable = add_5(x); //my variable = x + 5
        //other code//
        return myVariable;
    }
    int main() {
    double myInput = 123;
        std::cout << nested_method(myInput) << std::endl; //output should = 128
    }
    

    If a method asks for 'n' parameters to call the method you must give it each of those 'n' parameters (of the apropriate type).

    ignoring overloads / default values etc

    0 讨论(0)
  • 2021-01-29 16:47

    The function needs an argument, so pass it an argument.

    It's as simple as that.

    0 讨论(0)
  • 2021-01-29 16:52

    Should be:

    int func2(){
        int v2 = func1(2) / 4; // It's suppose to be 12 / 4
        return v2;
    }
    

    Whenever you call a function, you have to pass the parameters it requires.

    You get "too few arguments" because you're not passing the right amount of arguments!

    0 讨论(0)
提交回复
热议问题