Finding errors in Functions! Valid or Invalid

Hi, So my class is just learning about functions and how they work. So I have an assignment were I need to find as many errors as possible and correct them, and the second part is determining whether a calling function is valid or not. I just wanted some feedback or someone who could just look over my answers and let me know what are right and what I need to fix and the reason for it.

Thanks in advance! P.S. the bolded stuff are the corrections I made to the function.

1. int total (int value1, int value2, int value3)
return value1 + value2 + value3;
Void function cannot return a value.
Value 2 and 3 do not have a data type

2. double average(int value1, int value2, int value3)
double average;
average = (value1 + value2 + value3) / 3;
when calculated the average the addition must be separated with parentheses

3. int area (int length = 30, int width)
return length * width;
void function can’t return a value
the initialized variable should always be the last in the list.

4. int getValue(int value&)
cout << “Enter a value: “;
cin >> value(removed &);
A void function can’t return a value.
When inputting the & is not needed after value.


5. Assume the declarations:
void Combine(string, char, int, double&, int&);
float Calculate(int,double,int);
char ch;
string str;
int x,y;
double z;
string ans;

which calls are invalid, which calls will lose significant data and why?
Valid Invalid Why
a. ans = Combine('a', "hello", z, x, y); Invalid because void can’t return a value so ans cannot store a value.

b. Combine (str, ch, 14, 15.3,5.4); Invalid because 5.4 cannot be stored inside a int data type it will be truncated to 5
c. Combine ("hi", 'b', z, x, 9); Invalid, because z is declared as a double and being passed as a integer

d. Combine(str,ch,x,z,y); ¬Valid, all variables correspond to the correct data types being passed.

e. if (Calculate(4,5.5,3) > 0) Valid
ans = “positive”;

f. while(Combine(4,5.5,3) > 0)) Valid
cout << "forever";

IMO You have problems with items 2, 4, 5a,b,c,d,e,f

Some of them relate to the biggest most common problems for beginners. Hint: what should one do at the same time as declaration?

One is to do with references, another is return statement, another is comparison between different types, loss of data because a lack of a cast, and there is one really pedantic & annoying error to do with punctuation. I reckon it is worth chocolate biccy's for the last one :)

Why don't you actually test these with your own compiler?

Make sure you set the warning levels to their highest, not sure what compiler you have, but here is an example so you can see what to look for: with g++, set these options:

-Wall, -Wextra, -pedantic, -Werror

there are other options that deal with floating point problems.

Hope all goes well

What do you mean? I'm not actually compiling them, My teacher has these in a note document and we have to simply state the errors and correct. So no compiler is needed.

It is homework isn't it? Take it to the lab, or home if you have a compiler there. Make the implementation very simple: get it to print out the value of each parameter (another big clue). You might be surprised at what you learn by doing that. Check for runtime errors too. Let the computer do the work, it is better at finding errors than humans are.

How are you going with the hints I have given? See if you can figure out what I am on about. As I said, most easily done by compiling.

At the moment I would say you have about 2/3 of it correct.

.... My teacher has these in a note document ....

That is another clue to the last pedantic chocolate biccy's error.

Maybe I am looking at it in too much detail, but the more things that are mentioned, the more brownie points.
Wow! I did not even think about that! Thank You! I will take a look at it with the compiler and see what comes up. Should I just copy the function into the compiler or make a basic code including #include etc.. and variables or should the compiler be able to detect a function alone?

Thank You
Should I just copy the function into the compiler or make a basic code including #include etc.. and variables or should the compiler be able to detect a function alone?

The corrections you have made are right, you are just missing some things.

Copy the code as you have it above, but obviously you will have to add stuff to make it a complete compilable program. The functions that have definitions are fine by themselves, but where there is only a declaration, I am suggesting putting std::cout to print the values of each variable, don't add anything else though. It is essential that you turn on all the warnings to their maximum level, and that is how you should always compile your programs.

Good luck
Topic archived. No new replies allowed.