Chapter 6:Understanding Java Functions: Syntax, Parameters, and Call by Value with Examples
Table of contents
- Chapter 6: Mastering Java Functions/Methods
- 1. Introduction to Functions/Methods
- 4. Parameters vs. Arguments
- Parameters:
- Arguments:
- 5. Memory Allocation in Functions
- Code Example: Swapping Two Numbers
- Visualizing Stack Frames:
- Explanation:
- Output:
- 6. Call by Value vs Call by Reference
- Example: Swap Two Numbers (Call by Value)
- Visual Explanation: Call by Value
- Table: Call by Value vs Call by Reference
- Additional Notes
- 7. Factorial of a Number
- 8. Function Overloading
- 9. Prime Number Check
- Conclusion
Chapter 6: Mastering Java Functions/Methods
1. Introduction to Functions/Methods
Functions (also known as methods in Java) are blocks of code designed to perform a particular task. Imagine using a TV remote to adjust the volume or change channels. You don’t write the logic to increase or decrease the volume each time; instead, you press a button and the remote performs the task for you. This concept is similar to functions in programming. The logic for each button is predefined, and when pressed, the same logic is executed.
Similarly, in programming, functions are reusable blocks of code that can be invoked multiple times without rewriting the same logic. This not only saves time but also makes the code more organized and efficient.
Key Benefits:
Reusability: Write once, use multiple times.
Modularity: Breaks complex problems into smaller, manageable pieces.
Maintainability: Easy to update the function without touching the main logic.
2) Syntax of Function and Explanation
In Java, functions (also called methods) are a block of code that performs a specific task. The basic syntax of a function is:
returnType functionName(parameterList) {
// Body of the function
return value; // This is optional if the return type is void.
}
Return Type: Specifies the data type of the value the function will return. If the function does not return any value, it uses
void
.Function Name: Identifies the function, typically in camelCase.
Parameter List: Variables passed to the function. These are optional and can be empty if no input is required.
Function Body: The code to be executed when the function is called.
Return Statement: If the function has a return type other than
void
, it must return a value of that type.
Analyzing the Main Function
In Java, the program execution always starts with the main
function. Here's the basic structure:
public static void main(String[] args) {
// Code to be executed
}
public: Accessible from anywhere.
static: Allows the method to be called without creating an instance of the class.
void: The
main
function doesn't return any value.String[] args: Accepts command-line arguments (not mandatory to use in every program).
Example: Function to Print "Hello World"
public class PrintHW {
// Function to print "Hello World"
public static void PrintHW() {
System.out.println("Hello World");
return; // No return value because the function is void
}
public static void main(String[] args) {
// Function call inside the main function
PrintHW(); // This calls the PrintHW function
}
}
void: The function
PrintHW()
doesn’t return any value. That's why we usevoid
.Function Call: In the
main
method, we call thePrintHW()
function to execute it. The execution starts from themain
method, and we explicitly call any other function from there.
Output:
Hello World
Functions vs Methods
In Java, functions are called methods when defined inside a class. So, function and method refer to the same thing in Java.
3) Syntax of Function with Parameters
A function can accept parameters (also called arguments) to operate on. The syntax for a function with parameters is:
returnType functionName(dataType parameter1, dataType parameter2, ...) {
// Body of the function
return value; // Optional if the return type is void.
}
- Parameters (Formal Parameters): These are placeholders in the function that accept values when the function is called. For example,
int a
andint b
in the example below.
Example: Function to Add Two Numbers
import java.util.Scanner;
public class Sum {
// Function with two parameters: a and b
public static int Sum(int a, int b) {
int sum = a + b;
return sum; // The sum is returned as the output
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); // Input scanner for user input
System.out.print("Enter a: ");
int a = sc.nextInt(); // First number (actual parameter)
System.out.print("Enter b: ");
int b = sc.nextInt(); // Second number (actual parameter)
// Calling the function Sum with arguments a and b
System.out.println("Sum of a and b is: " + Sum(a, b));
}
}
Explanation:
Function Declaration:
int Sum(int a, int b): The function
Sum()
accepts two parametersa
andb
. These parameters are used inside the function to perform the addition.return sum: Since the return type of the function is
int
, the result of the addition (sum
) is returned from the function.
Formal Parameters: In
public static int Sum(int a, int b)
,a
andb
are formal parameters because they are placeholders used within the function.Actual Parameters: In
Sum(a, b);
, the values provided by the user (a
andb
) are actual parameters (also called arguments). These are the values passed to the function during the function call.Multiple Functions: A class can have multiple functions, each serving different purposes. Here, the class
Sum
contains both theSum()
function and themain()
function.
Parameters:
int a
,int b
are called parameters (or formal parameters). They are placeholders that hold the values passed when the function is called.Arguments (Actual Parameters): When we call
Sum(a, b)
, we pass the actual values fora
andb
that are provided by the user.
Output:
Enter a: 5
Enter b: 7
Sum of a and b is: 12
Key Points:
A class can have multiple functions, each performing different tasks.
Parameters allow you to pass data to the function.
Return type specifies what kind of value the function will return (or no return in the case of
void
).
By using functions with parameters, you can pass different inputs and get the desired result based on those inputs.
4. Parameters vs. Arguments
Parameters:
Defined in the function signature.
Act as placeholders for the actual values.
Arguments:
- Actual values passed to the function during the function call.
5. Memory Allocation in Functions
When a function is called, the Java runtime allocates memory for its parameters and local variables on the call stack. Each time a method is invoked, it creates a stack frame. This stack frame contains:
Parameters passed to the method.
Local variables declared inside the method.
Return address, which points to the next instruction after the method returns.
Once the method completes execution, its stack frame is removed from the call stack, freeing the memory.
Let’s visualize the memory allocation for the following example:
Code Example: Swapping Two Numbers
public class Swap {
public static void swap(int a, int b) {
int temp = a;
a = b;
b = temp;
System.out.println("Inside swap: a = " + a + ", b = " + b);
}
public static void main(String[] args) {
int a = 5;
int b = 6;
swap(a, b);
System.out.println("After swap: a = " + a + ", b = " + b);
}
}
Visualizing Stack Frames:
- Before calling
swap()
:
Only themain()
method's stack frame is active. It holds the variablesa
andb
.
|-------------------------|
| [Main] Stack Frame |
|-------------------------|
| a = 5, b = 6 |
|-------------------------|
- When
swap()
is called:
A new stack frame forswap()
is created on top of themain()
stack frame. It holds the parametersa
,b
, and the local variabletemp
.
|-------------------------|
| [swap] Stack Frame |
|-------------------------|
| a = 5, b = 6 |
| temp = 5 |
|-------------------------|
| [Main] Stack Frame |
|-------------------------|
| a = 5, b = 6 |
|-------------------------|
During execution of
swap()
:The values of
a
andb
in theswap()
stack frame are updated after the swap logic.The
temp
variable stores the value ofa
(5), anda
is updated tob
(6). Then,b
is updated totemp
(5).
|-------------------------|
| [swap] Stack Frame |
|-------------------------|
| a = 6, b = 5 |
| temp = 5 |
|-------------------------|
| [Main] Stack Frame |
|-------------------------|
| a = 5, b = 6 |
|-------------------------|
- After
swap()
returns:
Theswap()
method completes its execution, and its stack frame is removed from the call stack. Themain()
stack frame remains unchanged, meaning the values ofa
andb
inmain()
are still5
and6
, respectively.
|-------------------------|
| [Main] Stack Frame |
|-------------------------|
| a = 5, b = 6 |
|-------------------------|
Explanation:
Call by Value: In Java, when the
swap()
method is called, a copy of the values ofa
andb
frommain()
is passed to theswap()
function. Changes made inside theswap()
function only affect the copies ofa
andb
in theswap()
stack frame.The original values of
a
andb
inmain()
remain unchanged because Java uses call by value.The swap logic works inside
swap()
, but it does not affect the values inmain()
after the method returns.
Output:
Inside swap: a = 6, b = 5
After swap: a = 5, b = 6
The output shows that the variables were swapped inside the swap()
method, but after returning to main()
, the original values remain unchanged.
6. Call by Value vs Call by Reference
Java is a language that uses call by value. This means that when you pass arguments to a method, Java passes a copy of the value of the argument. As a result, changes made to the parameter inside the method do not affect the original value in the calling code.
Key Concepts:
Call by Value: The method receives a copy of the argument's value, not the actual reference to the variable.
Call by Reference: The method receives a reference to the original variable, so changes to the parameter affect the original variable.
Since Java is strictly call by value, even though you cannot directly implement call by reference, you can mimic this behavior by using object references.
Example: Swap Two Numbers (Call by Value)
In this example, we attempt to swap two numbers using a method:
public class Swap {
public static void swap(int a, int b) {
int temp = a;
a = b;
b = temp;
System.out.println("Inside swap: a = " + a + ", b = " + b);
}
public static void main(String[] args) {
int a = 5;
int b = 6;
swap(a, b);
System.out.println("After swap: a = " + a + ", b = " + b);
}
}
Output:
Inside swap: a = 6, b = 5
After swap: a = 5, b = 6
Explanation:
Inside swap:
The
swap
method receives copies of the values ofa
andb
.It swaps the values of these copies and prints them.
The output shows that inside the method, the values have been swapped.
After swap:
The values of
a
andb
in themain
method remain unchanged.This is because Java passed only copies of the values, not references to the original variables.
Visual Explanation: Call by Value
Here's a visual representation of call by value:
Initial Values:
a = 5
b = 6
Method Call:
swap(a, b)
is called.Copies of
a
andb
are passed to theswap
method.
Inside swap():
a = 6
(copy of originalb
)b = 5
(copy of originala
)
Back to main():
- The original
a
andb
remain unchanged.
- The original
State | a (main) | b (main) | a (swap) | b (swap) |
Before swap call | 5 | 6 | - | - |
Inside swap() | 5 | 6 | 6 | 5 |
After swap call | 5 | 6 | - | - |
Table: Call by Value vs Call by Reference
Feature | Call by Value | Call by Reference |
Definition | Passes a copy of the value of the argument. | Passes a reference to the original variable. |
Effect on Original | Changes in the method do not affect the original variable. | Changes in the method affect the original variable. |
Java | Java uses call by value. | Not applicable; Java does not support call by reference. |
Example | Swapping two integers using call by value. | Not directly supported in Java. |
Additional Notes
Call by Value is ideal for immutable types like primitives where you don't want to affect the original data.
Call by Reference can be mimicked in Java using object references, where changes to object fields will affect the original object.
This comprehensive explanation covers the concept of call by value in Java and its implications, along with a practical example and visual representation.
7. Factorial of a Number
The factorial of a number ( n ) is the product of all positive integers from 1 to ( n ). It is denoted as ( n! ).
- Mathematically:
( n! = n* (n-1)* (n-2) …..* 1 )
For example:
( 4! = 4 * 3* 2 *1 = 24 )
Factorials only exist for positive numbers.
Code: Factorial Calculation
Below is the Java code to calculate the factorial of a number ( n ):
public class Fact {
public static int fact(int n) {
int f = 1;
for (int i = 1; i <= n; i++) {
f = f * i;
}
return f;
}
public static void main(String[] args) {
System.out.println(fact(2)); // Example: Factorial of 2
}
}
How the Code Works:
fact method:
f = 1
: This is where the factorial result is stored.The
for
loop runs from 1 ton
and multipliesf
with the loop variablei
to get the factorial.Finally, the method returns the computed factorial.
Main method:
- Calls
fact(2)
to compute the factorial of 2 and prints the result.
- Calls
Dry Run: Factorial of 2
Dry run refers to a step-by-step walkthrough of the code execution. Let's dry-run the given code for ( n = 2 ).
Step | Variable | Value | Explanation |
1 | n | 2 | Input is 2. |
2 | f | 1 | Initialize f to 1. |
3 | i | 1 | First iteration (i = 1 ). f = f * i = 1 * 1 = 1 |
4 | i | 2 | Second iteration (i = 2 ). f = f * i = 1 * 2 = 2 |
5 | i | - | Loop ends because i exceeds n . |
6 | Return | 2 | Return f , which is 2. |
7 | Output | 2 | The factorial of 2 is printed: 2. |
Output:
2
The factorial of ( 2 ) is correctly computed as ( 2! = 2 ).
8. Function Overloading
Function overloading is the ability to define multiple functions with the same name but different parameter lists.
Example: Function Overloading by Parameters
public class Funct_over {
public static int Sum(int a, int b) {
return a + b;
}
public static int Sum(int a, int b, int c) {
return a + b + c;
}
public static void main(String[] args) {
System.out.println(Sum(4, 8)); // Calls the 2-parameter function
System.out.println(Sum(5, 7, 8)); // Calls the 3-parameter function
}
}
Output:
12
20
9. Prime Number Check
A prime number is a number that is divisible only by 1 and itself.
Example: Check if a Number is Prime
public class PrimeCheck {
public static boolean isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i < n; i++) {
if (n % i == 0) return false;
}
return true;
}
public static void main(String[] args) {
System.out.println("Is 7 prime? " + isPrime(7));
}
}
Output:
Is 7 prime? true
Conclusion
Functions (or methods) are the building blocks of reusable and maintainable code. Understanding the syntax, parameters, memory management, and advanced topics like function overloading and recursion is key to mastering Java programming.
By mastering these concepts, you can write efficient, organized, and scalable code in Java. Keep practicing these concepts with various real-world problems to solidify your understanding.
By mastering functions, you’re taking a significant step toward writing clean, maintainable, and efficient Java programs. Stay tuned for more chapters in my Java with DSA series on Hashnode!
Feel free to share this post and leave your feedback!
Related Content:
Connect:
Let’s discuss more about Java and coding! Feel free to leave a comment below or connect with me on LinkedIn.
Thank you for taking the time to read this post! Your interest and feedback help improve the content and make learning more engaging for everyone.