code.club

 找回密碼
 立即註冊
搜索
查看: 6352|回復: 0

函數的傳值或傳址

[複製鏈接]
發表於 2017-1-4 02:45:50 | 顯示全部樓層 |閱讀模式
未特別指定是傳址例如 func(int &x)的話,一般是傳值,所以不會改變傳進去的那個變數原值。

#include <iostream>
using namespace std;

int x = 9;

int add(int j){
        j = j+2;
        return j;
}

int add(){
        x = x+2;
        return x;
}

int main(){
        cout<<add(x)<<endl;//cout 11
        cout<<x<<endl;// cout 9;
       
        cout<<add()<<endl;//cout 11
        cout<<x<<endl;//cout 11
        return 0;
}
回復

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

小黑屋|手機版|Archiver|code.club  

GMT+8, 2024-3-29 03:30 , Processed in 0.107293 second(s), 28 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表