Methods vs Functions in C++ with Examples

In this tutorial, we are going to learn about the aspects that make methods different from functions in C++. A function contains a set of instructions that are used to perform a specific task. The advantage of using functions is code reusability. In general, methods are called member functions of a class in C++.

The main difference between methods and functions is the methods are defined under a class but the functions are defined outside a class i.e the functions do not belong to a particular class.

Declaring a method inside a class in C++:

#include <bits/stdc++.h>
using namespace std;
class CodeSpeedy{   
    public:             
        void print();
};
int main() {  

}

In the above code, print() is a method under the class CodeSpeedy.

Declaring a function in C++:

#include <bits/stdc++.h>
using namespace std;
void print();
int main() {  

}

In the above code, print() is a function.

 

We can call a method only by using the objects of a class. But we can use a function without such restriction. We can call a function independently from anywhere inside a program.

Calling a method in C++:

#include <bits/stdc++.h>
using namespace std;
class CodeSpeedy{   
    public:             
        void print(){
            cout<<"Welcome to CodeSpeedy"<<endl;
        }
};
int main() {  
    // Creating an object
    CodeSpeedy obj;
    // Calling the method using the object
    obj.print();
}

Output:

Welcome to CodeSpeedy

Calling a function in C++:

#include <bits/stdc++.h>
using namespace std;
void print(){
    cout<<"Welcome to CodeSpeedy"<<endl;
}
int main(){
    print();
}

Output:

Welcome to CodeSpeedy

 

We can call both methods and functions multiple times. Functions and methods do a similar job. They take the necessary inputs as arguments and provides us the required results.

The main advantage of using methods is its ability to access or modify all kinds of data members of an object. We cannot directly access or modify private and protected members of an object. For example execution of the below-given code results in a compile-time error.

#include <bits/stdc++.h>
using namespace std;
class Animal{
    private:
        string name;
    public:
        Animal(string s){
            name=s;
        }
};
int main(){
    Animal a("Tiger");
    cout<<a.name<<endl;
}

In the above code, we are trying to access the private data member directly which results in a compile-time error. But we can access or modify all kinds of members using methods.

#include <bits/stdc++.h>
using namespace std;
class Animal{
    private:
        string name;
    public:
        Animal(string s){
            name=s;
        }
        // Method for accessing the name
        string Name(){
            return name;
        }
};
int main(){
    Animal a("Tiger");
    cout<<a.Name()<<endl;
}

Output:

Tiger

In the above code, we are using a method called Name() for accessing the name of the object.

 

We hope that you have understood the variation between a method and a function.

Also read: std::stoul and std::stoull functions in C++

List in C++: Basic Functions

Leave a Reply

Your email address will not be published. Required fields are marked *