Skip to content

Commit 9b13f8e

Browse files
committed
feat: Add FunctionsCC2 class with calculateSum returning result
WHAT the code does: - Defines a `FunctionsCC2` class with a static method `calculateSum(int num1, int num2)`. - Unlike the previous version, this method **returns** the sum instead of printing it. - `main()` calls `calculateSum(3, 4)` and prints the returned result → `7`. WHY this matters: - Demonstrates the difference between methods that **return values** vs those that only print. - Returning values increases **reusability** since the result can be stored, reused, or combined with other logic. - Highlights better design for utility methods. HOW it works: 1. `calculateSum(3, 4)` computes `7`. 2. Method returns `7` to the caller. 3. `main()` prints the returned value. Tips & gotchas: - Returning instead of printing allows combining results in further calculations. - If used in multiple places, avoids redundant printing. - Works for integers only — overloading or using generics could extend flexibility. - For larger ranges or performance-critical cases, ensure integer overflow is considered. Use-cases: - Educational example for **methods with return values**. - Building block for calculator applications. - Utility function for number-crunching programs. - Serves as a comparison against `FunctionsCC` which prints directly. Short key: class-functions cc2-calculate sum-return. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 7990091 commit 9b13f8e

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

Section10Methods/PracticeProblems/src/FunctionsCC2.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ public class FunctionsCC2 {
22
public static int calculateSum(int num1, int num2){
33
return num1 + num2;
44
}
5-
public static void main(String[] args)
6-
{
5+
public static void main(String[] args) {
76
System.out.println(calculateSum(3, 4));
87
}
9-
}
8+
}

0 commit comments

Comments
 (0)