简体   繁体   English

void函数和float函数有什么区别?

[英]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.

相关问题 C中void和float function的区别 - Difference between void and float function in C C语言中void和静态void函数之间的区别是什么? - What is the difference between void and static void function in C? void function 和 int *function for return arrays 有什么区别 - What is the difference between void function and int *function for returning arrays 在void *函数中返回float - Returning float in void* function 函数和* function有什么区别? - what the difference between a function and *function? 回调函数:void(* func)(int)和void(func)(int)之间的区别 - callback function: difference between void(*func)(int) and void(func)(int) &#39;void *&#39; 和 &#39;void * (*function)(void *)&#39; 初始化、转换和函数指针调用之间的区别? - Difference between 'void *' and 'void * (*function)(void *)' initialization, casting, and function pointer calls? 函数定义中的指针与数组:void fct1(int * p)和void fct1(int p [])之间的区别是什么? - Pointer vs Array in function definition: what is the difference between void fct1(int *p) and void fct1(int p[])? C语言:函数中的float和void之间的区别 - C language: Difference between float and void in functions (void **)&x和(void *)x有什么区别? - What is the difference between (void **)&x and (void *)x?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM