RSS Forums RSS
Please support our C advertiser: Programming Forums

home work help funcations and arry issues

Join Date: Jul 2005
Posts: 1,353
Reputation: Lerner is just really nice Lerner is just really nice Lerner is just really nice Lerner is just really nice 
Rep Power: 9
Solved Threads: 182
Lerner Lerner is offline Offline
Nearly a Posting Virtuoso

Re: home work help funcations and arry issues

  #4  
Jul 20th, 2006
The function prototype tells the compiler that there will be a function of a given name using a given number and type of parameters that will be presented in a given order and that the function will return a given type. The definition, which can be placed before main() is declared, if you wish, instead of the prototype, will include all of that information and will include names of all of the parameters as well as the function body.

The function will do whatever it is told to do in the body of definition to the parameters it is passed and return whatever it is told to.

In my example:

int readStudentDataFromFile(FILE *, char &, int &);

is the prototype. With this information the compiler knows that the function readStudentDataFromFile() will take three parameters. The first parameter will be a pointer to a struct of type FILE, the second parameter will be a reference to a char, and the third will be a reference to an int. In addition the function will return an int. At this point, there is no way to know exactly what the function will do. That information is what is in the function body.

The following is the definition of the readStudentDataFromFile() function.

int readStudentDataFromFile(FILE * filePtr, char & status, int & credits)
{
int numb= fscanf(filePtr, "%c %d", &status, &credits);
return numb;
}

Now each of the parameters is given a name. Within the opening { and the closing } are instructions as to what the function will do with the parameters. In this case all three of the parameters passed to readStudentDataFromFile() are passed to the fscanf() function. In turn, fscanf() will read the contents of the file that was previously opened and asssociated with filePtr. fscanf() will put the first token it finds in the file into the address of the parameter called status and it will put the second token in the file into the address of the parameter called credits. fscanf() will return the number of variables it read in which is then stored in numb. numb is then returned from readStudentDataFromFile() back to where ever it was called from. The return value can be used for whatever purpose you want, including ignoring it, but I used it as a check to help ensure proper reading of the file.

In main() I told the program to call readStudentDataFromFile() when I wrote this:

numberRead = readStudentDataFromFile(filePtr, status, credits);

This says to pass the variables filePtr, status, and credits to readStudentDataFromFile() and that the return value of readStudentDataFromFile(), that is numb, will be assigned to numberRead.

The while loop in main says if numberRead doesn't equal 2 then fscanf didn't read stuff from the file correctly so readStudentDataFromFile() should stop being called. Hopefully, that doesn't happen until the end of the file. If numberRead wasn't 2 you could check it's value. If numberRead equals EOF, then you are at the end of the file and alls well. If numberRead is anything but 2 or EOF, then the file is probably corrupt. I didn't do those steps, however. In my code, I just left it that if numberRead is 2 I will confirm what data was read in with the printf() statements and pause the program long enough so I can read them and check them against the data in my test file.

If everything checks out to this point then I know I can: open and associate the correct file with my program and I can read data from the file in the way I want. Now I can go on to deal with that information according to the instructions given by the instructor.

Note that all three parameters passed to readStudentDataFromFile() were passed by reference. This means that whatever is done to them in readStudentDataFromFile() will be visible back in main(). So in this example where the file read had the first student data of U 9, the value of status back in main() will be U and the value of credits back in main() will be 9, even though the value of those variables was determined in readStudentDataFromFile() which in turn really got the data from fscanf(), again, by passing the parameters to fscanf() by reference, rather than by value. I don't think any of the other functions you will need to write for this program will embed a function within a function like readStudentDataFromFile() did, but it is a very common practice.
Last edited by Lerner : Jul 20th, 2006 at 6:52 pm.
Reply With Quote  
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 6:50 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC