在C中以浮点形式调用函数
我正在使用C语言中的函数。如果我想返回void taxCalculator(),代码会是什么样子;将main()整型为float,这样就可以在那里打印 这是代码的样子: 上面的定义在C中以浮点形式调用函数,c,C,我正在使用C语言中的函数。如果我想返回void taxCalculator(),代码会是什么样子;将main()整型为float,这样就可以在那里打印 这是代码的样子: 上面的定义 void taxCalculator(float income, float tax); 主要功能: int main(void){ float income, tax; printf("Enter the amount of income: "); scanf("%f", &income); t
void taxCalculator(float income, float tax);
主要功能:
int main(void){
float income, tax;
printf("Enter the amount of income: ");
scanf("%f", &income);
taxCalculator(income, tax);
}
以及taxCalculator功能:
void taxCalculator(float income, float tax)
{
if( income < 750.00f){
tax = income * 0.01f;
}else if (income <= 2250.00f){
tax = 7.50f + (income - 750) * 0.02f;
}else if (income <= 3750.00f){
tax = 37.50f + (income - 2250) * 0.03f;
}else if (income <= 5250){
tax = 82.50f + (income - 3750) * 0.04f;
}else if (income <= 7000){
tax = 142.50f + (income - 5250) * 0.05f;
}else if(income > 7000.00f){
tax = 230.00f + (income - 7000) * 0.06f;
}
printf("The amount of tax: %.2f", tax);
}
void taxCalculator(浮动收入、浮动税)
{
如果(收入<750.00f){
税=收入*0.01f;
}如果(收入你可以这样做
float taxCalculator(float income) {
...
return tax;
}
...
printf("The amount of tax: %.2f", taxCalculator(income));
int result = taxCalculator(income, &tax);
if (-1 == result)
{
perror("taxCalculator() failed");
}
else
{
printf("The amount of tax: %.2f", tax);
}
当函数执行时,当它终止时,它将被其返回值替换,因此printf()
将使用该值进行打印
完整示例:
#include <stdio.h>
float taxCalculator(float income) {
float tax;
if (income < 750.00f) {
tax = income * 0.01f;
} else if (income <= 2250.00f) {
tax = 7.50f + (income - 750) * 0.02f;
} else if (income <= 3750.00f) {
tax = 37.50f + (income - 2250) * 0.03f;
} else if (income <= 5250) {
tax = 82.50f + (income - 3750) * 0.04f;
} else if (income <= 7000) {
tax = 142.50f + (income - 5250) * 0.05f;
} else if (income > 7000.00f) {
tax = 230.00f + (income - 7000) * 0.06f;
}
return tax;
}
int main(void) {
float income = 2250;
printf("The amount of tax: %.2f", taxCalculator(income));
return 0;
}
#包括
浮动税计算器(浮动收入){
浮动税;
如果(收入<750.00f){
税=收入*0.01f;
}如果(收入你可以这样做
float taxCalculator(float income) {
...
return tax;
}
...
printf("The amount of tax: %.2f", taxCalculator(income));
int result = taxCalculator(income, &tax);
if (-1 == result)
{
perror("taxCalculator() failed");
}
else
{
printf("The amount of tax: %.2f", tax);
}
当函数执行时,当它终止时,它将被其返回值替换,因此printf()
将使用该值进行打印
完整示例:
#include <stdio.h>
float taxCalculator(float income) {
float tax;
if (income < 750.00f) {
tax = income * 0.01f;
} else if (income <= 2250.00f) {
tax = 7.50f + (income - 750) * 0.02f;
} else if (income <= 3750.00f) {
tax = 37.50f + (income - 2250) * 0.03f;
} else if (income <= 5250) {
tax = 82.50f + (income - 3750) * 0.04f;
} else if (income <= 7000) {
tax = 142.50f + (income - 5250) * 0.05f;
} else if (income > 7000.00f) {
tax = 230.00f + (income - 7000) * 0.06f;
}
return tax;
}
int main(void) {
float income = 2250;
printf("The amount of tax: %.2f", taxCalculator(income));
return 0;
}
#包括
浮动税计算器(浮动收入){
浮动税;
如果(收入<750.00f){
税=收入*0.01f;
}否则,如果(收入您可以将结果返回到传递给函数的内存位置:
void taxCalculator(float income, float * ptax)
{
float tax;
... // init tax here
printf("The amount of tax: %.2f", tax);
*ptax = tax;
}
这样说吧:
int main(void)
{
float income, tax;
...
taxCalculator(income, &tax);
...
然后,您可以自由使用的返回值作为错误指示器:
#include <errno.h>
#include ...
int taxCalculator(float income, float * ptax)
{
float tax;
if ((0. > income) || (NULL == ptax))
{
errno = EINVAL;
return -1;
}
... // init tax here
*ptax = tax;
return 0;
}
您可以将结果返回到传递给函数的内存位置:
void taxCalculator(float income, float * ptax)
{
float tax;
... // init tax here
printf("The amount of tax: %.2f", tax);
*ptax = tax;
}
这样说吧:
int main(void)
{
float income, tax;
...
taxCalculator(income, &tax);
...
然后,您可以自由使用的返回值作为错误指示器:
#include <errno.h>
#include ...
int taxCalculator(float income, float * ptax)
{
float tax;
if ((0. > income) || (NULL == ptax))
{
errno = EINVAL;
return -1;
}
... // init tax here
*ptax = tax;
return 0;
}
tax
未用作输入,因此不需要将其作为参数。仍然需要从函数调用中删除tax
:)如果我从函数调用中删除tax,它将显示error@TacoCat你忘了改变函数的原型了吗?如果它不是一个乌托邦,我会投票反对你的答案,只是因为它定义了负税…;-)tax
未用作输入,因此不需要将其作为参数。仍然需要从函数调用中删除tax
:)如果我从函数调用中删除tax,它将显示error@TacoCat你忘了更改函数的原型了吗?如果它不是乌托邦,我会对你的答案投反对票,只是因为它定义了负税…;-)你使用的是浮动
,那么为什么它会返回整数
?另外,你通常应该更喜欢双倍
,而不是e> float
。您是对的,亲爱的先生。请从main()
中返回一个值。启用编译器警告,它们将为您节省大量时间。@iharob:从C99开始,在main
结尾处到达结尾处的}
会隐式返回0;
。显式返回值仍然是个不错的主意(例如,gcc默认不执行C99规则)@KeithThompson很好的澄清,我总是将-Wall-Wextra-Werror
与gcc
一起使用,它可以让您学习我认为的最佳实践,而且可以避免犯许多愚蠢的错误。您使用的是float
,那么为什么它会返回int
?此外,您通常更喜欢double
,而不是float
。您是对的,亲爱的先生。请从main()
中返回一个值。启用编译器警告,它们将为您节省大量时间。@iharob:从C99开始,在main
结尾处到达结尾处的}
会隐式返回0;
。显式返回值仍然是个不错的主意(例如,gcc在默认情况下不强制执行C99规则)。@KeithThompson很好的澄清,我总是将-Wall-Wextra-Werror
与gcc
一起使用,它让您学习我认为的最佳实践,同时避免了许多愚蠢的错误。我认为其目的是将函数放在printf()中
。如果是这样,那么这种方法对IMHO没有多大帮助。这就是精神!同意!:D@alk我喜欢它,我会投你一票,但是我已经达到了投票的每日限制,对不起!我认为它的目的是将函数放入printf()中
。如果是这样,那么这种方法对IMHO没有多大帮助。这就是精神!同意!:D@alk我喜欢,我会投你一票,但我已经达到了每天的投票上限,对不起!