SunQuest
           C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old January 19th, 2003, 01:24 PM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
returning variable*s* from functions?

if a function that's called from your main function needs to use, modify and return numerous int variables (like i and j counter/tracker types of variables along with a few others, for example), is there any other way to get these back to the main function and back into their respective variables, other than making them global, or semi-global (external variables declared static) or is making them global/semi-global the best way?

this is a problem that has cropped up for me in various little exercise bits of code i'm doing, so i can only imagine the problem getting worse, and i'm wondering how this sort of thing is dealt with generally? it's advised that you pass variables as much as possible via function parameters and return them via return, but the parts of my code that lend themselves well to function-ising often need to make use of and return more than one variable. arrays don't need returning (which has always seemed a bit odd to me - the difference between single and array variables), but ints do need returning and it seems that only one can be returned.

this is mainly with variables that already exist in the main loop like as i mentioned earlier i & j type counters, in mind.

Reply With Quote
  #2  
Old January 19th, 2003, 01:33 PM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,969 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 39 m 55 sec
Reputation Power: 184
if you want to modify variables inside a function, pass them by reference or pointer:
Code:
void add(int a, int b, int &result) {
  result=a+b;
}
or:
void add(int a, int b, int *result) {
  *result=a+b;
}
__________________
--
Manuel Hirsch - Linux, FreeBSD, programming, administration articles, tutorials and more.

Reply With Quote
  #3  
Old January 19th, 2003, 03:29 PM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
ok thanks. is that c or c++? if it's c++, sorry i meant c. if it is in c i can't get it to work right now but i haven't tried particularly hard yet

Reply With Quote
  #4  
Old January 19th, 2003, 03:42 PM
Strike Strike is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: Houston, TX
Posts: 383 Strike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 7
Send a message via ICQ to Strike Send a message via AIM to Strike Send a message via Yahoo to Strike
If you'd rather just return modified versions of a certain variable (instead of directly modifying the variable itself), simply bundle the modified variables together into a struct and return that struct. The caller can pick the pieces out of the struct that it wants.
__________________
Debian - because life's too short for worrying.
Best. (Python.) IRC bot. ever.

Reply With Quote
  #5  
Old January 20th, 2003, 03:56 AM
Analyser's Avatar
Analyser Analyser is offline
*bounce*
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Delft, The Netherlands
Posts: 510 Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 20 h 37 m 7 sec
Reputation Power: 9
Send a message via ICQ to Analyser
M. HIrsch's second example is the C variant. That is:

Code:
void add(int a, int b, int *result) {
  *result = a+b;
}


Since you're new to pointers, maybe you'll find this example helpful:

Code:
void swap (int *a, int *b) {
   int tmp;
   tmp = *a;
   *a = *b;
   *b = tmp;
}

int main() {
   int i, j;

   i = 12;
   j = 15;
   printf("i = %d, j = %d\n", i, j);
   swap (&i, &j);
   printf("i = %d, j = %d\n", i, j);
}


Good luck
__________________
"A poor programmer is he who blames his tools."
http://analyser.oli.tudelft.nl/

Reply With Quote
  #6  
Old January 20th, 2003, 07:20 AM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
right so the answer is pointers basically. as you mentioned, i haven't got that far yet. i had thought that i'd pretty well much covered variables in the book i'm learning from, and therefore missed how this should be done. but i haven't missed it. i just haven't got to it yet. ok, thanks.

Reply With Quote
  #7  
Old January 20th, 2003, 11:17 AM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,969 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 39 m 55 sec
Reputation Power: 184
so the "big fun" (pointers) still lies ahead of you...

rule #1:
never give up.
rule #2:
if you still want to, go to #1

Reply With Quote
  #8  
Old January 20th, 2003, 11:37 AM
Analyser's Avatar
Analyser Analyser is offline
*bounce*
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Delft, The Netherlands
Posts: 510 Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 20 h 37 m 7 sec
Reputation Power: 9
Send a message via ICQ to Analyser
Quote:
rule #1:
never give up.
rule #2:
if you still want to, go to #1


Ack! You should know better than to use goto!
Code:
while (!given_up) {
    try_harder();
}



Reply With Quote
  #9  
Old January 20th, 2003, 11:42 AM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,969 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 39 m 55 sec
Reputation Power: 184
no, i meant:

while (given_up || !given_up) {
try_harder();
}


Reply With Quote
  #10  
Old February 1st, 2003, 11:34 PM
CStrauss CStrauss is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 37 CStrauss User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 3 m 50 sec
Reputation Power: 6
also to add to this post it is always more effiecent to pass by refrence rather then value. This is also helpful information cause if you need to pass an array you can only pass it by refrence. also when passing an array you must include the array size. okay that is my two cents on this topic

Reply With Quote
  #11  
Old February 2nd, 2003, 05:18 PM
ClayDowling ClayDowling is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Flint, MI
Posts: 328 ClayDowling User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 19 m 25 sec
Reputation Power: 6
Balance,

If you haven't studied pointers yet and still find a need to modify many variables in a function, there's a good chance that your program logic has problems.

There's a couple of good rules to stick by here. First, a function should only try to do one thing. Second, a function should be kept short. These two rules play nicely into each other.

It sounds like you are a student at University, so I'm going to suggest looking your professor or your TA up during office hours, bringing a recent homework assignment with you, and ask about better ways to structure your code. I make my living programming and I still talk with my boss and my colleagues about how to structure my programs.
__________________
Clay Dowling
Lazarus Notes
Articles and commentary on web development
http://www.lazarusid.com/notes/

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > returning variable*s* from functions?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights re