Parameters or arguments in java

 Parameters:

  • It provides data communication between the calling function and the called function.
  • There are two types of parameters,
    (i). Actual parameters:
  • Used in data transferred from the main function to the user-defined function.
    (ii). Formal parameters:
  • Used in data transferred from a user-defined function into main function.
Local and global variables:
Local variable:
  • The local variables are defined within the body of the function.
  • This variable used within the function only, other functions can not access these variables.
Example:

int a=10,b=5;
if(a>b)
{
          int c=a+b;
}
else
{
        int c=a-b;
}

Global variable:
  • The global variables are defined outside the function or conditions ,these variables are only used in this particular part.
Example:

int x=5,y=10;
int z;
if(x<y)
{
         z=x+y;
}
else
{
         z=x-y;
}

Example program for local and global variables.

public class Parameters {

    int p=10,q=10;//Global variable

    int add(int x,int y)//Formal argument

    {

        return x+y;

    }

    void sub()

    {

        int r=p-q;//Local variable is r

        System.out.println("Subtraction value: "+r);

    }

    void mul()

    {

        int r=p*q;//Local variable is r

        System.out.println("Multiplications value: "+r);

    }

    public static void main (String args[])

    {

        int a=10,b=5;

        Parameters p=new Parameters();

        System.out.print("Result of a add b value is : " +p.add(a, b)+"\n");//Actual 

                                                                                                                 argument

        p.sub();

        p.mul();

       

    }

 

}

Output:
Share:

No comments:

Post a Comment

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me