473,387 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

concerns about passing arrays into functions

1
how to pass arrays into the function and what is the use of functions.what do you mean by a modification of the array values in the function and does the modification of the values occur only inside the function or does it happen globally
Jun 7 '07 #1
2 1550
DeMan
1,806 1GB
If you pass a reference to the array to the function, all modification is global, however you can copy it to a local array if you want to avoid this.

Alternatively, I'm pretty sure you can pass the actual array (or at least a copy of) to a method, in which case all modification is local ot the method (I think)....
Jun 7 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
You do not pass arrays to functions. What you pass is the name of the array. In C and C++ the name of the array is the address of element 0. So, what is actually passed is the address of the array.

That means any change to the array from inside the function changes the array used on the call.

What is the purpose of a function? Suppose you have an array of 5000 persons and you need to display the person's name and address. You have two choices: a) write the display logic 5000 times, once for each person, or b) write the display logic once, put it in a function, and call the function 5000 times from inside a loop. Your choice.

As to whether modifications made inside a function are local to the function or are made globally is based on the function arguments. All arguments are copies of the variables used on the call. Hence, all modifications are local.
There are two exceptions.

The first exception is a pointer argument. The pointer the function uses is still a copy of the original ione used on the call, but the pointer has the address of some data that is outside the function. By using the pointer inside the function, you change data outside the function.

The second exception is a reference argument (C++ only). A reference is not a copy. Instead, it is an alias for the original object. Any changes to the reference also change the original object since the reference is the original object.

You must look at each argument and ask yourself: Is this a copy? If yes, all chnages are local. If not, all changes are global.
Jun 8 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Andy Fish | last post by:
Hi, I have a HTML page with javascript in it which pops up another HTML page. I can pass simple variables fairly freely between the two pages. I can pass objects between them two, and I have...
12
by: Kevin Lyons | last post by:
Hello, I am trying to get my select options (courses) passed correctly from the following URL: http://www.dslextreme.com/users/kevinlyons/selectBoxes.html I am having difficulty getting the...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
2
by: dave.harper | last post by:
I'm relatively new to C++, but have a question regarding functions and arrays. I'm passing a relatively large array to a function several thousand times during the course of a loop, and it seems...
10
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences...
1
by: Kurt Richardson | last post by:
Hi all Sorry to bother you with what is probably a really trivial question for you C++ experts. My programming skill are pretty amateur, but I'm pretty good at VB.NET. However, I'm wanting to...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
2
by: nleahcim | last post by:
Hi - I am working on writing a number of matrix manipulation functions. The most basic one was a printing algorithm - and it shows the problem I'm having. I'm passing it a pointer a mutidimensional...
1
by: Fizzics | last post by:
This is my first post here at Bytes. I have been trolling it, mostly with the help of Google searches, for some time now. I have done about all of the searching and reading that I really know how to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.