Parts of The Programming Method
December 8, 2022
Read Time: 2 mins 12 secs
Word Count: 525
In Object-Oriented Programming, methods are the operations associated with a class. Methods are defined inside a class. Methods give an object of a class its behaviour. Sending a message to an object is the equivalent of invoking a method in the object. Methods are like functions in other programming paradigms. The parts of the method are:
- Method Name
- Return type
- The Data type of parameters
- Order of Parameters
- Arity
- Signature
- Body
Method Name
Every method has it's own name. The method name is the unique name used to define or name the method. This is the identifier used to invoke the method. The execution of the program branches to the method's body when that name appears in a program. When a method ends, execution moves back to the place in the program's code from which it was called and moves on to the following line of code.The method name
should follow the identifier naming conventions of the
implementing language (Pascal or Camel Casing).
e.g.
example();
methodName();
nameOfMethod();
Return Type
The data type of the value returned by the method is the method's return type. The data type specifies and limits the data type of the value that a method or function returns. When declaring a function in many programming languages the return type must be stated explicitly. It is usually found before the method name
e.g.
int methodName( );
float methodName( );
boolean methodName();
void methodName( );
Data Type of Parameters
Each formal argument passed into the method has its own data type. A method may also not be passed any arguments and will therefore have an empty parameter list.
e.g.example(int argument);
methodName(float argument);
nameOfMethod(string argument);
Order of Parameters
The names given to the arguments passed to the method do not matter, only the order and the data type. Therefore void method(int arg1, float arg2){}and void method(int x, float y){} are equivalent, however void method(float a, int b){} or void method(double a, double b){} are not.
Arity
TThe arity of a method is the number of arguments or parameters that the method accepts. The arity is always given as a positive integer, therefore a method CANNOT have a -1, etc arity. The minimum and maximum arity of a function can be determined by looking at the number of arguments that the function expects to receive. The number of arguments required to call a fixed-arity function must match the number of parameters listed in its declaration while a function with variable arity can have a different number of parameters for each call.
Signature
The signature of a method is the name of the method and its parameter list including the data type, arity, and order of the arguments but not the return type. The focus on just the method name and argument list is due to overloading. The ability to design methods with the same name but different inputs. The method signatures allow the compilers or virtual machines to distinguish between the various methods.
Body
The body of a method contains the executable statements to perform the operation it is supposed to carry out. The body of the method is used to determine the purpose or behavior of the method.