Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 15th, 2005, 9:41 AM   #1
Cipher
Hobbyist Programmer
 
Cipher's Avatar
 
Join Date: Feb 2005
Location: /home/cipher
Posts: 123
Rep Power: 4 Cipher is on a distinguished road
Send a message via AIM to Cipher Send a message via MSN to Cipher
Function Help(again sorry)

Sorry I am stuck on this guys. But specifically I am having trouble figureing out how functions return vaules.Could someone expalin to me the proceesss and maybe give some src concering funcs?
__________________
And there was much rejoicing... Yay....
Cipher is offline   Reply With Quote
Old Mar 15th, 2005, 10:42 AM   #2
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
int function(){

   do some stuff;
   return (int);

}

basically you declare the function as a type in the example above int, then you use the return command to return an int value, so you could have a function that returns a value you have added together, or -1 if there is an error or the number is invalid etc.

http://cplus.about.com/od/beginnerct.../aa061102a.htm

that might help i didnt read it but it should help i would write smoe more but i think someone can say it better esp in that tutorial.
Berto is offline   Reply With Quote
Old Mar 15th, 2005, 11:05 AM   #3
Xero
Hobbyist Programmer
 
Join Date: Dec 2004
Location: a cardboard box
Posts: 118
Rep Power: 4 Xero is on a distinguished road
To expand...

For example, you might want to create a function to calculate the area of a circle. The function would take in the radius as an arguement, and would return the area as return value to the what ever called the function. Returning values are used for if there was a calculation involved or if you want to signal a flag. The reason you would use a return value is because any changes made in the function locally, meaning they do not change the actual variables and are gone once the function ends. Returning a value helps retrieve calculations, etc. Ofcourse, there are alternative options such as pointers, but just ignore it for now as they would probably be even more confusing.
__________________
...
Xero is offline   Reply With Quote
Old Mar 15th, 2005, 3:12 PM   #4
Cipher
Hobbyist Programmer
 
Cipher's Avatar
 
Join Date: Feb 2005
Location: /home/cipher
Posts: 123
Rep Power: 4 Cipher is on a distinguished road
Send a message via AIM to Cipher Send a message via MSN to Cipher
hmm...I'll dwell on it..Once again..Thanks guys for the wonderful help/
__________________
And there was much rejoicing... Yay....
Cipher is offline   Reply With Quote
Old Mar 15th, 2005, 3:34 PM   #5
Benoit
Expert Programmer
 
Benoit's Avatar
 
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 555
Rep Power: 5 Benoit is on a distinguished road
When a function returns something its used like a variable, for example

x=sum(a,b);

int sum(int a, int b)
{
int sum;
sum=a+b;

return sum;
}

myfunction returns the value of sum and assigns it to variable x
__________________
Johnny was a chemist's son but Johnny is no more, for what Johnny thought was H2O was H2SO4
Benoit is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:10 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC