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

ยท

2 min read

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! ๐Ÿ˜Š

ย