Passing arguments to a function

When a function defined with parameters, then we should not call that function without passing the corresponding arguments to the corresponding parameters of that function.

 

Class Odemo A

{

Int   i, j;

Void   function1 ( int i)

{

i=i+1;

j=j+1;

System. Out. Prinln (i);  // i=7

System. Out. Prinln (j); // j=8

}

}

Class test

{

Public static void main (string args [ ])

O demo A d1 = new O demo A ();

d1. function1 (6);

System. Out. Prinln (d1.i); // i=0

System. Out. Prinln (d1.j); // j=8

}

}

 

Explanation:

The value of 6 is passed as an argument to function1 (). There int I is the parameter and it is a local variable to the function 1(). Now when the statement i=i+1 has executed the value of I is changed to 7 only inside the function. This will not affect the instance variable I inside the object. This is because as already mentioned I

This is because as already mentioned I is a local variable to the function1 () and remember local variables has had the highest priority. As we can define functions to accept primitive data types as arguments, we can define functions to accept an object of a class as an argument. This is illustrated in the following program.

Class Odemo A

{

Int   i , j;

Void   function1 ( int i)

{

System. Out. Prinln (i);

System. Out. Prinln (“inside function1()”);

}

Void   function2 ( A  a1) \\ call by reference or address

{

If (a1 ! = null)

{

A1 .i=203;

A1.j=115;

}

System. Out. Prinln (a1);

System. Out. Prinln (“ end of fun2()”);

}

Public static void main (string args [ ])

{

O demo A d1= new O demo A ();

A a2 = new A ();

d1. function2 (a2);

// d1. function2 (null); // we can pass null as an argument to a function

System. Out. Prinln (a2 .i);

System. Out. Prinln (“end main”);

}

}

 

Passing Arguments to a Function Homework Help