logo codesdope

Get values from method


In this section, we will learn to write methods which can return something.
First see an example:

class Add{
  public static int add_int(int x,int y){
    return x+y;
  }
  public static void main(String[] args){
    int z;
    z = add_int(2,4);
    System.out.println(z);
  }
}
Output
6

add_int(int x,int y) - This part of code should be clear that 'add_int' is the name of method and it is taking two parameters of type int.
int add_int(int x,int y) - 'int' before the method name means that this method will return an integer. i.e. we will get some integer value whenever we will call this method.
return x+y; - This part returns an integer having the value 'x+y' since our method has to return an integer.

Now come to the main method
int z = add_int(x+y); - We are calling 'add_int' method by passing two integers 2 and 4. And this will return 'x+y' i.e. '2+4'. So, this statement is equivalent to:
int z = 2+4; or int z = 6; (6) returned by 'add_int(2,4)'.

Now let's see an example:

class Area{
  public static double getArea(double x,double y){
    return x*y;
  }
  public static void main(String[] args){
    double z = getArea(10.2,23.4);
    System.out.println(z);
  }
}
Output
238.67999999999998

This method is returning 'double'( x*y )'. And the main method statement is equivalent to:
double z = 10.2*23.4; or double z = 238.67999999999998;

Calling a method inside another


Yes, we can call a method inside another method. We have already done this. We were calling our functions inside the main function. Now look at an example in which there are two user-defined functions. And we will call one inside another.

class Div{

public static int div_2(int a){
  if(a%2==0){
    return 1;
  }
  else{
    return 0;
  }
}

public static void div_6(int b){
  if( div_2(b)==1 && b%3 == 0 ){
    System.out.print("Yes, the number is divisible by 6.\n");
  }
  else{
    System.out.print("No, the number is not divisible by 6.\n");
  }
}

public static void main(String[] args){
  div_6(12);
  div_6(25);
}
}
Output
Yes, the number is divisible by 6.
No, the number is not divisible by 6.

A number is divisible by 6 if it is divisible by both 2 and 3. We have a method div_2 which will return 1 if the given number is divisible by 2. Another method that we have defined is dev_6 which calls div_2 inside itself.
if( div_2(b)==1 && b%3 == 0 ) - So if div_2 will returns 1, means that the number is divisible by 2. And if b%3==0 is true, it means that 'b' is divisible by 3. So, if it is divisible by both 2 and 3, then it is divisible by 6 also.

Recursion


Recursion is the calling of a method within the same method.

Let's consider an example in which we have to calculate the factorial of a number.

Factorial of a number n is the product of all the natural numbers till n. For example, the factorial of 5 is 1*2*3*4*5 = 120. Factorial of 0 and 1 is 1.

import java.util.*;
class Fact{

public static int factorial( int a ) /* method */
{
  if( a == 0 || a == 1)
  {
    return 1;
  }
  else
  {
    return a*factorial(a-1);
  }
}

public static void main(String[] args) 
{
  Scanner s = new Scanner(System.in);
  int n; 

  System.out.print("Enter number\n");
  n = s.nextInt();

  int fact = factorial(n);

  System.out.println("Factorial of "+n+" is "+fact);
}
}
Output
Enter number
4
Factorial of 4 is 24

Here, if the integer 'n' is 0 or 1, return 1; returns the factorial value as 1. Otherwise if the integer 'n' is greater than 1, then n*factoria;(n-1) multiplies n with the factorial of 'n-1'. For example, if the number is 4, then 'n*factorial(n-1)' implies '4*factorial(3)'. So by writing 'n*factorial(n-1)', we are again calling the method 'factorial' inside itself but this time the argument is 'n-1' i.e. factorial(n-1).

So, first give 0 to the method factorial, it will give us 1. Again give 1, it will return 1 again.

On giving 2, the condition of if becomes false and hence the body of else gets executed and thus 'n*factorial(n-1)' i.e. 2*factorial(1) gets returned. This again calls 'factorial' ( as factorial(1) ) which returns 1. Thus, the result is 2*factorial(1) = 2*1 i.e. 2.

Now, try this with 3. This time it has to return 3*factorial(2). Again it calls factorial(2) and factorial(2) returns 2*factorial(1). So, the final expression returns 3*2*factorial(1), which is 3*2*1 ( as factorial(1) will return 1 ).

For 4, the returning expression is 4*factorial(3).
Calling factorial(3) will give us 4*3*factorial(2) ( as factorial(3) will return 3*factorial(2) ).
Again calling factorial(2), the final expression will be 4*3*2*factorial(1).
Thus, at last, the method will return 4*3*2*1 or 24.

Now after this, you need to practice to get your concepts more strong and clearer in programming.

Programming is a skill best acquired by practice and example rather than from books.
-Alan Turing

Share what you know
Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.