By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,998 Members | 2,825 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,998 IT Pros & Developers. It's quick & easy.

What does it mean when a function returns an argument?

P: 7
I understand that when a function takes an argument, there is an argument called to the function, which it then manipulated by/put through the function. I do not really understand what it means if a function takes and returns arguments... Does it mean that it gives a value back? But don't functions that take no arguments also take a given something and spit something out?
Jan 5 '17 #1

✓ answered by stdq

Hi! A function may, or may not, return something to the callee. This "something" might be, coincidentally, one of the arguments the function received when it was called. It is also possible that a function takes no arguments and returns something, for example the rand() function, which generates pseudorandom numbers.

Share this Question
Share on Google+
3 Replies


P: 94
Hi! A function may, or may not, return something to the callee. This "something" might be, coincidentally, one of the arguments the function received when it was called. It is also possible that a function takes no arguments and returns something, for example the rand() function, which generates pseudorandom numbers.
Jan 5 '17 #2

P: 7
Ok so a function returns nothing when there is just something done? Like something is printed? If it is calculated and printed within the function does that mean it returns nothing even then? And it only returns something when some kind of data or value that has been manipulated within the function is returned to main()? Oh and if it takes no arguments there's no need for a value to be entered into the parameters? That makes a lot more sense if I am understanding this correctly! Thank you!
Jan 5 '17 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
Functions don't return arguments. But they do return data to the calling function by using an argument.

Case A: Provide the Address of a Variable in the Calling Function:


Expand|Select|Wrap|Line Numbers
  1. void CallingFunction()
  2. {
  3.  
  4.     int a = 0;  
  5.     CalledFunction(&a);
  6.  
  7.  
  8. }
  9.  
  10. void CalledFunction(int* result)
  11. {
  12.     *result = 10;  << this goes into the calling function variable.
  13.  
  14. }
  15.  
Case B: Provide the address of a Pointer to the called function:

Expand|Select|Wrap|Line Numbers
  1. void CallingFunction()
  2. {
  3.     char* str = 0;
  4.     CalledFunction(&str);
  5.  
  6.  
  7. void CalledFunction(char** result)
  8. {
  9.    *result = malloc(100 * sizeof(char));  << the memory address is in the calling function pointer
  10.  
  11.    strcpy(*result, "May the force be with you.");  << the called function copies into that memory.
  12. }
  13.  
Notice in these cases the called function has no return statement. The return staement is only to place a value in the return type of the function. Using the return type is only meaningful of the called function is on the right side of an assignment operator.
Jan 6 '17 #4

Post your reply

Sign in to post your reply or Sign up for a free account.