A method is a procedure or function in OOPs Concepts. Whereas, a function is a group of reusable code which can be used anywhere in the program. This helps the need for writing the same code again and again. It helps programmers in writing modular codes.
- A method also works the same as that of function.
- A method is defined inside a class. For Example: main() in Java
- A method can be private, public, or protected.
- The method is invoked by its reference/object only. For Example: If class has obj as an object name, then the method is called by:
- A method is able to operate on data that is contained within the class
- Each object has it’s own method which is present in the class.
- A function is a block of statements that takes specific input, does some computations, and finally produces the output.
- A function is defined independently. For Example: main() in C++
- By default a function is public.
- It can be accessed anywhere in the entire program.
- It is called by its name itself.
- It has the ability to return values if needed.
- If a function is defined, it will be the same for every object that has been created.
Below is the program to illustrate functions and methods in C++:
13 15 66 66 37 8 8 11 52
Welcome to GfG!
Will you eat fruit? Yes, I want fruit! Give an Apple.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Difference between regular functions and arrow functions
- Methods to concatenate string in C/C++ with Examples
- Default Methods in C++ with Examples
- isalpha() and isdigit() functions in C with cstring examples.
- strtok() and strtok_r() functions in C with examples
- Commonly used String functions in C/C++ with Examples
- Hiding of all overloaded methods with same name in base class
- Different methods to reverse a string in C/C++
- 5 Different methods to find length of a string in C++
- Different methods to copy in C++ STL | std::copy(), copy_n(), copy_if(), copy_backward()
- Calling virtual methods in constructor/destructor in C++
- Green vs Native Threads and Deprecated Methods in Java
- C++: Methods of code shortening in competitive programming
- Differences between wait() and join() methods in Java
- Difference between the Constructors and Methods
- Implementation of file allocation methods using vectors
- Implementation of all Partition Allocation Methods in Memory Management
- Differentiate between Write Through and Write Back Methods
- Difference between next() and nextLine() methods in Java
- Can static functions be virtual in C++?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.