Week 5: Methods and Functions in Java CSC 211 PDF

Summary

This document is lecture notes on methods and functions in Java for CSC 211. It covers topics such as the definition of methods, the syntax of methods, parameter handling, example, return types, operators, method overloading, recursion, and advantages and disadvantages of recursion.

Full Transcript

Week 5: Methods and Functions in Java CSC 211 Introduction to Methods What is a Method? A method is a block of code that performs a specific task. Methods help organize and reuse code efficiently. In Java, every method belongs to a class. A program’s logic is implemented u...

Week 5: Methods and Functions in Java CSC 211 Introduction to Methods What is a Method? A method is a block of code that performs a specific task. Methods help organize and reuse code efficiently. In Java, every method belongs to a class. A program’s logic is implemented using methods. Syntax of a Method: public class exampleEight { public static void main(String[] args) // Method returnType methodName(parameters) { // method body } } returnType: The data type of the value the method returns (e.g., int, void if nothing is returned). methodName: Name of the method. Should be descriptive of its function. parameters: Input values passed to the method (optional). These are enclosed in parentheses. Defining and Calling Methods Defining a Method: public class exampleEight { public static int addNumbers(int a, int b) { int sum = a + b; return sum; } } Calling a Method: Syntax int result = addNumbers(5, 10); // method call System.out.println("Sum: " + result); Method Parameters and Return Types Parameters Parameters: Values passed to a method when it is called. Example public static void greetUser(String name) { System.out.println("Hello, " + name); } Return Types: Return Types: Specifies what the method will return after execution. If the method does not return anything, the return type is void. Operators public static int multiply(int a, int b) { return a * b; } Method Overloading Method Overloading: Defining multiple methods with the same name but different parameter lists. Overloaded methods have different types or numbers of parameters. Example public static int add(int a, int b) { return a + b; } public static double add(double a, double b) { return a + b; } The correct method is chosen by the compiler based on the arguments passed when calling the method. Recursion What is Recursion? Recursion is when a method calls itself to solve a problem. It breaks down a problem into smaller instances of the same problem. Base Case and Recursive Case Base Case: The condition under which the recursive function stops calling itself. Recursive Case: The part of the function where the recursion happens. Example public static int factorial(int n) { if (n == 1) { return 1; // base case } else { return n * factorial(n - 1); // recursive case } } The method factorial keeps calling itself with a smaller number (n-1) until n equals 1 (the base case). Advantages and Disadvantages of Recursion: Advantages: Reduces complex problems into simpler sub-problems. Can be more elegant and easier to understand for problems that have a recursive nature (e.g., tree traversal, factorial, etc.). Advantages and Disadvantages of Recursion: Advantages: Reduces complex problems into simpler sub-problems. Can be more elegant and easier to understand for problems that have a recursive nature (e.g., tree traversal, factorial, etc.). Disadvantages of Recursion: Disadvantages: Recursion can be less efficient than iterative solutions due to the overhead of multiple function calls. If not designed carefully, it can lead to stack overflow errors if the recursion depth becomes too large. Practical Examples and Exercises Live Coding with Students System.out.println(“THANK YOU FOR ATTENDING !");

Use Quizgecko on...
Browser
Browser