Methods vs. Functions in C++ with Examples Last Updated : 01 Jun, 2020 Comments Improve Suggest changes Like Article Like Report 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 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(); 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. Below is the program to illustrate functions and methods in C++: Program 1: CPP // 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; } Output: 13 15 66 66 37 8 8 11 52 Program 2: CPP // 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; } Output: Welcome to GfG! Program 3: CPP // 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; } Output: Will you eat fruit? Yes, I want fruit! Give an Apple. Comment More infoAdvertise with us Next Article Methods vs. Functions in C++ with Examples sofiaa Follow Improve Article Tags : Difference Between C++ Write From Home CPP-Functions Practice Tags : CPP Similar Reads norm() function in C++ with Examples The norm() function is defined in the complex header file. This function is used to return the squared magnitude of the complex number z. Syntax: template<class T> T norm (const complex<T>& z); Parameter: z: It represents the given complex number. Return: It returns the squared magni 1 min read ios rdstate() function in C++ with Examples The rdstate() method of ios class in C++ is used to read the internal state of this stream. Syntax: iostate rdstate() const; Parameters: This method does not accept any parameter. Return Value: This method returns the current internal state of this stream. Example 1: CPP // C++ code to demonstrate / 1 min read log2() function in C++ with Examples The function log2() of cmath header file in C++ is used to find the logarithmic value with base 2 of the passed argument. Syntax: log2(x) Parameters: This function takes a value x, in the range [0, ∞] whose log value is to be found. Return Type: It returns the logarithmic value, as double, flo 2 min read ios good() function in C++ with Examples The good() method of ios class in C++ is used to check if the stream is good enough to work. It means that this function will check if this stream has raised any error or not. Syntax: bool good() const; Parameters: This method does not accept any parameter. Return Value: This method returns true if 1 min read ios fail() function in C++ with Examples The fail() method of ios class in C++ is used to check if the stream is has raised any fail error. It means that this function will check if this stream has its failbit set. Syntax: bool fail() const; Parameters: This method does not accept any parameter. Return Value: This method returns true if th 1 min read ios clear() function in C++ with Examples The clear() method of ios class in C++ is used to change the current state of the specified flag by setting it. Hence this function changes the internal state of this stream. Syntax: void clear(iostate state) Parameters: This method accepts the iostate as parameter which is the flag bit to be set in 2 min read ios operator() function in C++ with Examples The operator() method of ios class in C++ is used to any error flag of this stream is set. This includes the failbit or the badbit. Syntax: operator void*() const; Parameters: This method does not accept any parameter. Return Value: This method returns a null pointer if any error bit is set of this 1 min read ios operator !() function in C++ with Examples The operator!() method of ios class in C++ is used to any error flag of this stream is set. This includes the failbit or the badbit. Syntax: bool operator!() const; Parameters: This method does not accept any parameter. Return Value: This method returns true if any error bit is set of this stream, e 1 min read ios operator() function in C++11 with Examples The operator() method of ios class in C++11 is used to any error flag of this stream is set. This includes the failbit or the badbit. Syntax: explicit operator bool() const; Parameters: This method does not accept any parameter. Return Value: This method returns false if any error bit is set of this 1 min read std::is_function template in C++ with Examples The std::is_function of C++ STL is used to check whether the given type T is function or not. It returns the boolean value either true or false. Below is the syntax for the same: Header File: #include<type_traits> Syntax: template <class T> struct is_function; Parameter: The template std 2 min read Like