473,387 Members | 1,669 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.

Function returning 2 arrays - which way to do so?

Hi!

I have a function, a part of my code which I can use as a function. It
will return 2 arrays, and I am wondering what way to do so. Both
arrays hold strings, there are no special keys.

1) setting the arrays as globals
2) returnin an array of arrays
3) returning a large array with a known marker to indicate when the
2nd part starts.

Any other ideas?
As of now, it will only be used in one place... but that might change.

WBR
Sonnich
Jan 7 '08 #1
8 1770
jodleren wrote:
Hi!

I have a function, a part of my code which I can use as a function.
I have no clue what that sentence means. :P

It
will return 2 arrays, and I am wondering what way to do so. Both
arrays hold strings, there are no special keys.

1) setting the arrays as globals
2) returnin an array of arrays
3) returning a large array with a known marker to indicate when the
2nd part starts.

Any other ideas?
As of now, it will only be used in one place... but that might change.

WBR
Sonnich
I would go with something like 2, because it is the most structured
approach in my opinion.

Something like this:

function gimme2(){
$arr1 = array("hi","bla");
$arr2 = array("more","of","this");
$returnThis = array();
$returnThis["arr1"] = $arr1;
$returnThis["arr2"] = $arr2;
return $returnThis;
}

// from code:
$result = gimme2();
// $result["arr1"] now contains first array, $result["arr2"] the second

Of course you can put the resulting arrays into a new var if that is
convenient, eg:
$result1 = $result["arr1"];
$result2 = $result["arr2"];

// unset $result maybe if not needed anymore
unset($result);

I prefer returning complex datastructures as hashed arrays, because I
can be more descriptive (like what holds what), but that is a matter of
taste.

Regards,
Erwin Moller
Jan 7 '08 #2
..oO(jodleren)
>I have a function, a part of my code which I can use as a function. It
will return 2 arrays, and I am wondering what way to do so. Both
arrays hold strings, there are no special keys.

1) setting the arrays as globals
No.
>2) returnin an array of arrays
Possible.
>3) returning a large array with a known marker to indicate when the
2nd part starts.
No.
>Any other ideas?
4) Arguments passed by reference:

function foo(&$arg1, &$arg2) {
$arg1 = array(1, 2, 3);
$arg2 = array(4, 5, 6);
}

Micha
Jan 7 '08 #3
On Jan 7, 11:15*am, Erwin Moller
<Since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
jodleren wrote:
Hi!

It
will return 2 arrays, and I am wondering what way to do so. Both
arrays hold strings, there are no special keys.
1) setting the arrays as globals
2) returnin an array of arrays
3) returning a large array with a known marker to indicate when the
2nd part starts.
Any other ideas?
As of now, it will only be used in one place... but that might change.
WBR
Sonnich

I would go with something like 2, because it is the most structured
approach in my opinion.
[snip]
>
I prefer returning complex datastructures as hashed arrays, because I
can be more descriptive (like what holds what), but that is a matter of
taste.
I tend to agree.
Jan 7 '08 #4
jodleren wrote:
Hi!

I have a function, a part of my code which I can use as a function. It
will return 2 arrays, and I am wondering what way to do so. Both
arrays hold strings, there are no special keys.

1) setting the arrays as globals
2) returnin an array of arrays
3) returning a large array with a known marker to indicate when the
2nd part starts.

Any other ideas?
As of now, it will only be used in one place... but that might change.

WBR
Sonnich
Serialized in separated string.
Hmm ^^'

-thib´
Jan 7 '08 #5
On 7 Jan, 09:15, Erwin Moller
<Since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
jodleren wrote:
Hi!
I have a function, a part of my code which I can use as a function.

I have no clue what that sentence means. :P

It
will return 2 arrays, and I am wondering what way to do so. Both
arrays hold strings, there are no special keys.
<snip>
I would go with something like 2, because it is the most structured
approach in my opinion.

Something like this:

function gimme2(){
$arr1 = array("hi","bla");
$arr2 = array("more","of","this");
$returnThis = array();
$returnThis["arr1"] = $arr1;
$returnThis["arr2"] = $arr2;
return $returnThis;

}

// from code:
$result = gimme2();
// $result["arr1"] now contains first array, $result["arr2"] the second
<snip>
>
I prefer returning complex datastructures as hashed arrays, because I
can be more descriptive (like what holds what), but that is a matter of
taste.
/me prefers not to explicitly create data structures which are only
used once:

function gimme2(){
$arr1 = array("hi","bla");
$arr2 = array("more","of","this");
return array($arr1, $arr2);
}

list($first, $second)=gime2();

C.
Jan 7 '08 #6
On Jan 7, 2:34 am, Michael Fesser <neti...@gmx.dewrote:
.oO(jodleren)
I have a function, a part of my code which I can use as a function. It
will return 2 arrays, and I am wondering what way to do so. Both
arrays hold strings, there are no special keys.
1) setting the arrays as globals

No.
2) returnin an array of arrays

Possible.
3) returning a large array with a known marker to indicate when the
2nd part starts.

No.
Any other ideas?

4) Arguments passed by reference:

function foo(&$arg1, &$arg2) {
$arg1 = array(1, 2, 3);
$arg2 = array(4, 5, 6);

}

Micha
I'll second this one. Passing in both arrays by reference is clean &
tidy, and a commonly accepted way for a function to 'return' more than
one value.
Jan 7 '08 #7
Logos <ty*********@gmail.comwrote:
>
I'll second this one. Passing in both arrays by reference is clean &
tidy, and a commonly accepted way for a function to 'return' more than
one value.
That's true in C and C++, but only because there's no way to return several
things at once.

There are two issues with the reference solution. First, it requires the
caller to allocate arrays first, and then pass those arrays to the
function. In many cases, that's not natural. Second, it creates a high
degree of linkage between the callee and caller, which again may not be
natural.

Now, these are both highly philosophical issues, but there is good sense in
adapting a policy that things to be returned should always be "returned",
rather than by modifying in/out parameters.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jan 8 '08 #8
On Jan 7, 11:23 pm, Tim Roberts <t...@probo.comwrote:
Logos <tyler.st...@gmail.comwrote:
I'll second this one. Passing in both arrays by reference is clean &
tidy, and a commonly accepted way for a function to 'return' more than
one value.

That's true in C and C++, but only because there's no way to return several
things at once.

There are two issues with the reference solution. First, it requires the
caller to allocate arrays first, and then pass those arrays to the
function. In many cases, that's not natural. Second, it creates a high
degree of linkage between the callee and caller, which again may not be
natural.

Now, these are both highly philosophical issues, but there is good sense in
adapting a policy that things to be returned should always be "returned",
rather than by modifying in/out parameters.
--
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.
Well, I'll admit to a C background.

However, if you're going to offer a critique I think it's only fair to
report which solution you think best & why, no? We have several
options for things being returned :)

IMHO, I think this is one of those stylistic things that really
doesn't have a best answer. It doesn't really impact performance in
any way - all that it impacts is *how the coder* interacts with the
code.
Jan 9 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: jeff | last post by:
I have a php file that contains a couple of arrays used for state/country pull-down lists. I have two global arrays and an accessor method for each. I have some simple logging methods, so I know a...
1
by: john | last post by:
Relatively new to C coding, so any help would greatly be appreciated. I'm having problems try to return my string array from my parsing function. When I do a printf I am getting the correct value...
0
by: Tom | last post by:
Shouldn't you be able to return them as 3 By Ref arguments in the function call? >-----Original Message----- >I have a class with a function which need to return to the caller three...
5
by: shyam | last post by:
Hi All I have to write a function which basically takes in a string and returns an unknown number( at compile time) of strings i hav the following syntax in mind char *tokenize(char *) ...
26
by: cdg | last post by:
Could anyone correct any mistakes in this example program. I am just trying to return an array back to "main" to be printed out. And I am not sure how a "pointer to an array" is returned to the...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
2
by: mosesdinakaran | last post by:
Hi everybody, Today I faced a problem where I am very confused and I could not solve it and I am posting here.... My question is Is is possible to return a value to a particular function ...
26
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure...
17
by: boobikins | last post by:
Hi guys, I'm trying to complete a function that does the following: /* Tests if all balls selected match all balls drawn in the same order The function takes two arguments: an...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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.