[英]What are the difference between void function and float function?
I am still learning but... I don't think these functions: 我仍在学习,但是...我不认为这些功能:
void CalculateGross(float hours, float payrate, float *gross) //3.4
float CalculateGross(float hours, float payrate) //3.4
do the same thing. 做同样的事。 So which is better practice? 那么哪种更好的做法呢? I'm assuming void is better? 我假设虚空会更好?
/*
3.0 Payroll Application
3.1 PrintReportHeadings(inout reportFile as file)
3.2 InitializeAccumulators(out totreg, totovt,totpayrate,totgross,totfed,
totstate,totssi,totdefr, totnet as real)
3.3 InputEmployeeData(out lastname, firstname as string,
out hours, payrate, defr as real)
3.4 CalculateGross(in hours, payrate as real, out gross as real)
3.5 computeTaxes(in g,d as real, out ft, st, ssit as real)
3.5.1 cFed(in g,d as real, out fed as real)
3.5.2 cState(in ft as real,out state as real)
3.5.3 cSSI(in g,d as real, out ssi as real)
3.7 PrintSummaryReport( ......)
3.7.1 printTotals( ....)
3.7.2 printAverages( ....)
*/
#include <stdio.h>
#include "TAXRATES.h"
void InputEmployeeData(char *lastname,char *firstname, // 3.3
float *hours,float *payrate, float *defr);
void CalculateGross(float hours, float payrate, float * gross); //3.4
float CalculateGross(float hours,float payrate); //3.4
extern void computeTaxes(float g,float d,float * ft,float *st,float *ssit); //3.5
int main(void)
{
char ln[15+1];
char fn[10+1];
float fed,state,ssi;
float g,h,p,d,n;
InputEmployeeData(&ln[0],&fn[0],&h,&p,&d); // call 3.3
g = CalculateGross(h,p); // call 3.4
// vs
//CalculateGross(40.00,25.00,&g); // alternate call 3.4
computeTaxes(g,d,ADDR(fed),ADDR(state),ADDR(ssi)); // call 3.5
n = g-fed-state-ssi-d;
printf(" Fed = %8.2f\n",fed);
printf(" State = %8.2f\n",state);
printf(" SSI = %8.2f\n",ssi);
printf(" Net = %8.2f\n",n);
while(getchar() != '\n'); // flush(stdin)
return 0;
}
void CalculateGross(float hours, float payrate, float * gross) //3.4
{
if (hours <= 40)
*gross = hours * payrate;
else
*gross = 40* payrate + 1.5 * payrate * (hours-40);
}
float CalculateGross(float hours,float payrate) .. //3.4
{
if (hours <= 40)
return hours * payrate;
else
return 40* payrate + 1.5 * payrate * (hours-40);
}
void InputEmployeeData(char *lastname,char *firstname, // 3.3
float *hours,float *payrate, float *defr)
{
printf(" Enter the name ==> ");
scanf("%s%s",firstname,lastname);
printf(" Enter the hours and payrate ==> ");
scanf("%f%f",hours,payrate);
printf(" Enter the deferred earning amount ==> ");
scanf("%f",defr);
}
Functions should have input(s) and a single output. 函数应具有输入和单个输出。 Void says do something. 虚空说做点什么。 Pointers are great, but the intent is to calculate something. 指针很棒,但目的是计算一些东西。 Use f(x) = y, not f(x, out y) when possible, of course. 当然,尽可能使用f(x)= y,而不是f(x,out y)。
void CalculateGross(float hours, float payrate, float * gross); //3.4
float CalculateGross(float hours,float payrate); //3.4
where: 哪里:
void CalculateGross(float hours, float payrate, float * gross) //3.4
{
if (hours <= 40)
*gross = hours * payrate;
else
*gross = 40* payrate + 1.5 * payrate * (hours-40);
}
Here - it does NOT have any effect on your code. 在这里-它对您的代码没有任何影响。 (you will get warnings.. but no impact) Why? (您会收到警告。.但没有影响)为什么? The type
before the function name defines the type of return
for the function. 函数名称前的type
定义函数的return
类型。 What you get back if you do: 如果这样做,您会得到什么:
foo = CalculateGross (....);
Since CalculateGross
does not return any value (it only operates on values internally), the return type has no impact anywhere else. 由于CalculateGross
不返回任何值(它仅在内部对值进行操作),因此返回类型对其他地方没有影响。
Which is correct? 哪个是对的? That is a different question. 那是一个不同的问题。 void
is correct for all function where no value is returned. void
对于不返回任何值的所有函数都是正确的。 When you specify float
, the compiler will check your function for return somevalue;
当您指定float
,编译器将检查您的函数是否return somevalue;
. 。 If not found it will generate a warning (or error). 如果找不到,它将生成警告(或错误)。
So, if your function returns a value (or pointer), match the type
to the type the function returns. 所以,如果你的函数返回一个值(或指针),匹配type
为类型的函数返回。 If your function returns nothing, void
is the proper return type. 如果函数不返回任何内容,则void
是正确的返回类型。
Note: just because you declare a function void
does not mean you cannot use return
within the void function. 注意:仅仅因为您声明一个函数void
并不意味着您不能在void函数内使用return
。 You can use return;
您可以使用return;
by itself to cause to function to return ( bail out ) at that point without returning a value. 本身导致函数在该点返回( 纾困 )而不返回值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.