Skip to content

Commit 50e66a1

Browse files
committed
feat: Add FunctionsCC5 class with calculateSum and return type example
WHAT the code does: - Defines a `FunctionsCC5` class with a static method `calculateSum(int num1, int num2)`. - `calculateSum()` computes and **returns** the sum of two integers. - `main()` calls the method with arguments `(3, 4)` and prints the result → `7`. WHY this matters: - Demonstrates the importance of **return types** in Java methods. - Shows that return types can be changed (e.g., `int`, `double`, `String`) depending on desired output. - Reinforces declaring return types explicitly for type safety. HOW it works: 1. `calculateSum(3, 4)` → computes `3 + 4 = 7`. 2. Returns `7` to the caller. 3. `main()` prints the result. Tips & gotchas: - Return type must match the actual returned value → otherwise, compile-time error. - Method only handles two integers → could extend with overloading or varargs for flexibility. - If changed to `double`, division or floating-point sums could be supported. - Comments highlight importance of specifying return type correctly. Use-cases: - Educational example for **methods with return values and type declarations**. - Base for calculator functions. - Useful in teaching type safety and compiler enforcement in Java. - Can be extended for sums involving arrays or multiple numbers. Short key: class-functions cc5-return type-example. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 0ad057f commit 50e66a1

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
//Specifying the return type is important.
2-
3-
41
public class FunctionsCC5 {
5-
public static int /*return type we can change it into any type.*/ calculateSum (int num1, int num2)
6-
{
2+
public static int calculateSum (int num1, int num2)
3+
/*return type we can change it into any type.*/ {
74
return num1 + num2;
5+
//Specifying the return type is important.
86
}
97
public static void main (String[] Args){
108
System.out.println(calculateSum(3, 4));
119
}
12-
}
10+
}

0 commit comments

Comments
 (0)