Sunday, 21 July 2024

Method vs function in Programming language

 

The programming languages have two concepts’ functions and methods. functions are defined in structural language and methods are defined in object-oriented language.

Function & Method is a block of code that performs a task. All of these enhance code quality and reusability.

The difference between both is given below:


Method

Function

Methods are defined in object-oriented languages like C#, Java

Functions are defined in structured languages like Pascal,C and object based language like javaScript

Methods are called using instance or object.

Functions are called independently.

 

Methods do not have independent existence they are always defined with in class.

Functions have independent existence means they can be defined outside of the class.

We can control access using access specifier.

We cannot restrict access

Methods are tied to objects and operate within a class.

 

Functions are independent, reusable code blocks.

 

Can access and manipulate object data

Can have parameters and return values

Called using the object’s name and the method name

Called by their name

Often changes the state of an object

Typically stateless

It can be public or private.

Has no visibility control, accessible from anywhere in the code.

Can access and modify the state of an object.

Cannot access or modify the state of an object.

Enables encapsulation and abstraction in object-oriented programming.

Does not support encapsulation or abstraction.

Invoked using the object or class instance followed by the dot operator (.).

Invoked using the function name followed by parentheses ().

It accepts ref variable as input

No ref variable


No comments:

Post a Comment

Thanks for the contribution, our team will check and reply back if response required.