return function

what is its real purpose?

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 787 Views - Last Post: 09 June 2009 - 05:54 AM Rate Topic: -----

#1 DingleNutZ  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 121
  • Joined: 02-May 09

return function

Posted 07 June 2009 - 10:12 PM

what is the real purpose of the return function?
its always there, like your mum if your a mummies boy for example.

so what does it really do?
reward for the best answer.
Is This A Good Question/Topic? 0
  • +

Replies To: return function

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4316
  • View blog
  • Posts: 12,096
  • Joined: 18-April 07

Re: return function

Posted 07 June 2009 - 10:28 PM

First of all, it is not a function. It is statement. This means that it is a command rather than a series of commands which can be called like that of a function. Typically you find return statements inside of functions and their job is to take a value and "return it to the caller of the function".

If a function says that it will return an integer as a result of an add computation you may see something like..

// Function with return statement
int add(int number1, int number2) {
	 return number1 + number2;
}

// Caller
int result = add(5, 3);



So here the return statement will take the two numbers, add them together and RETURN the value to the waiting variable "result". In this instance, it will return the value 8 to the caller which will take the value and place it into the variable "result".

You can have multiple return statements in a function, but not all may be encountered. The first return statement encountered during the execution of the function will immediately end the function's execution and return the value to the caller.

int add(int number1, int number2) {
	 if (number1 == 3) {
		 return number1;
	 }
	 return number1 + number2;
}



In this instance if the caller specifies the first parameter as 3 it will return the value 3 and the second return statement will never be executed. If the first parameter is not 3 then the second return statement will execute and return the sum... having never executed the first return statement inside the if statement.

Hope this is all clear. For more information just look on the web for "return statement" and you will find tons of stuff talking about it.

Enjoy!

"At DIC we be return mastering code ninjas... we have gone to hell, taken it over and now returned" :snap:
Was This Post Helpful? 1
  • +
  • -

#3 DingleNutZ  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 121
  • Joined: 02-May 09

Re: return function

Posted 07 June 2009 - 10:35 PM

thankyou ur reward is getting thanked
Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: return function

Posted 07 June 2009 - 10:40 PM

EDIT: DingleNutZ responded while I was typing and made my response irrelevant.

This post has been edited by janotte: 08 June 2009 - 02:10 AM

Was This Post Helpful? 0
  • +
  • -

#5 Elcric  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 101
  • View blog
  • Posts: 453
  • Joined: 02-May 09

Re: return function

Posted 08 June 2009 - 12:16 AM

:D Hello,

Return is for error messages.

Passing information by reference does what most programmers think of when return is discussed. Passing arrays by reference allows more than one data element to be "returned" by a function. This leaves return where it belongs, for error messages.

However, you do not see return used for error messages as often as you should. Because most programmers consider errors to be of the throw catch type exceptions. Errors are also user input. Most users do not appear to know how to read, and they make input errors that have to be caught by the program.

My vote is for error messages.
Was This Post Helpful? 0
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: return function

Posted 08 June 2009 - 05:23 AM

Wait... Where is the button for "This post was Unhelpful"? :P
Was This Post Helpful? 1
  • +
  • -

#7 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: return function

Posted 08 June 2009 - 05:30 AM

View PostNickDMax, on 8 Jun, 2009 - 04:23 AM, said:

Wait... Where is the button for "This post was Unhelpful"? :P


:^: :D :^: :D :^: :D :^: :D :^: :D :^: :D :^: :D :^: :D :^: :D :^:
Was This Post Helpful? 1
  • +
  • -

#8 Elcric  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 101
  • View blog
  • Posts: 453
  • Joined: 02-May 09

Re: return function

Posted 08 June 2009 - 06:05 AM

:D :D :D :D :D :D :D :D :D :D

It might not have been the best answer but it deserves a prize for being the most creative!
:D :D :D :D :D :D :D :D :D :D
Was This Post Helpful? 0
  • +
  • -

#9 Zerobu  Icon User is offline

  • Black Hatter

Reputation: 13
  • View blog
  • Posts: 1,822
  • Joined: 14-January 08

Re: return function

Posted 08 June 2009 - 06:24 AM

Return simply ends a function and it returns whatever value that corresponds to the function type, if the function is void nothing is returned, if int, then int is return, if string, then string is returned.

This post has been edited by Zerobu: 08 June 2009 - 06:27 AM

Was This Post Helpful? 0
  • +
  • -

#10 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: return function

Posted 08 June 2009 - 06:25 AM

a return "function" which you called it is actually a return statement as Martyr said and the purpose of it is to do something within a function which will return it for general use. you would be able to use it as another int/string/char depending on what kind it is. the point is that you would be returning an object and you would be able to use it in pretty much any way you wish.
Was This Post Helpful? 0
  • +
  • -

#11 Elcric  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 101
  • View blog
  • Posts: 453
  • Joined: 02-May 09

Re: return function

Posted 08 June 2009 - 06:34 AM

:D

This one is better!

http://tbn2.google.com/images?q=tbn:jZ6WUZsxZFPlrM:http://www.cartoonstock.com/newscartoons/cartoonists/mly/lowres/mlyn193l.jpg
Was This Post Helpful? 0
  • +
  • -

#12 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: return function

Posted 08 June 2009 - 06:37 AM

1-this is a help thread, so let's try not to make it into a bump thread
2-this isn't the amusing pictures thread
Was This Post Helpful? 1
  • +
  • -

#13 DingleNutZ  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 121
  • Joined: 02-May 09

Re: return function

Posted 08 June 2009 - 05:30 PM

so, does it actually matter if this statement is excluded?
i never put it in because it doesnt make any difference. should i get into the habit of using it?
Was This Post Helpful? 0
  • +
  • -

#14 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: return function

Posted 08 June 2009 - 05:37 PM

It depends what your trying to accomplish.

For example, a function to add two integers and return the result would need a return statement
int add(int a, int y)
{
       return a+y;
}



Where as, if you wanted to print out the value of the two from within the function, a return would not be needed

void add(int a, int y)
{
        printf("%d", a+y); //EDIT: had to change int b to int y because it was showing as a smiley :D/>
}


The first example is better practice however, so that the function is reusable.

This post has been edited by erik.price: 08 June 2009 - 05:40 PM

Was This Post Helpful? 0
  • +
  • -

#15 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: return function

Posted 08 June 2009 - 06:05 PM

Well you should include it every time you are trying to return a value. You should also include it in main since main does return a value.

Note that (at least on windows) every program exits with an "exit code" that is sometimes used to make decisions based upon the results of a program.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2