简体   繁体   English

浮点函数返回值

[英]Float function return value

I am writing my object first app and i can't understand why compiler gives my errors. 我正在写我的对象第一个应用程序,但我不明白为什么编译器会给出错误。 (With int codes works... ) (使用int代码可以...)

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool {


        // 1st var
        NSLog(@"Hi, %f World!", res(1.0f, 2.0f, 3.0f));

    }
    return 0;
}
float res (float a, float b, float c)
{
    float res=a+b+c;
    return res;
}

Try declaring res before main, so that it is known to the compiler when it founds it in main . 尝试在main之前声明res ,以便编译器在main找到它时就知道它。 If you don't declare it beforehand, what happens is: 如果您不事先声明,会发生以下情况:

  1. the compiler first encounters res in main body; 编译器首先在main遇到res

  2. it makes up an "implicit declaration" for res , based on what it can infer from the way res is called inside of main ; 它根据从main内部调用res的方式可以推断出res的“隐式声明”; this implies an int return type, according to C conventions; 根据C的约定,这意味着一个int返回类型;

  3. when the real res is found later on, a mismatch between the inferred signature (ie, return type) and the real one triggers the compilation error. 当稍后找到真实的res时,推断出的签名(即返回类型)与真实签名之间的不匹配会触发编译错误。

To fix it: 要解决这个问题:

float res (float a, float b, float c);

int main(int argc, const char * argv[])
{
  @autoreleasepool {

    // 1st var
    NSLog(@"Hi, %f World!", res(1.0f, 2.0f, 3.0f));
  }
  return 0;
}

float res (float a, float b, float c)
{
  float res=a+b+c;
  return res;
}

You are just forgetting to declare function, just put 您只是忘记声明函数,只是放了

float res (float a, float b, float c);

before your int main 在您的int main之前

hope it helps you! 希望对您有帮助!

Try: 尝试:

#import <Foundation/Foundation.h>

float res (float a, float b, float c)
{
    float res=a+b+c;
    return res;
}

int main(int argc, const char * argv[])
{

    @autoreleasepool {


        // 1st var
        NSLog(@"Hi, %f World!", res(1.0f, 2.0f, 3.0f));

    }
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM