Greetings! Today, we will venture into the universe of functions and procedures in Java, which are vital tools in the programmer's toolbox - they introduce reusable components that can be defined once and used everywhere. Functions execute tasks and return a value, whereas procedures perform tasks without returning anything. This lesson will guide you through writing Java functions and procedures.
Functions and procedures are similar to recipes. They consolidate instructions to carry out specific tasks. Imagine baking a cake — a procedure — and sharing it — a function. Baking doesn't yield anything, so it's a procedure. However, sharing returns the remaining cakes, making it a function.
The structure of functions and procedures in Java is as follows:
At first, this structure may seem strange, but think of it as a recipe. static
is just a keyword you have to remember for now. returnType
is the type of dish you'll get after cooking. functionName
is the title of the recipe. The parameters
are the ingredients, with the {}
brackets containing the cooking method - the recipe itself.
With those explanations in mind, let's craft some functions and procedures.
