What is user-defined function ? Explain with Example.
In : BSc IT Subject : Structure Programming Methodology - JavaA user-defined function in Java is a block of code that you, the programmer, write to perform a specific task. Unlike built-in functions (like `System.out.println()`), user-defined functions are created by you and can be used again and again in your program. You define them using a name, a return type, and optionally parameters.
Example:
public class MyProgram {
// User-defined function
public static void greet() {
System.out.println("Hello, welcome to Java!");
}
public static void main(String[] args) {
greet(); // Calling the user-defined function
}
}
In this example, `greet()` is a user-defined function that prints a message. We call it inside the `main` method to use its functionality. This helps avoid repeating code and makes your program organized and easier to manage.