![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
|
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.... |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
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. |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Dec 2004
Location: a cardboard box
Posts: 118
Rep Power: 4
![]() |
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.
__________________
... |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
|
hmm...I'll dwell on it..Once again..Thanks guys for the wonderful help/
__________________
And there was much rejoicing... Yay.... |
|
|
|
|
|
#5 |
|
Expert Programmer
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 556
Rep Power: 5
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|