2017-06-02 71 views
-4

所有這些功能以外的INT主()內:如何使用參數使用函數的返回值的另一個功能

int func1(int x) { 

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

int func2(){ 
    int v2 = func1()/4; // It's suppose to be 12/4 
    //I get "too few arguments in function call" 
} 

我知道func1()內第2個功能缺乏論據,這就是原因「太少的論據「警告。我想知道的是,如何使用func2()func1()的返回值作爲變量的值。

這樣,與不帶參數的功能,這很好地工作:

 int sum() { 

     int v1 = 10; 
     int v2 = 4; 
     int v3 = v1 + v2; // 10 + 4 = 14 
     return v3; // v3 = 14 
    } 

    int sub() { 

     int v4 = sum() - 7; // 14 - 7 = 7; 
     return v4; // v4 = 7 
} 

感謝。

編輯:對不起,我想我已經說清楚了,當我說我理解了警告的原因。我的錯。

我需要用戶在函數1中的輸入是可變的。

因此,在main()中,當提示用戶提供一個數字時,它將被傳遞給函數1.參數不是固定的,而是可變的。

編輯2: 這裏是背後的主要想法:

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


} 

回答

4

你得到這個錯誤是因爲你必須給函數所需的所有參數。你沒有給出一個函數的參數,所以它不起作用。SUM()犯規收到任何論據,它被稱爲像這樣

sum() 

FUNC1()需要一個參數,因此無論是將其更改爲不帶任何參數

int func1() { 

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

功能,但它表明,你還沒有想過,你在做什麼,它有點沒用

或調用它在不改變功能

int v2 = func1(2)/4; 

編輯
所以,你可以改變FUNC2也得到一個參數,並把它傳遞給FUNC1

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

,並在主要做

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

還小的事你FUNC2()應該返回INT但你沒有return語句

+0

謝謝。正是我需要的。 – JohnnyJohnny

2

應該是:

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

每當你調用一個函數,你必須通過它需要的參數。

由於沒有傳遞適量的參數,你會得到「太少的參數」!

+0

但我需要的參數是可變的。我表達得很差。已經修復了帖子。抱歉。 – JohnnyJohnny

+2

@JohnnyJohnny:所以傳遞一個變量。你沒有「固定該帖子」;至於你的實際問題還是非常不清楚。 –

+0

@JohnnyJohnny然後將該數字作爲參數傳遞給func2,然後將其傳遞給func2中的func1。 – Carcigenicate

1

EDITED ANWSER

如果您需要的變量是用戶輸入,那就試試這個:

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"; 
} 

然後你的職責應該是這樣的:

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

由於讀取了未初始化的變量,您的代碼可能具有UB。 **檢查你的I/O是否成功** –

+0

@Dat Hat謝謝。差不多了。另外,你問? – JohnnyJohnny

+0

@JohnnyJohnny你問'你問過什麼?' –

0

你能澄清你的問題?從我的理解你正在做你想要的東西正確。

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 
} 

如果一個方法請求「N」參數來調用該方法你必須給它的每一個「N」參數(apropriate類型)的。

忽略過載/默認值等

0

我可能會誤解你的問題,但是這對我的作品:

$ 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 
1

功能需要一個自變量,所以它傳遞的參數。

就這麼簡單。

相關問題