Method

Method
A method is some operation that you expect to be doing over and over. A method is a way of grouping these operations to prevent you from rewriting the same code in your program. They also make it easier to make minor changes that would have taken hours if you had written it without methods. Methods in different languages are incredibly similar in their syntax. The method has a return type, pass values, a name, and a body. There are class methods and global methods, both with their respective scope.

A basic method: code format="cpp" int max(int x, int y){ if(x > y) return x;   return y; } code

In this method, int is the return type, the name is "max", and "x" and "y" are the pass values. A method can also have a return type of 'void' which means it doesn't return anything. This often used for a print function. A method can be 'called' by using the name followed by the pass values in parentheses.

Calling a method: code format="cpp" int main{ int val; val = max(2, 3); //the method will return 3

return 0; } code

A method can be **overloaded** by simply having another method of the same name, but with different pass values or return types. An overload of the above max method for floats: code format="cpp" float max(float x, float y){ //overload if(x > y) return x;   return y; } code

A method can be **overridden** by having the exact same method that is in the parent class in a child class. code format="java" class Parent{ int open{ return 0; } } class Child extends Parent{ int open{ //override return 1; } } code

In C++ you can make a prototype of methods that you plan to create. This is useful if your methods call other methods in the same file. code format="cpp" int max(int, int); //some prototypes float max(float, float); void print; code