Chapter-8
Chapter-8
Actual parameters
Actual parameters are the values that are passed to a function when it is called. They are
also known as arguments.
When a function is called, the actual parameters are evaluated, and their values are copied
into the corresponding formal parameters of the called function.
Formal parameters
Formal parameters are the variables declared in the function header that are used to receive
the values of the actual parameters passed during function calls.
They are also known as function parameters.
Formal parameters are used to define the function signature and to specify the type and
number of arguments that a function can accept.
Example:
Formal Parameters
class Parameter
{
void m1(int i, int j)
{
System.out.println(i+j);
}
}
class Main
{
public static void main(String[] args)
{
Parameter p=new Parameter();
p.m1(20, 30);
}
} Actual Parameters
/* Output
* 50
*/
By:Ankur Patel
Topic: Passing Array to Function
Functions are used to break and divide a huge code into small chunks so that the code
becomes more understandable and thus reduces the complexity of the code. Arrays are the
homogenous data structures for reducing the code complexity, increasing efficiency, and
reducing the execution time of the code. We make use of the array and think of both these
time-saving parts of programming are implemented together. Thereby exists the concept
of Passing Array to Function.
In this section, we will learn how we can pass an array to a user-defined function and will
notice how it makes the code more optimistic as well as efficient.
Generally, the purpose of passing an array to a function is to transfer a large amount of data
between methods. To pass an array to a function, just pass the array as function's parameter
(as normal variables), and when we pass an array to a function as an argument, in actual
the address of the array in the memory is passed, which is the reference. Thus, any changes
in the array within the method will affect the actual array values.
import java.util.*;
class PrintArrayElements
{
void array(int x[])
{
for(int i=0;i<x.length;i++)
{
System.out.println("Array Elements are:x["+i+"]="+x[i]);
}
}
}
class Main
{
public static void main(String [] args)
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter Size of array :");
int n=sc.nextInt();
int []a=new int[n];
for(int i=0;i<a.length;i++)
{
System.out.print("Enter Elements of array:a["+i+"]=");
a[i]=sc.nextInt();
}
System.out.println();
By:Ankur Patel
PrintArrayElements pa=new PrintArrayElements();
pa.array(a);
}
}
/* Output
Enter Size of array :3
Enter Elements of array:a[0]=15
Enter Elements of array:a[1]=52
Enter Elements of array:a[2]=25
import java.util.*;
class PrintArrayElements
{
void array(int x[])
{
for(int i=0;i<x.length;i++)
{
System.out.println("Array Elements are:x["+i+"]="+x[i]);
}
}
}
class Main
{
public static void main(String [] args)
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter Size of array :");
int n=sc.nextInt();
int []a=new int[n];
for(int i=0;i<a.length;i++)
{
System.out.print("Enter Elements of array:a["+i+"]=");
a[i]=sc.nextInt();
}
Arrays.sort(a);
System.out.println();
PrintArrayElements pa=new PrintArrayElements();
By:Ankur Patel
pa.array(a);
}
}
/* Output
Enter Size of array :4
Enter Elements of array:a[0]=26
Enter Elements of array:a[1]=13
Enter Elements of array:a[2]=65
Enter Elements of array:a[3]=35
import java.util.*;
class PrintArrayElements
{
void array(int x[])
{
for(int i=x.length-1;i>=0;i--)
{
System.out.println("Array Elements are:x["+i+"]="+x[i]);
}
}
}
class Main
{
public static void main(String [] args)
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter Size of array :");
int n=sc.nextInt();
int []a=new int[n];
for(int i=0;i<a.length;i++)
{
System.out.print("Enter Elements of array:a["+i+"]=");
a[i]=sc.nextInt();
}
Arrays.sort(a);
System.out.println();
By:Ankur Patel
PrintArrayElements pa=new PrintArrayElements();
pa.array(a);
}
}
/* Output
Enter Size of array :4
Enter Elements of array:a[0]=56
Enter Elements of array:a[1]=25
Enter Elements of array:a[2]=13
Enter Elements of array:a[3]=65
import java.util.*;
class PrintArrayElements
{
void array(int x[])
{
for(int i=0;i<x.length;i++)
{
if(x[i]%2==0)
{
System.out.println("Even Array Elements are:x["+i+"]="+x[i]);
}
}
}
}
class Main
{
public static void main(String [] args)
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter Size of array :");
int n=sc.nextInt();
int []a=new int[n];
By:Ankur Patel
for(int i=0;i<a.length;i++)
{
System.out.print("Enter Elements of array:a["+i+"]=");
a[i]=sc.nextInt();
}
Arrays.sort(a);
System.out.println();
PrintArrayElements pa=new PrintArrayElements();
pa.array(a);
}
}
/* Output
Enter Size of array :5
Enter Elements of array:a[0]=21
Enter Elements of array:a[1]=26
Enter Elements of array:a[2]=98
Enter Elements of array:a[3]=99
Enter Elements of array:a[4]=12
import java.util.*;
class Max_Min
{
int max,min;
void max(int x[])
{
max=x[0];
for(int i=0;i<x.length;i++)
{
if(x[i]>max)
{
max=x[i];
}
}
}
By:Ankur Patel
void min(int x[])
{
min=x[0];
for(int i=0;i<x.length;i++)
{
if(x[i]<min)
{
min=x[i];
}
}
}
void show()
{
System.out.println("Max="+max);
System.out.println("Min="+min);
}
}
class Main
{
public static void main(String [] args)
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter Size of array :");
int n=sc.nextInt();
int []a=new int[n];
for(int i=0;i<a.length;i++)
{
System.out.print("Enter Elements of array:a["+i+"]=");
a[i]=sc.nextInt();
}
System.out.println();
Max_Min m=new Max_Min();
m.max(a);
m.min(a);
m.show();
}
}
/* Output
Enter Size of array :5
Enter Elements of array:a[0]=25
Enter Elements of array:a[1]=65
Enter Elements of array:a[2]=89
Enter Elements of array:a[3]=12
By:Ankur Patel
Enter Elements of array:a[4]=123
Max=123
Min=12 */
Array of objects
Java is an object-oriented programming language. Most of the work done with the help of
objects.
We know that an array is a collection of the same data type that dynamically creates objects
and can have elements of primitive types.
Java allows us to store objects in an array.
In Java, the class is also a user-defined data type. An array that conations class type
elements are known as an array of objects. It stores the reference variable of the object.
import java.util.*;
class ArrayofObject_1
{
public static void main(String[] arg)
{
Scanner sc = new Scanner(System.in);
//Creating array of class
System.out.print("Enter no.of Students n=");
int n=sc.nextInt();
Student s[] = new Student[n];
for(int i=0; i<s.length;i++)
{
//creating object of array
s[i] = new Student();
System.out.println("Enter Student "+(i+1)+" Data");
s[i].getData();
}
System.out.println("=================================== ");
System.out.println("------Your Students record is:------- ");
System.out.println("=================================== ");
for(int i=0; i<s.length;i++)
{
s[i].printData();
}
}
By:Ankur Patel
}
class Student
{
int roll_no;
String name;
double java_marks;
void getData()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter Roll No: ");
roll_no = sc.nextInt();
sc.nextLine();
System.out.print("Enter Name: ");
name = sc.nextLine();
System.out.print("Enter java Marks: ");
java_marks = sc.nextDouble();
System.out.println();
}
void printData()
{
System.out.println("Roll number = "+roll_no);
System.out.println("Student Name = "+name);
System.out.println("JAVA-1 marks = "+java_marks);
System.out.println();
}
}
/*
Enter no.of Students n=2
Enter Student 1 Data
Enter Roll No: 01
Enter Name: AP
Enter java Marks: 21
Output:
===================================
------Your Students record is:-------
===================================
Roll number = 1
Student Name = AP
By:Ankur Patel
JAVA-1 marks = 21.0
Roll number = 2
Student Name = VP
JAVA-1 marks = 23.0 */
Let’s create an example program where we will pass argument value to the parameter
of the method using call by value.
class CallbyValue
{
int change(int b)
{
By:Ankur Patel
// Create an object of class.
CallbyValue obj = new CallbyValue();
int a = 20;
int x = obj.change(a);
System.out.println("Value of a after passing: " +a);
System.out.println("Value of x after modifying: " +x);
}
}
The below figure will illustrate how call by value in java works in the above program.
By:Ankur Patel
Lets See another example for swapping values of a and b by Call by Value
class CallByValue
{
void swapFunction(int a, int b)
{
System.out.println("Before swapping(Inside), a = " + a + " b = " + b);
// Swap n1 with n2
int c = a;
a = b;
b = c;
System.out.println("After swapping(Inside), a = " + a + " b = " + b);
}
}
class Main
{
public static void main(String[] args)
{
CallByValue CV=new CallByValue();
int a = 30;
int b = 45;
System.out.println("Before swapping, a = " + a + " and b = " + b);
CV.swapFunction(a, b);
System.out.println("**Now, Before and After swapping values will be same here**:");
System.out.println("After swapping, a = " + a + " and b is " + b);
}
}
By:Ankur Patel
Call by Reference in Java
In Java “Call by Reference” means passing a reference (i.e. address) of the object by value
to a method. We know that a variable of class type contains a reference (i.e. address) to an
object, not object itself.
Thus, when we pass a variable of class type as an argument to a method, actually, a copy
of a reference (address) to an object is passed by value to the method, not a copy of the
object itself. This is because Java does not copy the object into the memory.
Actually, it copies reference of the object into the memory and passes the copy to the
parameters of the called method.
If we change the reference of the object then the original reference does not get changed
because this reference is not original. It’s a copy.
class CallbyRef
{
/*
* The original value of 'a' will be changed as we are trying
* to pass the objects. Objects are passed by reference.
*/
int a = 10;
void call(CallbyRef eg)
{
eg.a = eg.a+10;
}
}
class Main
{
public static void main(String[] args)
{
By:Ankur Patel
}
E:\java ARP>javac CallbyRef.java
Object as an Argument
In Java “Object as an argument” means passing a reference (i.e. address) of the object by
value to a method. We know that a variable of class type contains a reference (i.e. address)
to an object, not object itself.
Thus, when we pass a variable of class type as an argument to a method, actually, a copy
of a reference (address) to an object is passed by value to the method, not a copy of the
object itself. This is because Java does not copy the object into the memory.
Actually, it copies reference of the object into the memory and passes the copy to the
parameters of the called method.
If we change the reference of the object then the original reference does not get changed
because this reference is not original. It’s a copy.
//(9)Object as an argument
class Point
{
int x,y;
void set(int a,int b)
{
x=a;
y=b;
}
void get()
{
System.out.println(x);
System.out.println(y);
}
void copy(Point p1)//Object received as an argument
{
x=p1.x;
y=p1.y;
}
}
class Main
{
By:Ankur Patel
public static void main(String args[])
{
Point p1=new Point();
p1.set(10,20);
p1.get();
Point p2=new Point();
p2.copy(p1);//object passed as an argument
p2.get();
}
}
/* Output:
10
20
10
20
*/
class Test
{
double c_marks, java_marks;
String Name, test;
double Total_Marks;
double Avg_Marks;
void get()
{
System.out.println("Name=" + Name);
System.out.println("Test=" + test);
System.out.println("c_marks= " + c_marks);
System.out.println("java_marks =" + java_marks);
System.out.println();
}
By:Ankur Patel
//Method receiving object as argument
void average(Test t1, Test t2)
{
c_marks = t1.c_marks + t2.c_marks;
java_marks = t1.java_marks + t2.java_marks;
Total_Marks = (c_marks + java_marks);
Avg_Marks = (c_marks + java_marks) / 2;
}
void printTotalAverage()
{
System.out.println("Total_Marks of T1 and T2= " + Total_Marks);
System.out.println("Avg_Marks of T1 and T2= " + Avg_Marks);
}
}
class Main1
{
public static void main(String args[])
{
Test t1=new Test();
Test t2=new Test();
t1.set("Milan","T1",21.0,19.0);
t2.set("Milan","T2",23.0,21.0);
t1.get();
t2.get();
Test t3=new Test();
t3.average(t1,t2);//Object as an argument
t3.printTotalAverage();
}
}
/*
Name=Milan
Test=T1
c_marks= 21.0
java_marks =19.0
Name=Milan
Test=T2
c_marks= 23.0
java_marks =21.0
By:Ankur Patel
Avg_Marks of T1 and T2= 42.0
*/
class Test
{
double c_marks, java_marks;
String Name, test;
double Total_Marks;
double Avg_Marks;
void get()
{
System.out.println("Name=" + Name);
System.out.println("Test=" + test);
System.out.println("c_marks= " + c_marks);
System.out.println("java_marks =" + java_marks);
System.out.println();
}
By:Ankur Patel
}
void printTotalAverage()
{
System.out.println("Total_Marks of T1 and T2= " + Total_Marks);
System.out.println("Avg_Marks of T1 and T2= " + Avg_Marks);
}
}
class Main1
{
public static void main(String args[])
{
Test t1=new Test();
Test t2=new Test();
t1.set("Milan","T1",21.0,19.0);
t2.set("Milan","T2",23.0,21.0);
t1.get();
t2.get();
Test t3=new Test();
Name=Milan
Test=T2
c_marks= 23.0
java_marks =21.0
By:Ankur Patel