Question about Using Function to Make File Input

I have a quick question about this. For a small assignment using Visual Basic I need to use a function to call in a few pieces of data from a file. What would I name this function, I'm not sure of the verbage I should use, but I'm talking about the word before the actual name of the function
IE [ float computeFinalPrice(int price, float tax) ]

What would i use for the float part, it is all data so due to this I'm thinking to use float, but I need to read in the data from a file. It's actually 4 pieces of data that I'm going to put into arrays. I don't need a function for each piece of data do I? If I don't then do I return each array? Is it possible to return multiple things from a single function? Additionally, could I just call it in to a single array and then from that array in the main function separate it using a loop to the 4 separate arrays since it is arranged in order?

I hope I posted in the right section, and I hope that these questions not only make sense, but are not stupid. I'm just beginning to learn and any help would be greatly appreciated!
Last edited on
I am assuming that when you say: "using Visual Basic" you mean the MSVS IDE not the Visual Basic language.

That's called the 'datatype' or sometimes just the 'type'. It has nothing to do with the actual name of the funcion, rather it defines the datatype of the variable that is to be returned from the function.

No, you don't need a function for each piece of data. The whole point of a function is to cut down on redundancy in code like this.

You could return the array or you could work with the data inside of the function, that's entirley up to you. I should have said you could pass the array by reference, and make the modifications to it in the function. These changes would be reflected on the array you passed in.

Yes it is possible to return multiple variables from a function. This is most easily done by passing arguments in by reference. This way changes are made to the arguments not copies of them.

Additionally, could I just call it in to a single array and then from that array in the main function separate it using a loop to the 4 separate arrays since it is arranged in order?

You lost me here, please rephrase this part of your question.
Last edited on
First off, thank you for the timely response!

Yes, I'm sorry! Using the IDE. Thank you very much for the clarification on the dataype, I understood the idea but did not no the correct name for it.

That's what I thought about the functions, to cut things down and to somewhat help organize the program. Also, if I were to return multiple arrays from a function how would i go about doing so? If I return them to the main program, what would I do to retrieve them inside of the main program? When you say it is most easily done by passing arguments in by reference, what exactly does that mean? How would it look in code?

I'm sorry about that question... Basically what I mean is, could I return a single array with all of the information from the file to the main program? Then after doing so, could I take that array and separate it into the 4 separate arrays that would be used for the rest of the program? I believe I know how I would do this, but I'm wondering if this is the most efficient way to go about doing this, and if not what the preferred way would be?

I hope that clarified that question a little better. I'm sorry for the confusion I created and once again, Thank You!
You wouldn't actually return the array, for this assignment I would just work with the array within the function. This is because by requiring you to use a function to read the data from the file, you're creating a new 'ifstream' object every time the function is called and technically destroying it when you exit the function. This makes keeping track of what piece of data to read in next more complicated then it has to be.

Actually with the description of the assignment that you gave me, you don't seem to do anything with the data so why do you want an array?
Sorry I gave a vague description of the assignment. Basically I need to write a program that reads from a file the customer number, height, weight, and age of 10 individuals. I then need to compute clothing sizes for each individual with given formulas. I need to compute hat size, jacket size, and pant size. And I was asked to use a function for each calculation, as well as a function to read in the data from a file and a function to print the results to an output file.

By no means am I asking for the program to be written for me! I think that's why I was so vague with the description. I really want to figure it out, but I think I need a few tips, pointers and some help with it all.

All Main would do is call Function 1 and return 0; to the OS.

Function 1 would read the data from a file in a loop passing each persons relavent data to the Function that does the appropriate calcuation and save the value that was returned to another variable. Then pass that Variable to Function 6 for output.

Function 2-4 would take the arguments from Function 1 and do the math with the given algorithms then Return the new data to Function 1.

Function 5 would output the data given to it from Function 1.

Last edited on
Topic archived. No new replies allowed.