473,757 Members | 8,356 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1792
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","o f","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_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
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_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
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","o f","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","o f","this");
return array($arr1, $arr2);
}

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

C.
Jan 7 '08 #6
On Jan 7, 2:34 am, Michael Fesser <neti...@gmx.de wrote:
.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*********@gm ail.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.com wrote:
Logos <tyler.st...@gm ail.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
1958
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 little bit about what I'm getting back. When I try to get the arrays from another file using the accessor methods, I get no array back. Within the file that contains the arrays, I can access them by global name and also by the accessor methods....
1
3329
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 for my first element but my subsequent printf's will return garbage. Can someone let me know what I am doing wrong. Thanks in advance. -jlewis
0
1092
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 >two-dimensional arrays. Right now the function sends the arrays as an array >of arrays. The caller then decomposes the array of arrays back to its
5
5481
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 *) is it ok?
26
2819
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 function call. #include <iostream> using namespace std; int* GetArray(); //function prototype void main()
17
3259
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: ================================================================================ /* A function that returns a pointer-of-arrays to the calling function. */ #include <stdio.h> int *pfunc(void);
2
1922
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 The question may be silly or even meaning less but please............
26
4879
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 about the syntax of calling the same. #include <stdio.h> void fp1()
17
2028
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 array of drawn balls an array of selected balls.
0
9298
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10072
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9906
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8737
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7286
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6562
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3829
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.