如何让函数返回C中多个变量类型的结果? [英] How do I make a function return the result of multiple variable types in C?

查看:78
本文介绍了如何让函数返回C中多个变量类型的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚开始从Cocoa开发指南学习C,我想知道如何(如果可能的话)我会返回一个函数的结果与多个变量类型。例如,我有一个简单的数学函数,用于练习我正在阅读的内容,并给出了多种变量类型:

  #include< stdio.h> 

float doMath(int variable1,float variable2,int variable3,float variable4);

main()
{
printf(Math is fun !! \\\
);

float theMath = doMath(2,3.66,9009,7.990);
printf(Result =%f\\\
,theMath);


$ b float doMath(variable1,variable2,variable3,variable4)
{
return(variable1 * variable2)+(variable3 - variable4);
}

如何在单个函数/方程中正确使用多个变量类型?
另外,我不确定返回行的语法是否正确...我有点刺伤了它。

解决方案

首先,您不要将函数定义放在 main()函数。在外面做。
你可能想把 int main()而不是main,并且返回0 结束 int main()



然后只分配一个float变量来保存结果并返回。 p>

  #include< stdio.h> 

float doMath(int variable1,float variable2,int variable3,float variable4);

int main()
{
printf(Math is fun !! \\\
);

float theMath = doMath(2,3.66,9009,7.990);
printf(Result =%f\\\
,theMath);

返回0;


float doMath(int variable1,float variable2,int variable3,float variable4)
{
float answer =(variable1 * variable2)+(variable3 - variable4 );
返回答案;
}


Just started learning C from Cocoa developing guide and I was wondering how (if at all possible) I would return the result of a function with multiple variable types. For example, I have a simple math function that I made to practice what I am reading and I gave it multiple variable types:

#include <stdio.h>

float doMath (int variable1, float variable2, int variable3, float variable4);

main()
{
    printf ("Math is fun!!\n");

    float theMath = doMath (2, 3.66, 9009, 7.990);
    printf ( "Result = %f\n", theMath );

}

float doMath (variable1, variable2, variable3, variable4) 
{
    return (variable1 * variable2) + (variable3 - variable4);
}

How would I utilize multiple variable types properly in a single function/equation? Also, I'm not sure the syntax of the return line is correct either...I sort of just took a stab at it.

解决方案

First, you don't put the function definition inside the main() function. Do it outside. And you might want to put int main() instead of just main, and return 0 at the end of int main()

Then just assign a float variable to hold the result and return it.

#include <stdio.h>

float doMath (int variable1, float variable2, int variable3, float variable4);

int main()
{
    printf ("Math is fun!!\n");

    float theMath = doMath (2, 3.66, 9009, 7.990);
    printf ( "Result = %f\n", theMath );

    return 0;
}

float doMath (int variable1, float variable2, int variable3, float variable4) 
{
    float answer = (variable1 * variable2) + (variable3 - variable4);
    return answer;
}

这篇关于如何让函数返回C中多个变量类型的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆