Share
Go down
Admin
Posts : 11
Join date : 2018-08-07
Age : 19
View user profilehttp://rachellann.forumotion.com

Passing an Array to a Method

on Wed Aug 08, 2018 2:31 pm
You learned that you can declare an array to create a list of elements, and that you can use any individual array element in the same manner you would use any single variable of the same type. For example, suppose that you declare a numeric array as follows:
num someNums[12]
[bYou can subsequently output someNums[0] or perform arithmetic with someNums[11] , just as you would for any simple variable that is not part of an array. Similarly, you can pass a single array element to a method in exactly the same manner you would pass a variable or constant.
Consider the program shown in Figure 9-13. This program creates an array of four numeric values and then outputs them. Next, the program calls a method named tripleTheValue() four times, passing each of the array elements in turn. The method outputs the passed value, multiplies it by 3, and outputs it again. Finally, back in the calling program, the four numbers are output again. Figure 9-14 shows an execution of this program in a command-line environment
.[/b]







As you can see in Figure 9-14, the program displays the four original values, then passes each value to the tripleTheValue() method, where it is displayed, multiplied by 3, and displayed again. After the method executes four times, the logic returns to the main program where the four values are displayed again, showing that they are unchanged by the new assignments within tripleTheValue() . The oneVal variable is local to the tripleTheValue() method; therefore, any changes to it are not permanent and are not reflected in the array declared in the main program. Each oneVal variable in the tripleTheValue() method holds only a copy of the array element passed into the method, and the oneVal variable that holds each newly assigned, larger value exists only while the tripleTheValue() method is executing.

In all respects, a single array element acts just like any single variable of the same type would. Instead of passing a single array element to a method, you can pass an entire array as an argument. You can indicate that a method parameter must be an array by using the convention of placing square brackets after the data type in the method’s parameter list. When you pass an array to a method, changes you make to array elements within the method are permanent; that is, they are reflected in the original array that was sent to the method. Arrays, unlike simple built-in types, are passed by reference ; the method receives the actual memory address of the array and has access to the actual values in the array elements. The name of an array represents a memory address, and the subscript used with an array name represents an offset from that address.






The program shown in Figure 9-15 creates an array of four numeric values. After the numbers are output, the entire array is passed to a method named quadrupleTheValues() .
Within the method header, the parameter is declared as an array by using square brackets after the parameter type. Within the method, the numbers are output, which shows that they retain their values from the main program upon entering the method. Then the array values are multiplied by 4. Even though quadrupleTheValues() returns nothing to the calling program, when the program displays the array for the last time within the mainline logic, all of the values have been changed to their new quadrupled values. Figure 9-16 shows an execution of the program. Because arrays are passed by reference, the quadrupleTheValues() method “knows” the address of the array declared in the calling program and makes its changes directly to the original array that was declared in the calling program.

When an array is a method parameter, the square brackets in the method header remain empty and do not hold a size. The array name that is passed is a memory address that indicates the start of the array.
Depending on the language you are working in, you can control the values you use for a subscript to the array in different ways. In some languages, you might also want to pass a constant that indicates the array size to the method. In other languages, you can access the automatically created length field for the array.
Either way, the array size itself is never implied when you use the array name. The array name only indicates the starting point from which subscripts will be used.
TWO TRUTHS & A LIE
Passing an Array to a Method
1. You can pass an entire array as a method’s argument.
2. You can indicate that a method parameter must be an array by placing square brackets after the data type in the method’s parameter list.
3. Arrays, unlike simple built-in types, are passed by value; the method receives a copy of the original array.

WHICH OF THEM IS A LIE?
Guru
Guru
Posts : 10
Join date : 2018-08-14
View user profile

Re: Passing an Array to a Method

on Tue Aug 14, 2018 8:35 am
What program language used in this?
Guru
Guru
Posts : 10
Join date : 2018-08-14
View user profile

Re: Passing an Array to a Method

on Tue Aug 14, 2018 8:37 am
This is such a good post. I now understand the arrays. Looking forward to your future discussions.
avatar
Guru
Guru
Posts : 10
Join date : 2018-08-16
View user profile

Re: Passing an Array to a Method

on Thu Aug 16, 2018 10:10 am
Admin wrote:
You learned that you can declare an array to create a list of elements, and that you can use any individual array element in the same manner you would use any single variable of the same type. For example, suppose that you declare a numeric array as follows:
num someNums[12]
[bYou can subsequently output someNums[0] or perform arithmetic with someNums[11] , just as you would for any simple variable that is not part of an array. Similarly, you can pass a single array element to a method in exactly the same manner you would pass a variable or constant.
Consider the program shown in Figure 9-13. This program creates an array of four numeric values and then outputs them. Next, the program calls a method named tripleTheValue() four times, passing each of the array elements in turn. The method outputs the passed value, multiplies it by 3, and outputs it again. Finally, back in the calling program, the four numbers are output again. Figure 9-14 shows an execution of this program in a command-line environment
.[/b]







As you can see in Figure 9-14, the program displays the four original values, then passes each value to the tripleTheValue() method, where it is displayed, multiplied by 3, and displayed again. After the method executes four times, the logic returns to the main program where the four values are displayed again, showing that they are unchanged by the new assignments within tripleTheValue() . The oneVal variable is local to the tripleTheValue() method; therefore, any changes to it are not permanent and are not reflected in the array declared in the main program. Each oneVal variable in the tripleTheValue() method holds only a copy of the array element passed into the method, and the oneVal variable that holds each newly assigned, larger value exists only while the tripleTheValue() method is executing.

In all respects, a single array element acts just like any single variable of the same type would. Instead of passing a single array element to a method, you can pass an entire array as an argument. You can indicate that a method parameter must be an array by using the convention of placing square brackets after the data type in the method’s parameter list. When you pass an array to a method, changes you make to array elements within the method are permanent; that is, they are reflected in the original array that was sent to the method. Arrays, unlike simple built-in types, are passed by reference ; the method receives the actual memory address of the array and has access to the actual values in the array elements. The name of an array represents a memory address, and the subscript used with an array name represents an offset from that address.






The program shown in Figure 9-15 creates an array of four numeric values. After the numbers are output, the entire array is passed to a method named quadrupleTheValues() .
Within the method header, the parameter is declared as an array by using square brackets after the parameter type. Within the method, the numbers are output, which shows that they retain their values from the main program upon entering the method. Then the array values are multiplied by 4. Even though quadrupleTheValues() returns nothing to the calling program, when the program displays the array for the last time within the mainline logic, all of the values have been changed to their new quadrupled values. Figure 9-16 shows an execution of the program. Because arrays are passed by reference, the quadrupleTheValues() method “knows” the address of the array declared in the calling program and makes its changes directly to the original array that was declared in the calling program.

When an array is a method parameter, the square brackets in the method header remain empty and do not hold a size. The array name that is passed is a memory address that indicates the start of the array.
Depending on the language you are working in, you can control the values you use for a subscript to the array in different ways. In some languages, you might also want to pass a constant that indicates the array size to the method. In other languages, you can access the automatically created length field for the array.
Either way, the array size itself is never implied when you use the array name. The array name only indicates the starting point from which subscripts will be used.
TWO TRUTHS & A LIE
Passing an Array to a Method
1. You can pass an entire array as a method’s argument.
2. You can indicate that a method parameter must be an array by placing square brackets after the data type in the method’s parameter list.
3. Arrays, unlike simple built-in types, are passed by value; the method receives a copy of the original array.

WHICH OF THEM IS A LIE?

SALAMAT PO MA'AM!!!
avatar
Guru
Guru
Posts : 10
Join date : 2018-08-16
View user profile

Re: Passing an Array to a Method

on Thu Aug 16, 2018 10:12 am
Hyegi wrote:This is such a good post. I now understand the arrays. Looking forward to your future discussions.


site some examples yhegiiee yhegiiee hahaha
avatar
Guru
Guru
Posts : 10
Join date : 2018-08-16
View user profile

Re: Passing an Array to a Method

on Thu Aug 16, 2018 10:13 am
Hyegi wrote:What program language used in this?

there is no specific program language ata diyan e dahil ganiyan na talaga siya brodie parang unisexual
yan pangkalahatan yata

skl ha!
avatar
Guru
Guru
Posts : 10
Join date : 2018-08-16
View user profile

Re: Passing an Array to a Method

on Thu Aug 16, 2018 10:14 am
Noob wrote:
Admin wrote:
You learned that you can declare an array to create a list of elements, and that you can use any individual array element in the same manner you would use any single variable of the same type. For example, suppose that you declare a numeric array as follows:
num someNums[12]
[bYou can subsequently output someNums[0] or perform arithmetic with someNums[11] , just as you would for any simple variable that is not part of an array. Similarly, you can pass a single array element to a method in exactly the same manner you would pass a variable or constant.
Consider the program shown in Figure 9-13. This program creates an array of four numeric values and then outputs them. Next, the program calls a method named tripleTheValue() four times, passing each of the array elements in turn. The method outputs the passed value, multiplies it by 3, and outputs it again. Finally, back in the calling program, the four numbers are output again. Figure 9-14 shows an execution of this program in a command-line environment
.[/b]







As you can see in Figure 9-14, the program displays the four original values, then passes each value to the tripleTheValue() method, where it is displayed, multiplied by 3, and displayed again. After the method executes four times, the logic returns to the main program where the four values are displayed again, showing that they are unchanged by the new assignments within tripleTheValue() . The oneVal variable is local to the tripleTheValue() method; therefore, any changes to it are not permanent and are not reflected in the array declared in the main program. Each oneVal variable in the tripleTheValue() method holds only a copy of the array element passed into the method, and the oneVal variable that holds each newly assigned, larger value exists only while the tripleTheValue() method is executing.

In all respects, a single array element acts just like any single variable of the same type would. Instead of passing a single array element to a method, you can pass an entire array as an argument. You can indicate that a method parameter must be an array by using the convention of placing square brackets after the data type in the method’s parameter list. When you pass an array to a method, changes you make to array elements within the method are permanent; that is, they are reflected in the original array that was sent to the method. Arrays, unlike simple built-in types, are passed by reference ; the method receives the actual memory address of the array and has access to the actual values in the array elements. The name of an array represents a memory address, and the subscript used with an array name represents an offset from that address.






The program shown in Figure 9-15 creates an array of four numeric values. After the numbers are output, the entire array is passed to a method named quadrupleTheValues() .
Within the method header, the parameter is declared as an array by using square brackets after the parameter type. Within the method, the numbers are output, which shows that they retain their values from the main program upon entering the method. Then the array values are multiplied by 4. Even though quadrupleTheValues() returns nothing to the calling program, when the program displays the array for the last time within the mainline logic, all of the values have been changed to their new quadrupled values. Figure 9-16 shows an execution of the program. Because arrays are passed by reference, the quadrupleTheValues() method “knows” the address of the array declared in the calling program and makes its changes directly to the original array that was declared in the calling program.

When an array is a method parameter, the square brackets in the method header remain empty and do not hold a size. The array name that is passed is a memory address that indicates the start of the array.
Depending on the language you are working in, you can control the values you use for a subscript to the array in different ways. In some languages, you might also want to pass a constant that indicates the array size to the method. In other languages, you can access the automatically created length field for the array.
Either way, the array size itself is never implied when you use the array name. The array name only indicates the starting point from which subscripts will be used.
TWO TRUTHS & A LIE
Passing an Array to a Method
1. You can pass an entire array as a method’s argument.
2. You can indicate that a method parameter must be an array by placing square brackets after the data type in the method’s parameter list.
3. Arrays, unlike simple built-in types, are passed by value; the method receives a copy of the original array.

WHICH OF THEM IS A LIE?

SALAMAT PO MA'AM!!!

TWO TRUTHS & A LIE
Passing an Array to a Method
1. You can pass an entire array as a method’s argument.
paano po siya gawing method's argument ma'am ????? TWO TRUTHS & A LIE
Passing an Array to a Method
1. You can pass an entire array as a method’s argument.
2. You can indicate that a method parameter must be an array by placing square brackets after the data type in the method’s parameter list.
3. Arrays, unlike simple built-in types, are passed by value; the method receives a copy of the original array.
avatar
Guru
Guru
Posts : 10
Join date : 2018-08-16
View user profile

Re: Passing an Array to a Method

on Thu Aug 16, 2018 10:15 am
Noob wrote:
Noob wrote:
Admin wrote:
You learned that you can declare an array to create a list of elements, and that you can use any individual array element in the same manner you would use any single variable of the same type. For example, suppose that you declare a numeric array as follows:
num someNums[12]
[bYou can subsequently output someNums[0] or perform arithmetic with someNums[11] , just as you would for any simple variable that is not part of an array. Similarly, you can pass a single array element to a method in exactly the same manner you would pass a variable or constant.
Consider the program shown in Figure 9-13. This program creates an array of four numeric values and then outputs them. Next, the program calls a method named tripleTheValue() four times, passing each of the array elements in turn. The method outputs the passed value, multiplies it by 3, and outputs it again. Finally, back in the calling program, the four numbers are output again. Figure 9-14 shows an execution of this program in a command-line environment
.[/b]







As you can see in Figure 9-14, the program displays the four original values, then passes each value to the tripleTheValue() method, where it is displayed, multiplied by 3, and displayed again. After the method executes four times, the logic returns to the main program where the four values are displayed again, showing that they are unchanged by the new assignments within tripleTheValue() . The oneVal variable is local to the tripleTheValue() method; therefore, any changes to it are not permanent and are not reflected in the array declared in the main program. Each oneVal variable in the tripleTheValue() method holds only a copy of the array element passed into the method, and the oneVal variable that holds each newly assigned, larger value exists only while the tripleTheValue() method is executing.

In all respects, a single array element acts just like any single variable of the same type would. Instead of passing a single array element to a method, you can pass an entire array as an argument. You can indicate that a method parameter must be an array by using the convention of placing square brackets after the data type in the method’s parameter list. When you pass an array to a method, changes you make to array elements within the method are permanent; that is, they are reflected in the original array that was sent to the method. Arrays, unlike simple built-in types, are passed by reference ; the method receives the actual memory address of the array and has access to the actual values in the array elements. The name of an array represents a memory address, and the subscript used with an array name represents an offset from that address.






The program shown in Figure 9-15 creates an array of four numeric values. After the numbers are output, the entire array is passed to a method named quadrupleTheValues() .
Within the method header, the parameter is declared as an array by using square brackets after the parameter type. Within the method, the numbers are output, which shows that they retain their values from the main program upon entering the method. Then the array values are multiplied by 4. Even though quadrupleTheValues() returns nothing to the calling program, when the program displays the array for the last time within the mainline logic, all of the values have been changed to their new quadrupled values. Figure 9-16 shows an execution of the program. Because arrays are passed by reference, the quadrupleTheValues() method “knows” the address of the array declared in the calling program and makes its changes directly to the original array that was declared in the calling program.

When an array is a method parameter, the square brackets in the method header remain empty and do not hold a size. The array name that is passed is a memory address that indicates the start of the array.
Depending on the language you are working in, you can control the values you use for a subscript to the array in different ways. In some languages, you might also want to pass a constant that indicates the array size to the method. In other languages, you can access the automatically created length field for the array.
Either way, the array size itself is never implied when you use the array name. The array name only indicates the starting point from which subscripts will be used.
TWO TRUTHS & A LIE
Passing an Array to a Method
1. You can pass an entire array as a method’s argument.
2. You can indicate that a method parameter must be an array by placing square brackets after the data type in the method’s parameter list.
3. Arrays, unlike simple built-in types, are passed by value; the method receives a copy of the original array.

WHICH OF THEM IS A LIE?

SALAMAT PO MA'AM!!!

TWO TRUTHS & A LIE
Passing an Array to a Method
1. You can pass an entire array as a method’s argument.
paano po siya gawing method's argument ma'am ????? TWO TRUTHS & A LIE
Passing an Array to a Method
1. You can pass an entire array as a method’s argument.
2. You can indicate that a method parameter must be an array by placing square brackets after the data type in the method’s parameter list.
3. Arrays, unlike simple built-in types, are passed by value; the method receives a copy of the original array.

TWO TRUTHS & A LIE
Passing an Array to a Method

2. You can indicate that a method parameter must be an array by placing square brackets after the data type in the method’s parameter list.
ganito po ba? STACATTO [6] 1, 2, 3 ,4 ,5 ,6 ,7??
avatar
Guru
Guru
Posts : 10
Join date : 2018-08-16
View user profile

Re: Passing an Array to a Method

on Thu Aug 16, 2018 10:16 am
Hyegi wrote:This is such a good post. I now understand the arrays. Looking forward to your future discussions.

can you site some examples Very Happy Very Happy
avatar
Guru
Guru
Posts : 10
Join date : 2018-08-16
View user profile

Re: Passing an Array to a Method

on Thu Aug 16, 2018 10:17 am
Noob wrote:
Hyegi wrote:This is such a good post. I now understand the arrays. Looking forward to your future discussions.

can you site some examples Very Happy Very Happy

nakasearch ako sa youtube ng mga examples ng ganun napakaangas ka mo gusto mo mapanood>>?? Cool Cool
avatar
Guru
Guru
Posts : 10
Join date : 2018-08-16
View user profile

Re: Passing an Array to a Method

on Thu Aug 16, 2018 10:18 am
Noob wrote:
Noob wrote:
Hyegi wrote:This is such a good post. I now understand the arrays. Looking forward to your future discussions.

can you site some examples Very Happy Very Happy

nakasearch ako sa youtube ng mga examples ng ganun napakaangas ka mo gusto mo mapanood>>?? Cool Cool

maangas kamo un pre napakaangas nung array kung paano siya nakokonekta sa data base para mas mabilis na mapaunlad ang sistema  Arrow Arrow
avatar
Guru
Guru
Posts : 10
Join date : 2018-08-16
View user profile

Re: Passing an Array to a Method

on Thu Aug 16, 2018 10:18 am
Noob wrote:
Noob wrote:
Noob wrote:
Hyegi wrote:This is such a good post. I now understand the arrays. Looking forward to your future discussions.

can you site some examples Very Happy Very Happy

nakasearch ako sa youtube ng mga examples ng ganun napakaangas ka mo gusto mo mapanood>>?? Cool Cool

maangas kamo un pre napakaangas nung array kung paano siya nakokonekta sa data base para mas mabilis na mapaunlad ang sistema  Arrow Arrow

Suspect Suspect Suspect Suspect pero mas better na kung ikaw gagawa ng iyo tas papacheck mo sa mga guru narin hahaha
avatar
Guru
Guru
Posts : 10
Join date : 2018-08-16
View user profile

Re: Passing an Array to a Method

on Thu Aug 16, 2018 10:19 am
Noob wrote:
Noob wrote:
Noob wrote:
Noob wrote:
Hyegi wrote:This is such a good post. I now understand the arrays. Looking forward to your future discussions.

can you site some examples Very Happy Very Happy

nakasearch ako sa youtube ng mga examples ng ganun napakaangas ka mo gusto mo mapanood>>?? Cool Cool

maangas kamo un pre napakaangas nung array kung paano siya nakokonekta sa data base para mas mabilis na mapaunlad ang sistema  Arrow Arrow

Suspect Suspect Suspect Suspect  pero mas better na kung ikaw gagawa ng iyo tas papacheck mo sa mga guru narin hahaha

last na to watch mo sa ww1.yowtub.tv ung gagawin nila hahhaa What a Face What a Face
Guru
Guru
Posts : 10
Join date : 2018-08-18
View user profile

Re: Passing an Array to a Method

on Sat Aug 18, 2018 6:54 pm
How can I pass an entire array to a method? can someone help me.
Guru
Guru
Posts : 10
Join date : 2018-08-18
View user profile

Re: Passing an Array to a Method

on Sat Aug 18, 2018 6:55 pm
ekkoekko wrote:How can I pass an entire array to a method? can someone help me.

I dont know. And also want to know how. can someone help us? Thanks in advance. lols.
Guru
Guru
Posts : 10
Join date : 2018-08-20
View user profile

Re: Passing an Array to a Method

on Mon Aug 20, 2018 2:54 pm
ekkoekko wrote:How can I pass an entire array to a method? can someone help me.

In C programming, a single array element or an entire array can be passed to a function.
for example.
C program to pass a single element of an array to function

#include <stdio.h>
void display(int age)
{
printf("%d", age);
}

int main()
{
int ageArray[] = { 2, 3, 4 };
display(ageArray[2]); //Passing array element ageArray[2] only.
return 0;
}

Output

4
Guru
Guru
Posts : 10
Join date : 2018-08-20
View user profile

Re: Passing an Array to a Method

on Mon Aug 20, 2018 3:10 pm
Sometimes, it is required to pass arrays to method parameters to transfer large amount of data between methods; instead of creating a global array which can be changed by every object. The concept used is Java passes "objects by reference" and not by value. Arrays, being predefined objects, pass by reference. Other way, a Java programmer can achieve pointer affect by passing objects between method calls.
Guru
Guru
Posts : 10
Join date : 2018-08-20
View user profile

Re: Passing an Array to a Method

on Mon Aug 20, 2018 3:12 pm
another example.

class sortNumbers
{
public static void main(String[] args)
{
int[] data={40,50,10,30,20,5};
System.out.println("Unsorted List is :");
display(data);
sort(data);
System.out.println("\nSorted List is :");
display(data);
}
static void display(int num[])
{
for(int i=0; i<num.length;i++)
System.out.print(num[i] + " ");
}
static void sort(int num[])
{
int i, j, temp;
for(i=0; i<num.length-i;i++)
{
for(j=0; j<num.length-i-1;j++)
{
if(num[j]>num[j+1])
{
temp = num[j];
num[j] = num[j+1];
num[j+1] = temp;
}
}
}
}
}



In this example, we sort the numbers by using the concept of passing arrays to methods. The unsorted array is passed to the sort () method. In the method's definition, the array referenced using num reference variable is sorted and the changes made are reflected back.
Guru
Guru
Posts : 10
Join date : 2018-08-20
View user profile

Re: Passing an Array to a Method

on Mon Aug 20, 2018 3:14 pm
Passing an array mimics a concept called "pass-by-reference", meaning that when an array is passed as an argument, its memory address location (its "reference") is used. In this way, the contents of an array CAN be changed inside of a method, since we are dealing directly with the actual array and not with a copy of the array.
Note: In Java, a pointer value to the memory address location is used, making arrays "pass-by-value" (and not actually "pass-by-reference" as seen in C++.)
Guru
Guru
Posts : 10
Join date : 2018-08-20
View user profile

Re: Passing an Array to a Method

on Mon Aug 20, 2018 3:58 pm
Does the basis in lecture are the things to identify the truth or lie?
Guru
Guru
Posts : 10
Join date : 2018-08-20
View user profile

Re: Passing an Array to a Method

on Mon Aug 20, 2018 3:59 pm
annmaganda wrote:Sometimes, it is required to pass arrays to method parameters to transfer large amount of data between methods; instead of creating a global array which can be changed by every object. The concept used is Java passes "objects by reference" and not by value. Arrays, being predefined objects, pass by reference. Other way, a Java programmer can achieve pointer affect by passing objects between method calls.

Well said
Guru
Guru
Posts : 10
Join date : 2018-08-20
View user profile

Re: Passing an Array to a Method

on Mon Aug 20, 2018 4:00 pm
may mas madali pa po bang way ng passing an array to a method? Weak lang pu ako e
Guru
Guru
Posts : 10
Join date : 2018-08-20
View user profile

Re: Passing an Array to a Method

on Mon Aug 20, 2018 4:01 pm
Noob wrote:
Hyegi wrote:What program language used in this?

there is no specific program language ata diyan e dahil ganiyan na talaga siya brodie parang unisexual
yan pangkalahatan yata

skl ha!

Oo nga walang specific na program language
Guru
Guru
Posts : 10
Join date : 2018-08-20
View user profile

Re: Passing an Array to a Method

on Mon Aug 20, 2018 4:01 pm
very informative forum. thankyou Ma'am Rachell.
Guru
Guru
Posts : 10
Join date : 2018-08-20
View user profile

Re: Passing an Array to a Method

on Mon Aug 20, 2018 4:02 pm
Noob wrote:
Noob wrote:
Noob wrote:
Hyegi wrote:This is such a good post. I now understand the arrays. Looking forward to your future discussions.

can you site some examples Very Happy Very Happy

nakasearch ako sa youtube ng mga examples ng ganun napakaangas ka mo gusto mo mapanood>>?? Cool Cool

maangas kamo un pre napakaangas nung array kung paano siya nakokonekta sa data base para mas mabilis na mapaunlad ang sistema  Arrow Arrow

HAHAHAHAHA
Sponsored content

Re: Passing an Array to a Method

Back to top
Permissions in this forum:
You cannot reply to topics in this forum