Functions in JAVA | Tamil Explanation with coding Examples ✅

Опубликовано: 22 Февраль 2026
на канале: siya_tech_tales
107
7

Follow us on Instagram: https://www.instagram.com/siyatechtal...
--------------------------------------------
A function in programming is like a recipe in the kitchen. When you want to make a dish, you follow the steps outlined in the recipe. Similarly, when you want to perform a task in programming, you write a function with specific steps. Once you’ve written the recipe (function), you can reuse it whenever you want to make the dish (perform the task), without writing the steps again.

Step-by-step Explanation:

✅What is a function?
Think of a function as a recipe. Just like a recipe is a set of instructions to make a dish, a function is a set of instructions to perform a task in Java. Once the function is written, you can reuse it as many times as you want.
Declaring a Function:

You start by defining what the recipe (function) does. This is done using a name and specifying any ingredients (parameters) it needs. If the recipe produces something (a result), you also need to mention what kind of dish (return type) it makes.

Syntax:
returnType functionName(parameters) {
// Instructions
}
Example: A function that adds two numbers together.

int addNumbers(int num1, int num2) {
int sum = num1 + num2;
return sum;
}
Explanation:


int is the return type (the kind of result the function gives, here an integer).
addNumbers is the function name (recipe name).
int num1, int num2 are the parameters (ingredients for the recipe).
Inside the function, sum = num1 + num2; is where we follow the steps to get the result.
Calling a Function:

Analogy: Calling a function is like asking someone to follow a recipe. You provide the ingredients (arguments) and the person (function) follows the recipe and gives you the result.

Example:

java
Copy code
int result = addNumbers(5, 3); // Calling the function
System.out.println(result); // Output will be 8
Why Use Functions?:

Reusability: Write the recipe once, use it many times.
Organization: Break big tasks into smaller, manageable pieces.
Readability: Makes the code cleaner and easier to understand.
Example Problem: Create a Simple Greeting Function
Problem: Write a function that greets a user by their name.

Function Declaration:

java
Copy code
void greetUser(String name) {
System.out.println("Hello, " + name + "!");
}

Explanation:
void means the function does not return anything.
greetUser is the name of the function.
String name is the parameter (the name of the person to greet).
Inside the function, we print a greeting message.
Function Call:

java
Copy code
greetUser("Alice"); // This will print: Hello, Alice!
greetUser("Bob"); // This will print: Hello, Bob!
Example Problem to Solve Using Functions:
Problem: Write a function to calculate the square of a number.

Function Declaration:

java
Copy code
int square(int number) {
return number * number;
}

Explanation:
int square(int number) defines a function that takes an integer (number) and returns the square of it.
The function body calculates the square by multiplying the number by itself and returning the result.

Solution with Function Call:
public class Main {
public static void main(String[] args) {
int result = square(4);
System.out.println("The square of 4 is: " + result);
}

static int square(int number) {
return number * number;
}
}
Output:
The square of 4 is: 16
This approach keeps things simple and relatable, making it easier for beginners to grasp functions.
--------------------------------------------
Copyright Disclaimer under Section 52 of the Copyright Act, 1957 (India):
This video is made for educational, informational, and entertainment purposes only. The content is transformative in nature, and its use is considered fair use under Indian copyright law. No copyright infringement is intended, and all rights belong to their respective owners. The purpose of this video is to educate, inform, and entertain, and it is not intended to harm or exploit any individual or entity. If you have any concerns or objections, please contact us at [email protected]

#functions
#javacodinginterview
#java