Beginner Functions - 2
Beginner Functions - 2
Agenda
1. Function Examples
2. Given N return sum of all even number
3. Given radius find area of circle
4. Even or odd
5. Given M check if it is a perfect square
Problem 1
Example 1
N = 10
Solution
N = 10
adding 2 + 4 + 6 + 8 + 10 = 30
Example 2
N = 5
Solution
N = 5
2 -> 4
adding 2 + 4 = 6
Approach
Code
static int evenSum(int N){
int sum = 0;
for(int i = 2 ;i <= N ; i += 2){
sum += i;
}
return sum;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
int sum = evenSum(N);
System.out.println(sum);
}
Dry run
N = 7
1 2 Yes 0+2 = 2 4
2 4 Yes 2+4 = 6 6
3 6 Yes 6+6 = 12 8
4 8 No
Problem 2
Approach
Code
Function Rules
Choices
Hi
Welcome
Home
Welcome
Home
Hi
Home
Welcome
Hi
Explaination
Question
What will be the output?
Choices
Hi
Hello
Hello
Hi
Error
Explaination
Some people might answer Error because of return statement in void type function But will it actually produce an error ?
Since return statement is not returning any thing it won't produce an error.
Question
What will be the output?
Choices
true
Hello
Hello
false
false
Hello
Explaination
We are calling isEven function with n = 60. So on entering the function we check for if n%2 == 0
Since 60 % 2 ==0 it enters if statement and returns true and isEven execution ends here
After that Hello is printed
Question
What will be the output?
Choices
Even
Hello
Odd Hello
Hello
Explaination
We are calling EvenOdd function with N = 10 So on entering function the we check for if N%2 == 0
Since 10 % 2 ==0 it enters if statement an prints Even and due to return statement EvenOdd execution ends.
After that Hello is printed.
Question
What will be the output?
Choices
15
Error
No output
Explaination
When we run the function we get an ERROR : Missing return statement in function
Note : If any function having return type other then void then should have atleast one return statement.
Question
What will br the output?
Explanation
When we run the function we get an ERROR : Missing return statement in function
This occured because compiler checks if the condition is false then is there any return statement. Since there is no return statement Error is thrown.
If you create a function and there is a mistake in it. It doesn't weather you call it or not you will get an error
If any function having return type other then void then should have return statements for all cases.
So According to rules if we add return statement after if we can resolve the error
Question
What will be the output?
Choices
5
Error
2
No output
Explaination
When we run the function we get an ERROR : Missing return statement in function
This occured because compiler checks if the first condition is false then is there any return statement.
Compiler checks if the second condition is false then is there any return statement.
Since there is no return statement Error is thrown.
Problem 3
Given N return true if the number is perfect square else return false?
N is said to be a perfect square if it can be expresses as product of two equal positive numbers.
Testcases
Idea
Starting from 1 (Smallest +ve number) check if there is any number p that satisfies the condition p*p == N
Example 1
Example 2
Observations
We can observe from above examples that :-
We need to iterate starting from i=1
We need iterate till i*i <= N
On each iteration we check if i*i == N if so then return true else we return false
Code
Problem 4
Testcases
Code
Choices
Error
30
Hey
Explanation
Here error is generated because of print command just after return in function sum
The code after return can not be executed still compiler sees valid statements(not commented lines) and hence throws unreachable statement error
Statements after return are not executed. If there are statements after return then compiler throws error --> [Statement Unreachable]
Question
What will be the output?
Choices
300
15
Compilation error
Explaination
We get answer 300 because value passed to function sum was 100 and 200 respectively(i.e. values of x and y of main function ) hence value of variables a
and b in function sum is set to 100 and 200 respectively and value of variables a and b in function sum is independent of the values of a and b defined in
function main
Therefore we can say that variables in a function are defined only in scope of the function
Question
What will be the output?
Choices
300
15
Compilation error
Explaination
We get answer 300 because value passed to function sum was 100 and 200 respectively(i.e. values of x and y of main function ) hence value of variables a
and b in function sum is set to 100 and 200 respectively and value of variables a and b in function sum is independent of the values of a and b defined in
function main
Also x and y defined in sum() & x and y defined in main() are seperate entities and changing the value of x and y in sum() won't affect the x and y defined
in main() .
Question
What will be the output?
Choices
50
300
Compilation error
Explaination
We get answer 50 because value passed to function sum was 100 and 200 respectively(i.e. values of x and y of main function ) hence value of variables a and
b in function sum is set to 100 and 200 respectively and value of variables a and b in function sum is independent of the values of a and b defined in function
main
But in sum() we set the value of a and b to 20 and 30 respectively since a and b are defined within scope of sum() there values are changed.
Question
What will be the output?
Choices
50
300
Error
Explaination
We get compilation error because we declared variables that are already defined in function sum()