DEV Community

Edwin Torres
Edwin Torres

Posted on

11

Java Tip: What are Methods, Functions, and Procedures?

Methods 🦁, Functions 🐯, and Procedures 🐻, Oh My!

Programmers often use these terms interchangeably. But there are differences among them. The following paragraphs describe methods, functions, and procedures in Java.

Methods

A method in Java is a subroutine that is part of a class. The subroutine is like a miniature program that can execute in other parts of the program. Methods promote code reuse and maintainability.

A method definition consists of the modifier, return type, name, parameter list, exception list, and body. The method name and parameter types form the method signature. The method signature uniquely identifies the method for execution.

Note: Java also has constructor methods. A constructor is a special method that creates an object of a class.

There are two types of methods: procedures and functions.

Procedures

A procedure is a method that does not have a return value. To define a method to be a procedure, define the return type to be void. An example of a built-in procedure in Java is System.out.println(). This procedure simply outputs its parameter to the console, without returning a value.

Functions

A function is a method that does have a return value. To define a method to be a function, set its return type to be the type of the value it is returning. An example of a built-in function in Java is Math.pow(). This Math function accepts two double parameters and returns the first parameter raised to the power of the second parameter. The return type is double.

Finally, user-defined methods are procedures and functions that you define. Since Java only provides general-purpose methods, it is up to you to define methods that make sense for your programming project.

If you liked this article proceed to my Twitter profile @realEdwinTorres and follow me! 😊

Top comments (1)

Collapse
 
rvsweb profile image
Raul Vela

Thank you for the explanation, it has helped me a lot to understand the differences between the defined terms.
Thank you for sharing your knowledge

Great read:

Is it Time to go Back to the Monolith?

History repeats itself. Everything old is new again and I’ve been around long enough to see ideas discarded, rediscovered and return triumphantly to overtake the fad. In recent years SQL has made a tremendous comeback from the dead. We love relational databases all over again. I think the Monolith will have its space odyssey moment again. Microservices and serverless are trends pushed by the cloud vendors, designed to sell us more cloud computing resources.

Microservices make very little sense financially for most use cases. Yes, they can ramp down. But when they scale up, they pay the costs in dividends. The increased observability costs alone line the pockets of the “big cloud” vendors.