[問題] 我的副函數無法傳值到主函數

看板C_and_CPP (C/C++)作者 (數學一等兵)時間10年前 (2015/07/13 05:47), 10年前編輯推噓0(005)
留言5則, 2人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) 我是把筆電(windows7)上Dev C++裝好就寫的,不知道應該是算是那個平台 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 副函數findroot應該能把平方根找到 但傳到主函數變數b時,總是得到0 餵入的資料(Input): 一個數 預期的正確結果(Expected Output): 例:輸入121,可以得到11,而不是0 錯誤結果(Wrong Output): 總是得到平方根是0 程式碼(Code):(請善用置底文網頁, 記得排版) #include <iostream> #include <cstdlib> #include <cmath> using namespace std; double findroot(double a, double c){ if(((a/c)-c)*((a/c)-c)<0.00000000001){ return c; } else { findroot(a,((a/c)+c)/2); } } int main(void){ //變數a,及其平方根b double a,b; //呈現使用說明 cout << "本程式將找出使用者給定之數的正平方根" << endl << "請輸入數字:" ; cin >> a; //計算平方根 b= findroot(a,1); //顯示結果 cout << a << " 的正平方根為 " << b; system("pause"); } 補充說明(Supplement): 謝謝大家指教! -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 49.158.107.184 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1436737662.A.AAF.html ※ 編輯: eggsu (49.158.107.184), 07/13/2015 05:50:01

07/13 05:50, , 1F
else 沒 return ?
07/13 05:50, 1F

07/13 05:52, , 2F
else 不 return 啊……還沒有找到值,繼續找
07/13 05:52, 2F

07/13 05:53, , 3F
我懂了。試試!
07/13 05:53, 3F

07/13 05:54, , 4F
遞迴一樣要return
07/13 05:54, 4F

07/13 06:06, , 5F
解決了,感恩!
07/13 06:06, 5F
文章代碼(AID): #1Lek1-gl (C_and_CPP)
文章代碼(AID): #1Lek1-gl (C_and_CPP)