LoginSignup
0
0

More than 1 year has passed since last update.

Javaのメソッドについて_引数と戻り値を追加

Last updated at Posted at 2023-03-14

paizaラーニングforTEAM Java入門編
レッスン6の#03

6-#03
// 引数と戻り値を追加する

public class Main {
    public static void main(String[] args) {
        int num1 = sum(3, 4); // sumメソッドの呼び出し_引数が3と4_戻り値をnum1に代入
        System.out.println(num1); // num1を出力
        int num2 = sum(300, 400); // sumメソッドの呼び出し_引数が300と400_戻り値をnum2に代入
        System.out.println(num2); // num2を出力
    }
    
    // 足し算をするsumメソッドを定義
    public static int sum(int x, int y) {
        // メソッドの結果を再利用したい場合に戻り値を記述する
        // メソッド定義では戻り値のデータ型を記述(ここではint)、戻り値がない場合はvoid
        // メソッド定義の引数にはデータ型と変数名を記述
        return x + y; // メソッドの中身_ここではメソッドの返り値
    }
}
0
0
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
0