0

I'm working on a program that uses two functions area and perimeter to return the area and perimeter of a square. The following code i wrote returns area correctly but produces a garbage value for perimeter. Can you correct what i'm doing wrong?

#include<iostream>
#include<cmath>
using namespace std;
int area(int s)
{
    int area = s * s;
    return area;
}
double perimeter()
{
    int s;
    int perimeter = 4 * s;
    return perimeter;
}
int main()
{
    int s;
    cout << "enter the side: "
        << endl;
    cin >> s;
    cout << "area of square is "
        << area(s) << endl;
    cout << "perimeter of square 25.  is" << perimeter() << endl;
}
Zeus
  • 3,703
  • 3
  • 7
  • 20
  • 1. NEVER line-number posted code on this site. If a specific line number is called out in an error message, mark it with an inline *comment*. 2. Always properly format and indent code posted on this site. 3. `int s; int perimeter = 4*s;` ... and `s` is *what* ?? it's an indeterminate `int`. Your code invokes *undefined behavior*. – WhozCraig Nov 27 '20 at 07:56
  • Would you mind removing the line numbers so that the code is easier to read for other people? – Waqar Nov 27 '20 at 07:56
  • The function `perimeter()` declares a variable `s`, which is never initialised, and then used. That function therefore exhibits undefined behaviour. The `s` in `perimeter()` is a distinct variable with no relationship whatsoever to the `s` declared in `main()` nor to the argument of `area()`. – Peter Nov 27 '20 at 08:19
  • with correct compiler flags this does not compile https://godbolt.org/z/cT59dW. The compiler is your friend, make use of it! – 463035818_is_not_an_ai Nov 27 '20 at 08:25

2 Answers2

8

You have to pass side s as parameter in perimeter function like:

double perimeter(double s){
    return 4 * s
}

And you should call your function with the parameter like:

int main(){
    double s;
    cin >> s;
    cout << "perimeter of square 25. is" << perimeter(s) << endl;
}

Let me know if it works.

Bayou
  • 3,293
  • 1
  • 9
  • 22
Azahar Alam
  • 708
  • 5
  • 16
0

In your perimeter() function the variable s is not initialised with value. The variable s is pointing to some garbage value from memory, when it is not initialised. For that reason you are getting the value as garbage, when you multiplying 4 with s (some garbage value).

You can change the method/function signature like following:

double perimeter(double s){
    return 4 * s
}

and call the function with parameter inside it.

 int square; 
 cin>>square;
 cout << "perimeter of square 25.  is" << perimeter(square) << endl;

or you can simply declare a global variable which hold the square value of the perimeter you want.

int square;
int main()
{
  cin>> square;  
  cout << "perimeter of square 25.  is" << perimeter() << endl; 
}
Papai from BEKOAIL
  • 1,469
  • 1
  • 11
  • 17
  • (nitpick) "The variable s is pointing to some garbage value from memory" thats a tiny bit misleading. When we are talking about C++ then there is no garbage value. The code has undefined behavior. Thats it. When we are talking about what the compiler does to the code, and how the resulting program behaves, we should look at the compilers output not the C++ code to understand what is going on – 463035818_is_not_an_ai Nov 27 '20 at 08:53
  • so, according to you, from where the word garbage value came from? what is the meaning of it? when you declare a variable without initialisation what value it primarily holds ? – Papai from BEKOAIL Nov 27 '20 at 10:08
  • imho the word "garbage" is a misleading myth in this context. It doesn't make much sense to reason about what code does according to the standard when it has ub, because by definition what the code does is undefined. What the resulting program does when you ask a compiler to compile it is a different story. What you get is typically contents of some memory location. And I don't have "garbage" in my computers memory ;) – 463035818_is_not_an_ai Nov 27 '20 at 10:15
  • where it comes from? Probably ppl expect to get something meaningful but get something else and then call that "garbage", but it isnt really. If you study the assembly you can find out where the value comes from – 463035818_is_not_an_ai Nov 27 '20 at 10:19
  • what value it primarily holds ? None. Its an indeterminate value. Its a bit like asking what is the sound of a falling tree when nobody is listening to it. – 463035818_is_not_an_ai Nov 27 '20 at 10:21
  • 2
    https://stackoverflow.com/questions/13423673/what-is-indeterminate-value – 463035818_is_not_an_ai Nov 27 '20 at 10:25