473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Void function returns value of 1000

It says all over that void f() means that it returns no value. But this function below does return a value to the main function which is 1000. Am I correct?

Basically void is taking place of a return type. Maybe it returns a value but no return type(ie. int, char, bool)?


Expand|Select|Wrap|Line Numbers
  1. void SetTo1000(int iaArray[], int iIndex) 
  2. {
  3.     iaArray[iIndex] = 1000;
  4. }
  5.  
  6. int main() 
  7. {
  8.     int iMyArray[5] = {-3,2,0,-1,5};
  9.     SetTo1000(iMyArray,3);
  10.     for (int iIndex=0; iIndex<5; iIndex++)
  11.     {
  12.         cout << iMyArray[iIndex] << endl;
  13.     }
  14.     return 0;
Apr 20 '10 #1

✓ answered by Banfa

No 1000 is not returned to main in the strictest sense of the word.

The return mechanism for any function is quite different to writing data to a memory location.

Your function does not return a value it returns void and so it does not return a value to main. It writes a value to a memory location that is accessible by main. The net result is that the value stored by the function is available in main but it has not been returned to main, it has been communicated to main by an alternate mechanism.

The actual return process often involves data being transferred on the stack as part of the stack unwinding that happens when a function returns.

10 2533
Dheeraj Joshi
1,123 Expert 1GB
Your code(function) do not return any value to main function.

Regards
Dheeraj Joshi
Apr 20 '10 #2
Hi Dheeraj

i see 1000 as part of the output. That 1000 gets returned from the function to main, no?
Apr 20 '10 #3
Hi Deanm007,
Your funtion will store the 1000 into iaArray[iIndex]. please find the below funtion for returning the int.



int funtion(int a)
{
a++;
return a ;
}
Apr 20 '10 #4
Dheeraj Joshi
1,123 Expert 1GB
Please read pass by reference and pass by value to understand why you get the value 1000 in your output.

Regards
Dheeraj Joshi
Apr 20 '10 #5
Banfa
9,065 Expert Mod 8TB
No 1000 is not returned to main in the strictest sense of the word.

The return mechanism for any function is quite different to writing data to a memory location.

Your function does not return a value it returns void and so it does not return a value to main. It writes a value to a memory location that is accessible by main. The net result is that the value stored by the function is available in main but it has not been returned to main, it has been communicated to main by an alternate mechanism.

The actual return process often involves data being transferred on the stack as part of the stack unwinding that happens when a function returns.
Apr 20 '10 #6
Dheeraj Joshi
1,123 Expert 1GB
Nice answer banfa, In fact i wanted to ans in same way, could not articulate it. Good explanation.

Regards
Dheeraj Joshi
Apr 20 '10 #7
@Banfa
Thanks a lot. that was very clear.

So you wouldn't need to pass in by reference to store a function's result in the memory, would you? Because in my code, the function would need ampersands like this:

Expand|Select|Wrap|Line Numbers
  1. # void SetTo1000(int& iaArray[], int& iIndex) 
  2. # {
  3. #     iaArray[iIndex] = 1000;
  4. # }
Apr 20 '10 #8
sridhar, dheeraj, thanks for responding
Apr 20 '10 #9
Banfa
9,065 Expert Mod 8TB
No you would need to pass a pointer to an array by reference because it is already a pointer, you would be unnecessarily dereferencing twice assuming you where just changing the contents of the array. Same for the size.

This should work just as well

Expand|Select|Wrap|Line Numbers
  1.  
  2. void SetTo1000(int iaArray[], int iIndex) 
  3. {
  4.     iaArray[iIndex] = 1000;
  5. }
  6.  
Note aiArray is a pointer to int (int *) with this syntax.
Apr 20 '10 #10
@Banfa
I got it, thank you.
Apr 21 '10 #11

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

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
3
by: Mariusz | last post by:
I want to write function to call another function which name is parameter to first function. Other parameters should be passed to called function. If I call it function('f1',10) it should call...
2
by: Highlander416 | last post by:
This is driving me crazy. I need to create a UDF that would return a TRUE/FALSE (bit) value based on a comparison it does. CREATE FUNCTION dbo.SelectedByApplication ( @ApplicationID int,...
4
by: Ken | last post by:
Hello I am trying to change the color of a font in a text box. I have been reading about and trying various examples found it this group, but still can't get it right. Here is where I am...
52
by: Douglas Garstang | last post by:
I can't believe I've been trying to work this out for hours now, and I can't believe I couldn't find someone asking for a similar solution in the newsgroups. No wonder I hate C so much, and every...
8
by: M. Moennigmann | last post by:
Dear all: I would like to write a function that opens a file, reads and stores data into an 2d array, and passes that array back to the caller (=main). The size of the array is not known before...
1
by: john | last post by:
Relatively new to C coding, so any help would greatly be appreciated. I'm having problems try to return my string array from my parsing function. When I do a printf I am getting the correct value...
29
by: whatluo | last post by:
Hi, c.l.cs I noticed that someone like add (void) before the printf call, like: (void) printf("Timeout\n"); while the others does't. So can someone tell me whether there any gains in adding...
5
by: Stijn van Dongen | last post by:
A question about void*. I have a hash library where the hash create function accepts functions unsigned (*hash)(const void *a) int (*cmp) (const void *a, const void *b) The insert function...
5
by: Giannis Papadopoulos | last post by:
I have the following code #include <stdio.h> void a(void) { printf("a called.\n"); } int b(void) { printf("b called.\n");
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.