Function and method are the same thing?

10

When we talk about methods and functions, are we saying the same thing?

For example:

function blablabla blabla

Is this a method?

    
asked by anonymous 12.06.2017 / 19:53

2 answers

15
It's not the same thing, but almost. The functionality of both is the same. It's a difference between imperative and object-oriented paradigm terminology.

The function is an algorithm, a set of instructions that does some processing, however small. In general functions produce some result. Functions can be given arguments.

The method is the same with the specialization that it is inside a class and usually works with the state of a given object. In the background this object is passed as an argument to the function so that its data can be manipulated. The method is considered a member that behaves over the state of the object.

The internal mechanism in the code is identical. It's practically a syntactic sugar to make this available in the local context.

Static Methods in the background are functions encapsulated within a class, ie it is just a where the function is.

Some languages may use specific terminology, but the concept is the same for all of them.

Read more about function: Who is who in using functions?

Related: What's the difference between functions and procedures?

    
12.06.2017 / 20:04
10

The answer can be yes and no this depends on the context.

Overall method and function are terms used to identify a named code block that can be reused in various parts of the program.

More specifically they are not the same thing. Functions are used in languages that use the imperative paradigm and another important feature they (most of the times) do not save states its execution is atomic or once all local variables have their values 'erased' or reset.

Methods This term is most commonly used with object orientation. An object / class is nothing more than a structure that combines data (properties) with behaviors (methods) and different from a function can save state.

    
12.06.2017 / 20:13