Methods vs. Functions in C++ with Examples - GeeksforGeeks

01 Jun, 2020
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.
Methods:
A method also works the same as that of function.A method is defined inside a class. For Example: main() in JavaA 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:obj.method();
A method is able to operate on data that is contained within the classEach object has it’s own method which is present in the class.
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:obj.method();

obj.method();

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.
Functions:
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.
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++:
Program 1:
// C++ program to illustrate functions#include "bits/stdc++.h"using namespace std;  // Function Call to print array elementsvoid printElement(int arr[], int N){      // Traverse the array arr[]    for (int i = 0; i < N; i++) {        cout << arr[i] << ' ';    }}  // Driver Codeint main(){      // Given array    int arr[] = { 13, 15, 66, 66, 37,                  8, 8, 11, 52 };      // length of the given array arr[]    int N = sizeof(arr) / sizeof(arr[0]);      // Function Call    printElement(arr, N);    return 0;}
13 15 66 66 37 8 8 11 52

Program 2:
// C++ program to illustrate methods// in class#include "bits/stdc++.h"using namespace std;  // Class GfGclass GfG {private:    string str = "Welcome to GfG!";  public:    // Method to access the private    // member of class    void printString()    {          // Print string str        cout << str << '\n';    }};  // Driver Codeint main(){      // Create object of class GfG    GfG g;      // Accessing private member of    // class GfG using public methods    g.printString();      return 0;}
Welcome to GfG!

Program 3:
// C++ program to illustrate methods// and functions#include <iostream>using namespace std;  // Function call to return stringstring offering(bool a){    if (a) {        return "Apple.";    }    else {        return "Chocolate.";    }}  // Class Declarationclass GFG {public:    // Method for class GFG    void guest(bool op)    {        if (op == true) {            cout << "Yes, I want fruit!\n";        }        else {            cout << "No, Thanks!\n";        }    }};  // Driver Codeint main(){    bool n = true;      cout << "Will you eat fruit? ";      // Create an object of class GFG    GFG obj;      // Method invoking using an object    obj.guest(n);      if (n == true) {          // Append fruit using function        // calling        cout << "Give an " + offering(n);    }      // Giving fruit..Function calling    else {          // Append fruit using function        // calling        cout << "Give a " + offering(n);    }      return 0;}
Will you eat fruit? Yes, I want fruit!
Give an Apple.

CPP-Functions
C++
Difference Between
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Operator Overloading in C++
Polymorphism in C++
Friend class and function in C++
Sorting a vector in C++
Convert string to char array in C++
Difference between BFS and DFS
Class method vs Static method in Python
Differences between TCP and UDP
Difference between var, let and const keywords in JavaScript
Difference between Process and Thread