程序返回垃圾值

Uba*_*lah 2 c++

我正在开发一个程序,该程序使用两个函数 area 和周长来返回正方形的面积和周长。我编写的以下代码正确返回区域,但会为周长生成垃圾值。你能纠正我做错了什么吗?

#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;
}
Run Code Online (Sandbox Code Playgroud)

Aza*_*lam 7

您必须s在周边函数中将side作为参数传递,例如:

double perimeter(double s){
    return 4 * s
}
Run Code Online (Sandbox Code Playgroud)

您应该使用如下参数调用您的函数:

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

让我知道它是否有效。