473,322 Members | 1,778 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,322 software developers and data experts.

Function confusion

Sheepman
I write most of my programs using only void function. They end up long and bloated because I often write the same code over and over again in each void function. I'm even confused by void functions though. The book says void functions don't return data but I make them do it. I'm using a void function to send data to a file and the screen. I use a void function to create the random number array. Then the other void functions use the random array, like sort it low to high. So I know they return data. In this program I created a 10 x 10 display twice and wrote to sceen and file twice. If I'm doing the same thing twice I know you can send the data i.e. print to file twice ,once sorted, once unsorted. Whenever I try and do function calls I end up a huge amount of errors. Also I'm creating the 10 x 10 display twice. Can't I somehow send the unsorted and sorted data to the same fuction to be formated and that be returned for display. Below are the functions without the statements. In main I have commented on what each function call does.

Expand|Select|Wrap|Line Numbers
  1. //Function Prototypes
  2.  
  3. void randNum(int[]);
  4. void bubbleSort(int[]);
  5. void show(int []);
  6. void tenByTen(int []);
  7.  
  8. //Called Functions
  9.  
  10. void randFunc(int firstArray[])
  11. {
  12. Statement
  13. }
  14. void bubbleSort(int firstArray[])
  15. {
  16. Statement
  17. }
  18. void tenByTen(int firstArray[])
  19. {
  20. Statement 
  21. }
  22.  
  23. int main()
  24. {
  25. srand((unsigned)time(0));//Seed random number generator
  26. int firstArray[100];//initialize array
  27.  
  28. randFunc(firstArray);//call random number genertor function, fill array, print unsorted data to screen and file in 10 x 10 format
  29.  
  30.  
  31. bubbleSort(firstArray);//Bubble sort array, lowest to highest
  32. tenByTen (firstArray);// Format sorted numbers in 10 x 10 display, print to screen and append to same file
  33. cout << endl;
  34. return 0;
  35. }
  36.  
No one in my class asks any questions, but when I ask them whats going on they say they can't explain it only do it! They just keep compiling it and correcting the errors until it works, crazy!
Jul 10 '07 #1
10 1584
scruggsy
147 100+
I'm a little confused myself, now. ;)
I write most of my programs using only void function. They end up long and bloated because I often write the same code over and over again in each void function.
That's usually a hint that you should create a separate function to handle that repeated code.
I'm even confused by void functions though. The book says void functions don't return data but I make them do it. I'm using a void function to send data to a file and the screen. I use a void function to create the random number array. Then the other void functions use the random array, like sort it low to high. So I know they return data.
No, they don't return data. They operate on the array you sent as an argument to the function. Which is fine - judging by your code, that's exactly what you want. But they don't return anything.
In this program I created a 10 x 10 display twice and wrote to sceen and file twice. If I'm doing the same thing twice I know you can send the data i.e. print to file twice ,once sorted, once unsorted. Whenever I try and do function calls I end up a huge amount of errors.
Post the text of the errors.
Below are the functions without the statements. In main I have commented on what each function call does.
Why don't you post the full code?
Jul 11 '07 #2
I'm a little confused myself, now. ;)


Why don't you post the full code?
The forum posting rules said not to, is that incorrect? That's why I've been asking questions using only pieces of it? It would make it easier for me!
Jul 11 '07 #3
scruggsy
147 100+
The forum posting rules said not to, is that incorrect? That's why I've been asking questions using only pieces of it? It would make it easier for me!
Oh, good point. :heh:
You can PM it to me if you'd like.
Jul 11 '07 #4
The forum posting rules said not to, is that incorrect? That's why I've been asking questions using only pieces of it? It would make it easier for me!
I believe you are supposed to post your code when asking questions. But people aren't supposed to post full code answers. Meaning you can't ask how to do this and have someone give you all of the code to use. They can point out the errors in your code and and correct your code.

How to ask a question
Please follow these guidelines when posting questions as submitting clear and concise questions allows those reading to understand your problem and respond more easily.

* Post your question in a relevant forum
* Make sure you are not posting in an Articles section
* Give the relevant Platform, OS and Version information
o In the C/C++ forum this might be Intel/Windows/MSVC++ 6.0 or StrongArm/ThreadX/Greenhills C/C++
o In Access it might just be the version of Access you are using 97, 2003 etc.
* Give as much detail as possible When you post a question or problem, express the situation clearly and concisely and include all relevant information, code used, data used, result expected, result achieved and any error codes or messages that you get as a result.
* Use Clear English to write your question in if possible, try to avoid using abbreviations
* Do not use leet speak or text speak, they are not Clear English
* Make use of the available formatting tags in the forum Use CODE tags around your code as appropriate:
This will preserve white space and use a mono-spaced font making the code easier to read.
* Do not ask people to reply by email or follow up answers with a PM
* Please don't say that a problem is urgent if it's not. If your problem is time sensitive, please give specific time frames e.g. In 3 days, within 24 hours.
* Please try to read your own post after you've posted it. If you can't make any sense of it, it's a fair bet that our experts will struggle and waste time just trying to understand what you're trying to say. Also, if you get bored when you're half-way through reading it because it rambles on endlessly, how will our experts fare?
* If you wish to post a question do not post it in a discussion created by someone else unless it is about exactly the same problem. Please start a new discussion.
Jul 11 '07 #5
ilikepython
844 Expert 512MB
I write most of my programs using only void function. They end up long and bloated because I often write the same code over and over again in each void function. I'm even confused by void functions though. The book says void functions don't return data but I make them do it. I'm using a void function to send data to a file and the screen. I use a void function to create the random number array. Then the other void functions use the random array, like sort it low to high. So I know they return data. In this program I created a 10 x 10 display twice and wrote to sceen and file twice. If I'm doing the same thing twice I know you can send the data i.e. print to file twice ,once sorted, once unsorted. Whenever I try and do function calls I end up a huge amount of errors. Also I'm creating the 10 x 10 display twice. Can't I somehow send the unsorted and sorted data to the same fuction to be formated and that be returned for display. Below are the functions without the statements. In main I have commented on what each function call does.

Expand|Select|Wrap|Line Numbers
  1. //Function Prototypes
  2.  
  3. void randNum(int[]);
  4. void bubbleSort(int[]);
  5. void show(int []);
  6. void tenByTen(int []);
  7.  
  8. //Called Functions
  9.  
  10. void randFunc(int firstArray[])
  11. {
  12. Statement
  13. }
  14. void bubbleSort(int firstArray[])
  15. {
  16. Statement
  17. }
  18. void tenByTen(int firstArray[])
  19. {
  20. Statement 
  21. }
  22.  
  23. int main()
  24. {
  25. srand((unsigned)time(0));//Seed random number generator
  26. int firstArray[100];//initialize array
  27.  
  28. randFunc(firstArray);//call random number genertor function, fill array, print unsorted data to screen and file in 10 x 10 format
  29.  
  30.  
  31. bubbleSort(firstArray);//Bubble sort array, lowest to highest
  32. tenByTen (firstArray);// Format sorted numbers in 10 x 10 display, print to screen and append to same file
  33. cout << endl;
  34. return 0;
  35. }
  36.  
No one in my class asks any questions, but when I ask them whats going on they say they can't explain it only do it! They just keep compiling it and correcting the errors until it works, crazy!
A void function has no return value. Even though you are changing arrays in your function you are not returning a variable to the caller (using the return keyword). For example, if you want to create a function that doubles a certain variable, it would be simpler to return the doubled variable.:
Expand|Select|Wrap|Line Numbers
  1. int Double(int x)
  2. {
  3.     return x * 2    //returns x * 2 to the caller
  4. }
  5. // ...code
  6. int x = 9;
  7. int doublex = Double(x);  // return value gets stored in doublex
  8.  
If you use void, you would have to pass by reference which is unneccesary in this case.
Jul 11 '07 #6
Here's the whole thing. Everything is working except the second write to file. I get the sorted data on the screen but the text file isn't appended with the sorted data. It only contains the unsorted data. The error is located in the function tenByTen just above main

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <cstdlib>
  5. #include <ctime>
  6. using namespace std;
  7. void randNum(int[]);
  8. void bubbleSort(int[]);
  9. void show(int []);
  10. void tenByTen(int []);
  11. void randFunc(int firstArray[])
  12. {
  13. int min = -50;
  14. int max = 50;
  15. int randNum;
  16. for (randNum = 0; randNum < 100; randNum++)
  17. {
  18. firstArray[randNum] = rand()%(max - min + 1) + min;
  19. cout << setw (5) << firstArray[randNum] ;
  20. //cout << endl;
  21. }//Close for
  22. char yourFileName [80];
  23. int d1, d2;
  24. //Prompt user for file name to store array
  25. cout << "Enter name of file to store your randomly generated numbers: " << endl;
  26. cin >> yourFileName;
  27. ofstream outfile (yourFileName, ios::out);//create and open file
  28. if (!yourFileName)//errorcheck
  29. {
  30. cout << "Error opening your file " << endl;
  31. }//close error check
  32. //fomat array output to 10 x 10
  33.  
  34. for (d1 = 0; d1 <= 9; d1++)
  35. {
  36. for (d2 = 0; d2 <= 9; d2++)
  37. {
  38. cout << setw(5) << firstArray[(d1 * 10) + d2];//write to screen
  39. outfile << setw(4) << firstArray[(d1 * 10) + d2];//write to file 
  40. }//close nested for
  41. cout << endl;
  42. outfile << endl;
  43. }//close for
  44. cout << endl;
  45. cout << endl;
  46. outfile.close();
  47. }//close tenBy
  48.  
  49. void bubbleSort(int firstArray[])
  50. {
  51.  
  52. for (int pass = 1; pass < 100; pass++)
  53. {
  54. for (int randNum = 0; randNum < 100 - pass; randNum++)
  55. {
  56. if (firstArray[randNum] > firstArray[randNum + 1])
  57. {
  58. int swap = firstArray[randNum];
  59. firstArray[randNum] = firstArray[randNum + 1];
  60. firstArray[randNum + 1] = swap;
  61.  
  62. }//close if
  63. }
  64. }
  65. }
  66.  
  67. void tenByTen(int firstArray[])
  68. {
  69. char yourFileName [80];
  70. int d1, d2;
  71. //Prompt user for file name to store array
  72. //cout << "Enter name of file to store your randomly generated numbers: " << endl;
  73. //cin >> yourFileName;
  74. ofstream outfile;//create and open file
  75. outfile.open("yourFileName", ios::app);
  76. if (!yourFileName)//errorcheck
  77. {
  78. cout << "Error opening your file " << endl;
  79. }//close error check
  80. //fomat array output to 10 x 10
  81.  
  82. for (d1 = 0; d1 <= 9; d1++)
  83. {
  84. for (d2 = 0; d2 <= 9; d2++)
  85. {
  86. cout << setw(5) << firstArray[(d1 * 10) + d2];//write to screen
  87. outfile << setw(4) << firstArray[(d1 * 10) + d2];//write to file 
  88. }//close nested for
  89. cout << endl;
  90. outfile << endl;
  91. }//close for
  92. outfile.close();
  93. }//close tenBy
  94.  
  95. int main()
  96. {
  97. srand((unsigned)time(0));//Seed random number generator
  98. int firstArray[100];//initialize array
  99.  
  100. randFunc(firstArray);//call random number genertor function, fill array 
  101. bubbleSort(firstArray);//Bubble sort array, lowest to highest
  102. tenByTen (firstArray);// Format display in 10 x 10 
  103. cout << endl;
  104. return 0;
  105. }
  106.  
My orginal idea was to create a random generating function, supply the output to a function that created a 10 x 10 output. Have that sent to a sort and print function. Then have the sorted data sent to the same print function. Couldn't even get close with that setup.
Jul 11 '07 #7
well, for one thing in your tenByTen function you never define the yourFileName array. So it's probably writing the sorted list to a file with a name composed of the garbage data that the array contains.
Jul 11 '07 #8
JosAH
11,448 Expert 8TB
I write most of my programs using only void function. They end up long and bloated because I often write the same code over and over again in each void function.
One of my kazillion rules of thumb says this:

"Never write a function you can't explain in one clear sentence"

In other words: a function should do one single thing and it should do it well.
It may be necessary to return a value from a function; otherwise if you want
Basic, you know where to find it.

Repeat the rule above until you end up with a bunch of functions that do the job.

kind regards,

Jos
Jul 11 '07 #9
well, for one thing in your tenByTen function you never define the yourFileName array. So it's probably writing the sorted list to a file with a name composed of the garbage data that the array contains.
I thought that's what the
Expand|Select|Wrap|Line Numbers
  1. char yourFileName [80];
  2.  
was doing. The more I think I'm figuring it out...the more I'm realize how much I don't know. Isn't it also defined in main? Should I do it globally so everything sees it.? I tried changing the functions to do just specific things and send it to the other functions. Now it doesn't compile anymore, currently trying to back to the way it was. I'm just gonna figure out how to define it in tenByTen and leave it alone.

Does anyone know of a really good tutorial that explains how functions operate, where the data goes when called and returning data for these types types of functions
1. void with no parameters
2. void with parametes
3. type with no parameters
4. type with parameters

the bottom line is I have to figure this out to do the other stuff. This is currently a huge stumpling block for me!
Jul 12 '07 #10
I thought that's what the
Expand|Select|Wrap|Line Numbers
  1. char yourFileName [80];
  2.  
was doing. The more I think I'm figuring it out...the more I'm realize how much I don't know. Isn't it also defined in main? Should I do it globally so everything sees it.? I tried changing the functions to do just specific things and send it to the other functions. Now it doesn't compile anymore, currently trying to back to the way it was. I'm just gonna figure out how to define it in tenByTen and leave it alone.

Does anyone know of a really good tutorial that explains how functions operate, where the data goes when called and returning data for these types types of functions
1. void with no parameters
2. void with parametes
3. type with no parameters
4. type with parameters

the bottom line is I have to figure this out to do the other stuff. This is currently a huge stumpling block for me!
That line doesn't DEFINE the array, it only DECLARES the array. In order to DEFINE it you must store a value in it, in other words you must set it equal to something.

It doesn't matter that you declared and defined it in main, that yourFileName array is local to main, and doesn't exist in any of the functions.

I would suggest making that array global, that way it can be used by all of your functions. To do that, you put its declaration (char yourFileName [80];) outside of main, up with your function prototypes. Then you do NOT declare it anywhere else.

Also, you didn't define it in main, you defined it in the randFunc function. simply remove the declaration line from both/all functions and put it with the prototypes (outside of all functions) and then the input statement in randFunc will store the file name in it, which will be usable in all functions.

Also, I noticed that in your tenByTen function, that your open file statement doesn't even use the yourFileName array, you put quotes around it, so your sorted array is probably being stored in a file named yourFileName.

Here I edited your code to use a global array, and I formated it a bit:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <cstdlib>
  5. #include <ctime>
  6.  
  7. using namespace std;
  8.  
  9. void randNum(int[]);
  10. void bubbleSort(int[]);
  11. void show(int []);
  12. void tenByTen(int []);
  13.  
  14. char yourFileName [80];//makes a global array
  15.  
  16. void randFunc(int firstArray[])
  17. {
  18.     int min = -50;
  19.     int max = 50;
  20.     int randNum;
  21.  
  22.     for (randNum = 0; randNum < 100; randNum++)
  23.     {
  24.         firstArray[randNum] = rand()%(max - min + 1) + min;
  25.         cout << setw (5) << firstArray[randNum] ;
  26.         //cout << endl;
  27.     }//Close for
  28.  
  29.     int d1, d2;
  30.  
  31.     //removed array declaration
  32.  
  33.     //Prompt user for file name to store array
  34.     cout << "Enter name of file to store your randomly generated numbers: " << endl;
  35.     cin >> yourFileName;
  36.     ofstream outfile (yourFileName, ios::out);//create and open file
  37.  
  38.     if (!yourFileName)//errorcheck
  39.     {
  40.         cout << "Error opening your file " << endl;
  41.     }//close error check
  42.  
  43.     //fomat array output to 10 x 10
  44.     for (d1 = 0; d1 <= 9; d1++)
  45.     {
  46.         for (d2 = 0; d2 <= 9; d2++)
  47.         {
  48.             cout << setw(5) << firstArray[(d1 * 10) + d2];//write to screen
  49.             outfile << setw(4) << firstArray[(d1 * 10) + d2];//write to file 
  50.         }//close nested for
  51.  
  52.         cout << endl;
  53.         outfile << endl;
  54.     }//close for
  55.  
  56.     cout << endl;
  57.     cout << endl;
  58.     outfile.close();
  59. }//close randFunc
  60.  
  61. void bubbleSort(int firstArray[])
  62. {
  63.     for (int pass = 1; pass < 100; pass++)
  64.     {
  65.         for (int randNum = 0; randNum < 100 - pass; randNum++)
  66.         {
  67.             if (firstArray[randNum] > firstArray[randNum + 1])
  68.             {
  69.                 int swap = firstArray[randNum];
  70.                 firstArray[randNum] = firstArray[randNum + 1];
  71.                 firstArray[randNum + 1] = swap;
  72.             }//close if
  73.         }
  74.     }
  75. }//close bubbleSort
  76.  
  77. void tenByTen(int firstArray[])
  78. {
  79.     //removed the declaration of the array
  80.  
  81.     int d1, d2;
  82.  
  83.     ofstream outfile;//create and open file
  84.  
  85.     //outfile.open("yourFileName", ios::app);what’s up with the quotes????
  86.     outfile.open(yourFileName, ios::app);//this is what I think you meant
  87.  
  88.  
  89.     if (!yourFileName)//errorcheck
  90.     {
  91.         cout << "Error opening your file " << endl;
  92.     }//close error check
  93.  
  94.     //fomat array output to 10 x 10
  95.     for (d1 = 0; d1 <= 9; d1++)
  96.     {
  97.         for (d2 = 0; d2 <= 9; d2++)
  98.         {
  99.             cout << setw(5) << firstArray[(d1 * 10) + d2];//write to screen
  100.             outfile << setw(4) << firstArray[(d1 * 10) + d2];//write to file 
  101.         }//close nested for
  102.  
  103.         cout << endl;
  104.         outfile << endl;
  105.     }//close for
  106.  
  107.     outfile.close();
  108. }//close tenByTen
  109.  
  110. int main()
  111. {
  112.     srand((unsigned)time(0));//Seed random number generator
  113.     int firstArray[100];//initialize array
  114.  
  115.     randFunc(firstArray);//call random number genertor function, fill array 
  116.     bubbleSort(firstArray);//Bubble sort array, lowest to highest
  117.     tenByTen (firstArray);// Format display in 10 x 10 
  118.     cout << endl;
  119.     return 0;
  120. }
Jul 12 '07 #11

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

Similar topics

8
by: kalinga1234 | last post by:
there is a problem regarding passing array of characters to another function(without using structures,pointer etc,).can anybody help me to solve the problem.
15
by: phil-news-nospam | last post by:
This question is borderline between language and programming, but I want to focus more on the language standards issue, rather than the programming issue, so I am posting here. I have a number...
38
by: maadhuu | last post by:
does it make sense to find the size of a function ??? something like sizeof(main) ??? thanking you ranjan.
4
by: Ranginald | last post by:
Sorry for the simple question but thanks in advance: My goal is to create reusale code for a web app in C#. I have written the code already as a windows app but here is where I am confused: ...
1
by: Richard Lewis Haggard | last post by:
I'm having a problem with what appears to be some sort of confusion with references. I have a single solution with a dozen projects which has been working quite nicely for a while. The references...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
4
by: Tony Lownds | last post by:
(Note: PEPs in the 3xxx number range are intended for Python 3000) PEP: 3107 Title: Function Annotations Version: $Revision: 53169 $ Last-Modified: $Date: 2006-12-27 20:59:16 -0800 (Wed, 27 Dec...
7
by: siddhu | last post by:
Dear Experts, I have a question. I wrote the following code class A { public: void g(){}
21
by: globalrev | last post by:
i have a rough understanding of lambda but so far only have found use for it once(in tkinter when passing lambda as an argument i could circumvent some tricky stuff). what is the point of the...
8
by: vaib | last post by:
hi all , It really seems that C never ceases to amaze . All this time i've been doing C and i thought i was quite adept at it but i was wrong . So without wasting any more time , here's the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...

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.