What's the difference between a method and a function?

前端 未结 30 3245
粉色の甜心
粉色の甜心 2020-11-21 05:08

Can someone provide a simple explanation of methods vs. functions in OOP context?

30条回答
  •  无人及你
    2020-11-21 05:43

    Function or a method is a named callable piece of code which performs some operations and optionally returns a value.

    In C language the term function is used. Java & C# people would say it a method (and a function in this case is defined within a class/object).

    A C++ programmer might call it a function or sometimes method (depending on if they are writing procedural style c++ code or are doing object oriented way of C++, also a C/C++ only programmer would likely call it a function because term 'method' is less often used in C/C++ literature).

    You use a function by just calling it's name like,

    result = mySum(num1, num2);
    


    You would call a method by referencing its object first like,

    result = MyCalc.mySum(num1,num2);
    

提交回复
热议问题