0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

# Dartの関数

Posted at

Dartの関数

Dartでは関数の省略可能な引数の書き方がいくつかある。

引数名を指定して値を受け渡す関数の呼び出し方法が特徴的。

// 通常
int addIntNormal(int val1, int val2){
	return val1 + val2;
}

// 1行記法
int addIntOneLine(int val1, int val2) => val1 + val2;

// {}の付与で名前付き任意引数
int addIntOmit1({int val1, int val2}) {
	return val1 + ( val2 ?? 30 );
}
int addIntOmit2({int val1, int val2 = 30}) {
	return val1 + val2;
}

// 引数の一部のみを任意にする
int addIntOmit3(int val1, [int val2 ]) {
	return val1 + (val2 ?? 30);
}

main(){

	int x = 70;
	int y = 30;
	
	print(addIntNormal( x, y ));
	print(addIntOneLine( x, y ));
	print(addIntOmit1( val1 : x ));		// 引数名を指定
	print(addIntOmit2( val1 : x ));		// 引数名を指定
	print(addIntOmit3( x ));
}
// 実行結果
100
100
100
100
100
0
1
0

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?