7.3return語句

來源:互聯網
上載者:User

1.理解返回引用的至關重要的是:千萬不能返回局部變數的引用

2.引用返回左值

Code:
  1. #include <iostream>   
  2. #include <string>   
  3. using namespace std;   
  4. char &getStr(string &s,string::size_type ix)   
  5. {   
  6.     return s[ix];   
  7. }   
  8.   
  9. int main()   
  10. {   
  11.     string str("this is a string!");    
  12.     getStr(str,0) = 'T'; //引用返回左值   
  13.     cout << str;   
  14. }  

3.遞迴

遞迴必須有一個終止條件

Code:
  1. #include <iostream>   
  2. using namespace std;   
  3.   
  4. //求階乘   
  5. int factorial(int val)   
  6. {   
  7.     if(val > 1)   
  8.         return factorial(val-1) * val;   
  9.     return 1;   
  10. }   
  11.   
  12. int main()   
  13. {   
  14.     cout << factorial(3);   
  15. }   
Code:
  1. #include <iostream>   
  2. using namespace std;   
  3. //最大公約數   
  4. int r**(int v1,int v2)   
  5. {   
  6.     if(v2 != 0)   
  7.         return r**(v2,v1%v2);   
  8.     return v1;   
  9. }   
  10.   
  11. int main()   
  12. {   
  13.     cout << r**(200,350);   
  14. }  

4.主函數main不能調用自身

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.