Void Function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
include <iostream>
#include <cmath>

using namespace std;

void fun(float);

int  main ()

{
    float a,b,x;
    int ok;

    do
    {

    cout << " Enter 2 numbers  -> " << endl;
    cin >> a; cout << endl;
    cin >> b; cout << endl;
    x=((a*a)+(b*b));
    fun(x);



    cout << " End (0) Continue (1) ?" << endl;
    cin >> ok; cout << endl;
    }while(ok==1);
    return 0;
}

   void fun(float i)
{
    float x;

    cout << " Third size for the side = ";
    cout << sqrt(x);

}


Basically what i'm trying to do is to create a primitive Pythagorean theorem.
You enter 2 numbers. a, b. then a^2 + b^2, and then Square root from both of those numbers.
I've no idea what i'm doing wrong, it always gives me same number 1,29 regardless of what numbers i enter.
You calculate the square root of uninitialized local variable. So the function behavior is undefined

1
2
3
4
5
6
7
8
   void fun(float i)
{
    float x; // what is value of x?!!!

    cout << " Third size for the side = ";
    cout << sqrt(x);  // Should you use here i instead of x?

}
Last edited on
Thanks a lot, trying to learn on my own and it's rather confusing.
Again, thanks.
Topic archived. No new replies allowed.