Lesson 1
Function Fundamentals Revision
Java Functions

Welcome to the first course in the "Functional Programming in Java" path! Get ready to revisit the basics of functions, and then dive into exciting topics like functional interfaces, the powerful Streams API, and more—it's going to be a great journey toward mastering functional programming in Java!

Functions, or methods, are the building blocks of any Java program. They encapsulate code, making it reusable, modular, and easier to manage. In this section, we'll revisit the foundational concepts of functions in Java, including their syntax, arguments, and return types. We'll also delve into the details of essential keywords used in function declarations.

The Entry Point of a Java Program: main Method

Every Java application must have a main method, which serves as the entry point for the program. It is where the program starts executing.

Java
1public class Main { 2 3 public static void main(String[] args) { 4 // Your code here 5 } 6}

Let’s break down the main method:

  • public: This keyword specifies that the method is accessible from outside its defining class. It is the most open access level in Java.
  • static: Indicates that the method belongs to the class, not instances of the class. This means you can call the method without creating an object of the class.
  • void: The return type, indicating that the method does not return any value. If the function were to return something, this keyword would be replaced with the appropriate type (e.g., int, String).
  • main: This is the name of the method, and it is a special method recognized by the Java runtime as the entry point of the application.
  • String[] args: This is an array of String arguments passed to the main method. It allows for command-line arguments to be passed to the program.
Function Syntax and Structure
Java
1public class Main { 2 3 // A basic function in Java 4 public static void sayHello() { 5 System.out.println("Hello, world!"); 6 } 7 8 public static void main(String[] args) { 9 // Calling the function 10 sayHello(); // Output: Hello, world! 11 } 12}

In Java, a function is defined by specifying its return type, name, and any parameters it may take. Let’s break down the sayHello function:

  • Return Type: The type of value the function returns. void means the function does not return any value.
  • Function Name: The name of the function, which in this example is sayHello.
  • Parameters: Input values the function can accept. This function doesn't take any parameters.
  • Function Body: The block of code that runs when the function is called. Here, it prints "Hello, world!" to the console.
Functions with Arguments
Java
1public class Main { 2 3 // Function that takes an argument 4 public static void greet(String name) { 5 System.out.println("Hello, " + name + "!"); 6 } 7 8 public static void main(String[] args) { 9 // Calling the function with an argument 10 greet("Alice"); // Output: Hello, Alice! 11 } 12}

Arguments allow you to pass data into functions. The greet function takes a String argument named name:

  • Parameter: name is the parameter that the function accepts.
  • Function Overloading: Functions can also be overloaded by defining multiple functions with the same name but different parameter lists. This will be explored in a later unit.

The function can generate different outputs depending on the argument passed when it is called.

Function Return Types
Java
1public class Main { 2 3 // Function that returns a value 4 public static int add(int a, int b) { 5 return a + b; 6 } 7 8 // Function that doesn't return a value 9 public static void displaySum(int sum) { 10 System.out.println("Sum: " + sum); 11 } 12 13 public static void main(String[] args) { 14 // Calling the function and storing the result 15 int sum = add(5, 10); 16 // Calling the function that doesn't return a value 17 displaySum(sum); // Output: Sum: 15 18 } 19}

Functions can return values, which can then be used elsewhere in your code. In the add function, the return type is int, meaning the function must return an integer value. Here are a few important points:

  • Return Type Matching: The return type declared in the method signature (int in this case) must match the type of the value returned by the method.
  • Return Statements: Use the return keyword to return a value from a function. Functions without a return value use the void keyword, and they do not require a return statement.
  • Compilation Requirement: If a function’s return type is not void, it must return a value on every possible execution path. Failing to do so will result in a compilation error.
Why It Matters

Mastering the basics of Java functions is crucial for several reasons, including:

  1. Code Organization: Functions help you structure your code into manageable, logical sections.
  2. Reusability: Functions can be called multiple times, reducing code duplication.
  3. Maintainability: Well-organized functions make your code easier to read, understand, and modify.

With these basics in place, you are well-prepared to delve into more advanced Java programming concepts. Understanding functions is the foundation upon which much of your future Java knowledge will be built. Let's get ready to put these concepts into practice!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.