Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 112
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 3368: Cannot modify header information - headers already sent by (output started at /includes/bbcode.php:472)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 3370: Cannot modify header information - headers already sent by (output started at /includes/bbcode.php:472)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 3371: Cannot modify header information - headers already sent by (output started at /includes/bbcode.php:472)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 3372: Cannot modify header information - headers already sent by (output started at /includes/bbcode.php:472)
OrbWorks Community Forum • View topic - Return

Return

Postby md_lasalle on Tue Jan 28, 2003 12:04 pm

I was wondering, can a function return more than one value?
If yes, what is the syntaxe?

thanks
md_lasalle
 
Posts: 74
Joined: Fri Dec 20, 2002 12:26 am
Location: Canada

Postby jstadolnik on Tue Jan 28, 2003 1:06 pm

Just as in C, only one value can be "returned" by a given function. However there are other ways of getting more than one value back...

1) Use global variables:

int x; //global

int func() {
x=1;
return(2);
}

2) Pass data back through pointers:

int func(pointer argP) { //single value returned
*argP = 1;
return(2);
}

int func2(pointer argP) { //array of values returned
argP[0]=1;
argP[1]=2;
argP[2]=3;
return(4);
}

main() {
int x, y, z[3];
x=func(&y);
puts(x+","+y+"\n");
x=func2(z); //since z is an array it's actually a pointer
puts(z[0]+","+z[1]+","+z[2]+","+x+"\n");
}


3) Return a dynamic array.

pointer func() {
pointer a;
a=malloc(2); //dynamic allocations slow things down
a[0]=1;
a[1]=2;
}

main() {
pointer p;
p=func();
puts(p[0]+"\n"+p[1]+"\n");
free(p); //don't forget to free the array
}


Joe

The PToolboxLib guy.
http://www.geocities.com/retro_01775/PToolboxLib.htm
jstadolnik
 
Posts: 1741
Joined: Wed Dec 06, 2000 3:34 am
Location: USA

Postby wolfgang on Tue Jan 28, 2003 4:00 pm

Hi Joe,

you forgot to return the pointer in your last example, so it will not work as intended. Corrected version below!

Wolfgang


3) Return a dynamic array.

pointer func() {
pointer a;
a=malloc(2); //dynamic allocations slow things down
a[0]=1;
a[1]=2;
return(a);
}

main() {
pointer p;
p=func();
puts(p[0]+"\n"+p[1]+"\n");
free(p); //don't forget to free the array
}



Composing music on the PocketPC! - http://www.pdamusician.com
Composing music on the PocketPC! - http://www.pdamusician.com
wolfgang
 
Posts: 281
Joined: Tue Dec 12, 2000 4:22 am
Location: USA

Postby jstadolnik on Tue Jan 28, 2003 4:21 pm

Doh! Thanks,

Joe

The PToolboxLib guy.
http://www.geocities.com/retro_01775/PToolboxLib.htm
jstadolnik
 
Posts: 1741
Joined: Wed Dec 06, 2000 3:34 am
Location: USA

Postby soinsg on Wed Jan 29, 2003 4:27 pm

There is also another way, similar to the time() function as in the PocketC doc's.
Code: Select all
func() {
int a,b;
a = gets("a?");
b = random(100);
//puts(b + "\n");

return a*100 + b;
}

main() {
int a,b,c;
c = func();
a = c / 100;
b = c % 100;
puts(a + "\n");
puts(b + "\n");
}

I'm not sure if my code will work as it is a little confusing to get your head round (to me anyway).
soinsg
 
Posts: 198
Joined: Thu May 17, 2001 4:26 pm


Return to PocketC for Palm OS KB

Who is online

Users browsing this forum: No registered users and 1 guest

cron