0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

戻り値を返す、受け取る

Last updated at Posted at 2024-10-12
public static Main {
    public static void main(String[] args) {
        // ③戻り値を受け取る
        String return = function(str: "ABC");
        // ④戻り値を出力する
        System.out.println(return);
    }

    // public static voidだと戻り値を返さないということ(返す時には型を指定する)
    public static String function(String str) {
        // ①戻り値を設定する
        str = "(" + str + ")";
        // ②戻り値を返す
        return str;
    }
}

結果

(ABC)

main(親)はreturn, function(子)はstrとなっていますが、
親と子の戻り値を受け取る/返す時の変数名は異なっていても問題ない。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?