Lesson 1

Defining and Executing Java Functions and Procedures

Topic Overview and Actualization

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.

Understanding Java Syntax for 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:

1static returnType functionName(parameters) { 2 // Code body 3}

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.

Writing Our Own Java Function

With those explanations in mind, let's craft some functions and procedures.

1static int addTwoNumbers(int num1, int num2) { 2 int sum = num1 + num2; 3 return sum; // returns the sum of num1 and num2 4}

This function is named addTwoNumbers, takes two int numbers as parameters, and returns an int too. Inside the function, it adds two input parameters and returns their sum as the result of the function.

The return statement is akin to the chef announcing that the dish is ready. It notifies the program that the function has accomplished its task and produced a result, finishing the function's execution after that. In the addTwoNumbers function, return sum; indicates that the operation has concluded, and the sum of num1 and num2 is the result. You can't execute any other code or statements after return.

Writing Our Own Java Procedure

Next, we'll create a procedure that prints a greeting:

1static void printGreeting(String name) { 2 System.out.println("Hello "+ name + ", welcome to Java!"); 3}

The procedure is the same function, but with return type void - meaning "no return". As you can see, this procedure just prints a simple greeting message to the console and doesn't return anything as a result.

Executing Java Functions and Procedures

To employ these functions and procedures, we call them from our main() function:

1public static void main(String[] args) { 2 int result = addTwoNumbers(5, 7); // the function is called with 5 and 7 as arguments 3 System.out.println("The sum is: " + result); // outputs: The sum is: 12 4 int result2 = addTwoNumbers(10, 20); // just calling with other arguments 5 System.out.println("Another sum is: " + result2); // outputs: Another sum is: 30 6 7 printGreeting("Explorer"); // Outputs: Hello Explorer, welcome to Java! 8}

Both addTwoNumbers(5, 7) and addTwoNumbers(10, 20) call the function with two numbers as inputs, while printGreeting("Explorer") calls the procedure with "Explorer" as the argument. See how easy and reusable functions and procedures are? No need to duplicate the code anymore!

Lesson Summary and Practice

Splendid work, programmer! You've learned about and practiced writing functions and procedures in Java, a key skill for your journey in programming. Now, our forthcoming exercises will help you implement these concepts. Let's plunge right in and keep practicing — remember, practice makes perfect!

Enjoy this lesson? Now it's time to practice with Cosmo!

Practice is how you turn knowledge into actual skills.