如何将 float 变量从一个函数传递给另一个函数。?
How to pass float variable from one functions to other function.?
我正在编写一个程序,其中我必须在其他函数中使用局部变量。如果变量数据类型是 int 我可以做到,但如果它是 float 则它不起作用。
我正在使用以下代码为 int 传递值:
int func1()
{
float a = 2.34, b = 3.45, res1;
int c = 2, d = 3, res2;
res1 = a * b;
res2 = c * d;
return res2;
}
int func2(int res2)
{
res2 = func1(res2);
printf("%d", res2);
}
所以 res2
存储 int 值的结果,res1
存储 float 值的结果。根据以上逻辑,我可以传递 res2
(整数)但不能传递 res1
(浮点)的值。我不知道我哪里漏掉了重点。这该怎么做。请帮忙,谢谢!
函数的类型表示它的值是什么类型returns
// func1 returns values of type int
int func1(void) {
// return 3.14169; // automagically convert to 3
// return "pi"; // error: cannot convert "pi" to a value of type int
return 42;
}
如果你想要一个函数return浮点类型的值,你需要用浮点类型定义它们
// func3 returns a floating point value of type double
double func3(void) {
// return 3.14159 // return the value
// return "pi"; // error: cannot convert "pi" to a value of type double
return 42; // converts the int value to the same value in type double
}
我正在编写一个程序,其中我必须在其他函数中使用局部变量。如果变量数据类型是 int 我可以做到,但如果它是 float 则它不起作用。
我正在使用以下代码为 int 传递值:
int func1()
{
float a = 2.34, b = 3.45, res1;
int c = 2, d = 3, res2;
res1 = a * b;
res2 = c * d;
return res2;
}
int func2(int res2)
{
res2 = func1(res2);
printf("%d", res2);
}
所以 res2
存储 int 值的结果,res1
存储 float 值的结果。根据以上逻辑,我可以传递 res2
(整数)但不能传递 res1
(浮点)的值。我不知道我哪里漏掉了重点。这该怎么做。请帮忙,谢谢!
函数的类型表示它的值是什么类型returns
// func1 returns values of type int
int func1(void) {
// return 3.14169; // automagically convert to 3
// return "pi"; // error: cannot convert "pi" to a value of type int
return 42;
}
如果你想要一个函数return浮点类型的值,你需要用浮点类型定义它们
// func3 returns a floating point value of type double
double func3(void) {
// return 3.14159 // return the value
// return "pi"; // error: cannot convert "pi" to a value of type double
return 42; // converts the int value to the same value in type double
}