LoginSignup
0
0

More than 5 years have passed since last update.

関数とメソッド

Posted at

こちらで指摘いただいたのですが、関数とメソッドが分からなくなり検索。

IT用語事典によると

関数とは、引数と呼ばれるデータを受け取り、定められた通りの処理を実行して結果を返す一連の命令群。

メソッドとは、オブジェクト指向プログラミングにおいて、各オブジェクトが持っている自身に対する操作。
という感じの意味だそうです。

//メソッドの定義
class Add {
    int add(int x,int y) {
        return x + y;
    }
}
//メソッドの呼び出し
public class Main {
    public static void main(String[]args) {
        Add a = new Add();
        a.add(1, 3);

    }
}
public class Main {
//関数の定義
    int add(int x, int y) {
        return x + y;
    }
//関数の呼び出し
    int c = add(1,2);
}

javaだとこんな感じになるんだと思います。
関数をおなじクラス内で使っていると関数。
関数をクラスで定義して、そのクラスからインスタンスを生成し、別のクラスで使用するとメソッドと呼ばれる。

参考にさせていただきました。
https://qiita.com/T-N0121/items/ecf5b911463ac9fa1d3e

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