How To Call A Function Method

Table of contents:

How To Call A Function Method
How To Call A Function Method

Video: How To Call A Function Method

Video: How To Call A Function Method
Video: The 'call' Method in JavaScript | Function Call Explained 2024, May
Anonim

A method is a function that is one of the properties of a class or object that belongs to it. If we are talking about a method, then we mean that the paradigm of object-oriented programming is used.

How to call a function method
How to call a function method

Instructions

Step 1

A class method is a sequence of actions, it can take arguments and return a value, although neither is required. In the most general case, the call line looks like this: variable = object_name.object_method (list of arguments); The call syntax is very different, it all depends on which programming language is used, and even within the same language, you can call a method in very different ways. The first thing to do is to specify for which object or class the function will be called. Sometimes you don't need to do this if you are already working in the required namespace, for example, inside another class method.

Step 2

Then you need to call it, guided by the name of the method or in another way that is acceptable for a particular programming language, to explicitly point to it. Typically, a method follows the name of the object on which it is called, separated by a dot: object_name.method (). Some languages require other delimiters to be used, such as space or colon. If a control construct is used, which assumes that all actions are performed within the designated object (for example, in a number of languages such actions are allowed by the with control), then the compiler is already clear with which object the actions will be performed. You just need to specify the name of the method.

One of the features of object-oriented programming is namespace delineation. It is very important to keep this in mind when calling class methods. The method itself is an interface that already provides encapsulation.

Step 3

Next, you need to specify the arguments that the method requires. Usually the argument list is enclosed in parentheses. Many modern compilers, when describing a function, prompt the programmer about the types of arguments and their names, so that it is easier for a person to navigate and not pass data in the wrong order. Method developers can write them in such a way that an object can be passed as an argument, this approach avoids confusion. Many languages allow you to call methods of objects in such a way that the object itself is also passed as an argument.

Step 4

If the method returns a result, then most likely it should be written. Create or select a variable to store it and assign a function call to it. When it completes execution, it will return the result, which will be written to the memory area you specified. Some methods do not return anything, they just perform some kind of operation on the object. In this case, you do not need to worry about storing the result of the function in a variable.

Recommended: