编写函数,对传送出来的两个浮点数求出和值与差值并通过形参传送回调用函数

其中 通过形参传送回调用函数 是什么意思 (只说明这一点 )
2025-03-20 11:40:55
推荐回答(2个)
回答1:

函数只能有一个返回值, 如果有多个值要从函数传回被调用方,只能使用地址传递和引用传递,标准C只用地址传递, 没有值引用传递,C++可以使用引用传递。地址传递代码如下:

#include 

void fun1(float a, float b, float * x, float * y)
{
*x = a + b;
*y = a - b;
}

int main()
{
float a = 10, b=20, ans1, ans2;
fun1(a, b, &ans1, &ans2);
printf("%f,%f\n", ans1, ans2);
}

回答2:

//#include "stdafx.h"//vc++6.0加上这一行.
#include "stdio.h"
void fun(double a,double b,double *pa,double *ps){
*pa=a+b;
*ps=a-b;
}
void main(void){
double a,b,f_add,f_sub;
printf("Type 2 floating point numbers...\n");
scanf("%lf%lf",&a,&b);
fun(a,b,&f_add,&f_sub);
printf("The add is %f.\nThe sub is %f.\n",f_add,f_sub);
}