LoginSignup
1
2

More than 5 years have passed since last update.

関数内の変数にアクセスする方法(参照渡し)

Posted at

参照渡しってなんだっけ??って言葉でわからなくても、
こういうときに使うやつか!って納得する概念。

簡単な例


#include <iostream>

using namespace std;

int func(int ix, int iy, int& a, int& b){

    a = ix*10;
    b = iy*10;

    return a;
}

int main(){

    //関数内の変数をここで宣言
    int a, b;

    int x = 3, y = 5;

    //
    func(x,y,a,b);

    //30 50 3 5 が出力される
    cout << a << " " << b << " " << x << " " << y << "\n";

}

関数内の変数にアクセスする方法として、
関数内で構造体を作る方法などもあるらしいですネ

swiftならタプルでチョチョイなんだけど…

1
2
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2