- In computer programming, a function (also referred to as a procedure, method, subroutine, routine, or subprogram) is a callable unit of software logic that has a well-defined interface and behavior. It can be invoked multiple times within a program to execute specific tasks.
- A method differs from simply wrapping statements inside a block with a label. With methods, we can pass arguments and receive outputs, making them more flexible and dynamic. They offer higher modularity, allowing us to break down complex programs into manageable, reusable parts.
- Additionally, methods can be organized into libraries that can be imported into different parts of a program or even across multiple projects, promoting better code organization and reusability.
- Methods are used to encapsulate a series of statements under a label, making it easier to recall and reuse the code. This approach helps reduce boilerplate code, which refers to repetitive sections of code that appear multiple times within a program.
- To minimize boilerplate code, developers can apply the DRY (Don't Repeat Yourself) design principle. This principle encourages avoiding code duplication by structuring programs in a way that reduces repetition. By following DRY, you can prevent code from becoming WET (Write Everything Twice) and improve the overall efficiency and maintainability of your code.
Here is a list of advantages of using methods in programming:
- Code Reusability: Methods allow you to reuse the same code multiple times, reducing redundancy.
- Improved Readability: By encapsulating code in methods, the overall structure becomes cleaner and easier to read.
- Modularity: Methods divide a program into smaller, manageable sections, promoting modular design.
- Easier Debugging: Since methods are self-contained units, it is easier to isolate and debug individual sections of code.
- Reduced Code Duplication: Using methods helps implement the DRY principle, avoiding repeated code across the program.
- Simplifies Maintenance: Modifying a method's behavior in one place automatically updates all parts of the program that call it.
- Improved Collaboration: Methods make it easier for multiple developers to work on different parts of the codebase simultaneously.
- Abstraction: Methods hide complex code logic behind a simple interface, allowing the user to focus on functionality rather than implementation.
- Enhanced Testability: Isolated methods are easier to test individually, leading to more effective unit testing.
- Encapsulation: Methods can encapsulate functionality, improving the organization of the code and supporting object-oriented principles.

- Access modifier - controlling the level
- methods can be divided to 2 types: static (class/templates) and non static (instances)