Function headers

Can someone explain the difference to me about the two function headers with the float & amtC and the float amtC
Consider the following variable definitions and function call:

1
2
3
4
5
string customer = "Customer1";
float balance;
float amount = 72.56;
char type = S;
getBalance(customer, balance, type, 200.53);


Consider the two function headers below. For each header, say if you think the header for function getBalance is a valid header, and give reasons for your answer:

3. void getBalance (string customerP, float & balAmnt,char & typeC, float & amtC);
4. void getBalance (string customerP, float & balAmnt,char & typeC, float amtC);
Last edited on
both headers are valid but for POD like float it'd make more sense to pass by value:
http://stackoverflow.com/questions/2108084/pass-by-reference-more-expensive-than-pass-by-value
depending upon the context, it'd also make sense to const qualify variables wherever possible
Last edited on
Also, prefer to use double rather than float - if your assignment allows it. The precision of float is easily exceeded - it can only do 6 or 7 significant figures.

In this:

float amount = 72.56;

the 72.56 is actually a double (the default), so when it is assigned to float amount , there is an implicit cast to convert it to float. One can avoid that by doing float amount = 72.56f; , but it is better to just make the whole thing double in the first place.

Pass string by reference, probably const if it is not to be changed:

1
2
3
4
void getBalance (const std::string& customerP, 
                 double& Balance, 
                 const char typeC, 
                 const double amtC);


Notice I put the reference right next to the type, in C++ there are many who like to emphasise the type, as C++ is all about types. Also the way I have formatted the code when there are multiple parameters.

What does amtC have to do with getting a balance? What does amtC even mean?

I am not a fan of abbreviating variable names, unless there is a good reason. With balAmnt, the amount part is superfluous ; so what's wrong with just Balance ?

Good Luck !!


Last edited on
Thank you for your explanation. Unfortunately all we were given is what i wrote in my question. So we just have to look at the code fragment and the two headers and decide which header suits best.
These questions are very ambiguous
Only
void getBalance (string customerP, float & balAmnt,char & typeC, float amtC);
Matches the invocation
getBalance(customer, balance, type, 200.53);
Because lvalue references will not bind temporaries.
Last edited on
thank you
Topic archived. No new replies allowed.