Methods vs. Functions in C++ with Examples

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:

  1. A method also works the same as that of function.
  2. A method is defined inside a class. For Example: main() in Java
  3. A method can be private, public, or protected.
  4. 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();
    
  5. A method is able to operate on data that is contained within the class
  6. Each object has it’s own method which is present in the class.

Functions:

  1. A function is a block of statements that takes specific input, does some computations, and finally produces the output.
  2. A function is defined independently. For Example: main() in C++
  3. By default a function is public.
  4. It can be accessed anywhere in the entire program.
  5. It is called by its name itself.
  6. It has the ability to return values if needed.
  7. 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:



filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate functions
#include "bits/stdc++.h"
using namespace std;
  
// Function Call to print array elements
void printElement(int arr[], int N)
{
  
    // Traverse the array arr[]
    for (int i = 0; i < N; i++) {
        cout << arr[i] << ' ';
    }
}
  
// Driver Code
int 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;
}

chevron_right


Output:

13 15 66 66 37 8 8 11 52

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate methods
// in class
#include "bits/stdc++.h"
using namespace std;
  
// Class GfG
class 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 Code
int main()
{
  
    // Create object of class GfG
    GfG g;
  
    // Accessing private member of
    // class GfG using public methods
    g.printString();
  
    return 0;
}

chevron_right


Output:

Welcome to GfG!

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate methods
// and functions
#include <iostream>
using namespace std;
  
// Function call to return string
string offering(bool a)
{
    if (a) {
        return "Apple.";
    }
    else {
        return "Chocolate.";
    }
}
  
// Class Declaration
class 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 Code
int 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;
}

chevron_right


Output:

Will you eat fruit? Yes, I want fruit!
Give an Apple.

Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.