Functions

Suppose, you have to find factorial of two numbers. 

public class factorial
{
    public static void main()
    {
        int num=5,f=1;
        for(int i=1;i<=num;i++)
        f=f*i;
        System.out.println(“Factorial of “+num+” is “+f);


        int num1=6;f=1;
        for(int i=1;i<=num;i++)
        f=f*i;
        System.out.println(“Factorial of “+num1+” is “+f);
    }
}

As you see above, to find factorial of two numbers you have to write the same code two times. This increases the length of your program unnecessarily. This is when you should create a function for finding a factorial.

Functions are block of codes that performs specific tasks. you can use it as many times as you want.

Function header:

It is the first line, which contains access specifier, return type, function name, and a list of parameters.

  • Access specifier: It deals with the scope of the function.
  • Return type: It specifies what type of data is the function returning to its caller.
  • Function name: Your function name preferably should be related to the process.
  • Parameter list: It specifies what type of data and how much data is the caller giving to the function to process.

Function block:

Your function block should contain a set of statements for related operations. For instance:

  {
      int num=1;
      return num;
  }

How to use a function?

So here’s a function for addition:

public class Operation
{
    public int add(int a,int b,int c, int d)
    {
        return (a+b+c+d);
    }
    public static void main()
    {
        Operation ob=new Operation();
        System.out.println(“Answer1: “+ ob.add(4,5,6,7));
        System.out.println(“Answer2: “+ ob.add(8,9,10,11));
        System.out.println(“Answer3: “+ ob.add(12,13,14,15));
    }
}

You can clearly see how easy it is to use the function again and again. You just have to call it again and again (call through an object when called in static function). Here:

That is, parameters described in the function definition are ‘Formal Parameters’ and those described in the caller definition are ‘Actual Parameters’.

Return statement:

‘return’ statement is used in functions to return a value to the function caller. You can use a function to print something or return something. 

If it is not returning anything to its caller then you have to use void keyword, for instance:

public void factorial()

 If it is returning something then specify the data type of it, for instance:

 public int factorial().

How to use return statement?

  • It is used at the end of function because no statement can be executed after it:

public int sum (int a, int b)
{
    int s=a+b
    return s;
}

  • It can only return a single value. Hence, this is not allowed:

public int sum (int a, int b)
{
    int s=a+b;
    int p=a*b;
    return (s,p);    
}

  • While using ‘if- else’ in a function which returns a value, ‘return’ statement should be used in both conditions.

public int greater (int a, int b)
{   
    if(a>b)
    return a;
    else
    return b;
}

Can we use multiple function with same name?

Yes, you can. This is known as function overloading. But the number and types of parameters should be different. Example:

  • int add(int a,int b)
  • int add(int a,int b,int c)
  • int add(double a,int b)
  • double add (int a,int b)

Having all these functions with same name in the same class is valid.

Conclusion:

Now you know how to create a function and use it in your own program to return some value. Functions divide your programs into small chunks of logic. Having functions in your program definitely helps.