Functions

P

PTS

I am working on a program that will calculate some numbers. As an
example, I will say student average test score and print out the grade
but my question is how do I put AVERAGE and test score and the other
parts, call the functions and where? How would I structure that
program? Would I declare each function before the MAIN() function,
then call them in the main. The program will read in a file and
generate an output file. How do I represent the student's name with a
character string? The input file example will have names, and their
test scores. How many functions?
 
M

Mike Wahler

PTS said:
I am working on a program that will calculate some numbers. As an
example, I will say student average test score and print out the grade
but my question is how do I put AVERAGE and test score and the other
parts, call the functions and where?

Define your objects (a.k.a. 'variables') at scopes where they
are visible to code that needs to refer to them. Define your
functions wherever you like (however, if you put calls to them
before their definitions (or if their definitions are in different
translation units), be sure to provide prototypes for them.
Call your functions at the points where their results (and/or
side-effects) are needed.
How would I structure that
program?

See above. You have quite a bit of flexibility for this; e.g.
you could put everything in one translation unit (esp. if the
program is small/simple), or you could group related functions
and definitions in their own translation units. Basically you
should structure your program so it's as easy to understand
as possible. As a beginner writing simple programs, it's
probably best to just put everything in one translation unit
to start with. Once you get it working, then you can experiment
with breaking things into separate translation units.
Would I declare each function before the MAIN() function,

If you like. But you don't have to. (See my remarks above
about prototypes, however). Also note that C++ is case-sensitive.
The entry point's name is 'main', not 'MAIN'.
then call them in the main.

Any function can call any other function in the same translation
unit, or those in other translation units which have external
linkage (the default for functions), they needn't all be
called from 'main()'. (However, this explanation is rather
simplified, read about 'namespaces' and how they affect
visibility of names. Also, I didn't address class member
functions, which might or might not be visible, depending
upon how their class is organized.)
The program will read in a file and
generate an output file. How do I represent the student's name with a
character string?

Define (or dynamically allocate) an array of characters of sufficient
size to contain the longest possible name. Caution must be used to
ensure that you don't access outside the array boundaries. This is
why I, and almost everyone else here will advise you to *not* use
character strings, but to use the 'std::string' type instead, which
relieves you of the responsibility of managing its memory.

The input file example will have names, and their
test scores. How many functions?

As many as you need. The general 'rule of thumb' is for each
function to perform only one task.

Finally, your questions lead me to ask "Where's your textbook(s)"? :)
Your questions are very basic.

A good guide for selecting textbooks is the book review
section at www.accu.org

A good supplement to (but certainly not a replacement for) textbooks
is the "C++ FAQ":
http://www.parashift.com/c++-faq-lite/

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,278
Latest member
BuzzDefenderpro

Latest Threads

Top