User-defined functions

How can I calculate area of triangle using 3 user-defined functions which include:
(i) input()
(ii) calculateArea()
(iii) printArea()
I'm still 1 month old into programming, please guide me thank you very much.I have tried to work out the question but I only can use one user-defined functions to solve the problem. Supposedly I have to use 3 user-defined functions, below is my coding that I have work out so far.

Last edited on
Read: http://www.cplusplus.com/doc/tutorial/functions/

What kind of functions would make this program possible:
1
2
3
4
5
6
7
8
int main()
{
  float bs, ht;
  input( bs, ht );
  auto area = calculateArea( bs, ht );
  printArea( area );
  return 0;
}
It's actually one of my lab practical gave by my lecturer. Basically this program later will used to calculate area of other shapes as well such as rectangle, circle, etc using a menu-based program. But I know how to code a menu-based using if/else or case, the problem is I have to use 3 user-defined functions to solve the problem. Please guide me through. Thank you very much.
Hello TCK,

keskiverto has given you a good idea and start. I see that you have written a function "calculateArea". Just duplicate the concept for the other two functions. What is between the {} will be different though.

I would suggest using "double"s over the "float"s.

Hope that helps,

Andy
This is what I tried to come out with but I encountered error C4700 uninitialized local variable used in line 14 and 37.


Please do point out my mistake thank you very much.
Last edited on
What do you have on line 36?
The function surely will print the area that it got as parameter?


If a function returns double, then it returns exactly one double value.
Reread http://www.cplusplus.com/doc/tutorial/functions/
There is section about by reference parameters.
The best functions, do one thing, very simple, very fast, and then can be reused.

What wrong with my line 36? Why binary '<<' can't be used there?
Last edited on
Hello TCK,

If you take a close look at the function what is it doing?

1
2
3
4
void printArea(double area)
{
	cout << "The area of triangle is " << printArea(area) << "cm" << char(253) << endl;
}

The function returns a "void", so on line 36, line 3 above, when you call a void function what do you expect to get back?

In "main" your first line is to define two variables, but they are uninitialized. This is an error on my compiler. Because the next is using these uninitialized variables. The use of "auto" works, but better to call it a "double".

In the "input" function you are returning a double, but the return is trying to return two items. A function can only return one item. This could be a pointer to an array or a vector to return one than one item.

Inside the function starting with the function parameters you are receiving the variables "bs" and "ht" then inside the function you define "BS" and "HT". These really are not needed as the function parameters are all you need. Then you get user input to "BS" and "HT", but want to return "bs" and "ht". What is your idea here? If you pass "bs" and "ht" by reference and do your "cin" to these variables you can make the function a "void" return and what is entered into "bs" and "ht" will be reflected back in main.

The "calculateArea" function appears to be right although I would make the variables constant as in
double calculateArea(const double bs, const double ht).

After I made the changes the program compiled and worked.

Hope that helps,

Andy

Edit:
Last edited on
Hi Andy, basically in the line 36 above I want to print the answer I've got in the 'printArea(area)' function but the Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion). I actually don't understand what it mean because what I expect is the program will just print out the 'printArea(area)' in the main().

Besides, if I don't change the variables in the function, which is using capital 'BS' and 'HT', error 2082 redefinition of formal parameter will occur. As below,

Btw, I actually still can't get the mistakes I made in line 36, really thank you for helping me.

Last edited on
This code of yours:
1
2
3
4
void printArea(double area)
{
  cout << printArea(area); // error
}

Your printArea() prints the area by calling the prinArea(), which will call the printArea(), which will ...

That is infinite recursion. Be thankful that your syntax errors prevent it from compiling.

If you want to print a value, then do that:
1
2
3
4
void printArea( double area )
{
  cout << area;
}



1
2
3
4
5
void input( ??? foo, ??? bar )
{
	cout << "Enter base and height: ";
	cin >> foo >> bar;
}

What should be on the ????

You want to modify the variables of the caller. The by value parameters are copies and thus do not facilitate that. I did already mention by reference parameters.


@Andy:
The const by value parameters are of limited benefit.
The caller does not care (if you can make a copy, then you can)
and the function can always bypass the constness without sweat:
1
2
3
4
5
6
void foo( const int snafu )
{
  int bar = snafu;
  bar = 42;
  // use bar
}

Thus, that const is just a reminder for the programmer.
Hi keskiverto,

I understand the mistake I made regarding the infinite recursion u mentioned.

Is it true that the ??? should be double? I'm actually still quite confused, is it true that void will not get back a return? But how come I still encounter error ...

Error C2082 : redefinition of formal parameter in line 23
Last edited on
Your attempt is equivalent to:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

void bar( double foo ) // foo is a by value parameter
{
	double foo; // this local foo hides parameter foo
	foo = 42; // this modifies local foo
}

int main() {
  int gaz = 7;
  bar( gaz );
  std::cout << gaz << '\n'; // prints 7
}

You modify local variable and that will not change anything outside of the bar.

I did give a hint in code that I did post:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

void bar( double foo ) // foo is a by value parameter
{
	foo = 42; // this modifies foo
}

int main() {
  int gaz = 7;
  bar( gaz );
  std::cout << gaz << '\n'; // prints 7
}

A by value parameter is a copy of gaz. The bar does not modify gaz.

I told you to read about by reference parameters. Have you done so?

I will not show you the correct syntax, because I want you to learn it from the tutorial (or other material).
Hello TCK,

@keskiverto,

Sorry I seem to have missed your earlier post somehow. Anyway thank you for the input. I see your point here. I believe I did that based on what I have read here and was thinking more of the function definition at the time and not so much of what was in the function. in the future I will put a little more thought into it.


keskiverto has given you some very good hints and example code to work with. Like keskiverto I would like to see you learn the answers. You will retain the information better.

If you have not found what you need from the book(s) that you are using this will help http://www.cplusplus.com/doc/tutorial/functions/#reference After that it may be useful to read the whole web page about functions.

Your program is getting better, but I still see some problems.

Line 7 of the last code posted. This prototype needs to match the function definition. As keskiverto has pointed out the use of "const" in the function definition is not necessary. I was thinking of something else when I did that.

In the "input" function line 23 is redefining the variable that the definition has already defined. After that you need to understand the difference of pass by value and pass by reference.

Your program is close and with the proper fixes it works.

Hope that helps,

Andy
Hi keskiverto,

Really thank you for your patience to guide me through. I actually did study up the by reference parameter and i tried to modify the example with my questions but errors just made demotivated me all the time... because when i search more about the errors, I can't even understand the errors I made (my mistakes).

I now understand the why I shouldn't put variables in my functions input because it is a by value parameter. But I want to ask is it true that I have to such as
input(double&,double&)
to perform the by reference parameter?

Anyhow, I still got error C4700 uninitialized local variable at line 4 below and I can't understand why. Didn't I already initialized it in line 3? How come I still encounter errors...
1
2
3
4
5
6
7
8
9
int main()
{
	double bs,ht; // variables base and height
	input(bs,ht);
	double area = calculateArea(bs, ht);
	printArea(area);
	
	return 0;
}


On another compiler:
1
2
3
4
5
6
7
8
9
void bar( int foo ) // warning: parameter 'foo' set but not used
{
	foo = 42;
}

int main() {
  int gaz;
  bar( gaz ); // warning: 'gaz' is used uninitialized in this function
}

Your compiler must be more strict (which is good).

This generates no warnings:
1
2
3
4
5
6
7
8
9
void bar( int& foo )
{
	foo = 42; // this modifies gaz
}

int main() {
  int gaz;
  bar( gaz );
}


Didn't I already initialized it in line 3?
double bs, ht;

No. You do declare variables bs and ht, but there is no initialization.

double bs {3.14}; // here is initialization too
Thank you so much, keskiverto and Andy for your patience to guide me through. I finally understand pass by reference and pass by value. Just realized how important is to differentiate what is pass by reference and value. Really thank you two.
Last edited on
Hello TCK,

Good job. Now you have learned something.

I do have one suggestion.

One way or another some people are likely to enter one number and press Enter. This leaves the cursor hanging on the next line giving no idea what to do. Something like: cout << "Enter base and height, in cm, (1 2): ";. It just gives an example of how to enter the numbers. Or set up prompts and "cin"s for each number.

I would not call it efficient, but because of the differences between sometimes it is easier to treat each separately. Sometimes functions can be made generic to handle more than one of something.

In something like this from a menu choice you could use a switch to direct what function to call after first getting the input or calling a function for that.

Hope that helps,

Andy
Topic archived. No new replies allowed.