C 返回函数Don';不行。利用勾股定理

C 返回函数Don';不行。利用勾股定理,c,robot,nxt,C,Robot,Nxt,这个程序运行时不会出错,但当我运行它并打开调试器时,变量不起作用。我已经用smallbasic编程了,我知道这在它上面是可行的。我正在将它转换为robot-c摆脱getdistance中的所有return语句,除了结尾的return lengthformulareturn使函数停止运行,并立即返回指定的值,因此函数的其余部分永远不会运行,距离也永远不会计算出来 在main()中,需要将结果分配给变量: #pragma config(Motor, port1, Rightsi

这个程序运行时不会出错,但当我运行它并打开调试器时,变量不起作用。我已经用smallbasic编程了,我知道这在它上面是可行的。我正在将它转换为robot-c

摆脱
getdistance
中的所有
return
语句,除了结尾的
return lengthformula
return
使函数停止运行,并立即返回指定的值,因此函数的其余部分永远不会运行,距离也永远不会计算出来

main()
中,需要将结果分配给变量:

#pragma config(Motor,  port1,           RightsideB,    tmotorVex393_HBridge, openLoop)
#pragma config(Motor,  port2,           RightsideF,    tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port9,           LefttsideF,    tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port10,          LeftsideB,     tmotorVex393_HBridge, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard             !!*//

float xdiff, ydiff, firstpart, secondpart, firstpart2, secondpart2, total, lengthformula;
float getdistance(int x1, int x2,int y1, int y2){
    return x1;
    return x2;
    return y1;
    return y2;

    xdiff = x2 - x1;
    ydiff = y2 - y1;

    firstpart = (xdiff);
    return firstpart;
    secondpart = (ydiff);
    firstpart2 = pow(xdiff,2);
    secondpart2 = pow(ydiff, 2);

    total = firstpart2 + secondpart2;
    lengthformula = sqrt(total);
    return lengthformula;
}

task main()
{
    getdistance(0, 4, 0, 1);
    while (true){
        motor[RightsideB]= vexRT[Ch2];
        motor[RightsideF] = vexRT[Ch2];
        motor[LeftsideB]= vexRT[Ch3]; 
        motor[LefttsideF]= vexRT[Ch3];

    }

}

您可以使用<代码>距离>代码>您需要的任何东西。

如果在函数的中间有<代码>返回<代码>,函数的其余部分不执行。您不将返回值赋给<代码>主体()/<代码>。返回函数应该在哪里?我如何在main(0)中指定一个return,我尝试为getdistance指定它,但我遇到了重大错误。回想一下getdistance函数是一个空函数,因此我无法放置return getdistance(0,4,0,1)你似乎不明白返回x1的作用。我想你该回到课本上学习基础知识了。我可以用它作为全局变量吗?在我的回答中,它是一个局部变量。你可以全局声明它,然后只分配
distance=getdistance(0.4,0,1)
。我可以通过调试器看到值?顺便说一句,谢谢你的帮助。我太迷路了。我们的首席程序员没有任何线索。他有很多经验,但他从未使用过。是的,你可以在调试器中看到所有变量的值。
task main() {
    float distance = getdistance(0, 4, 0, 1);
    ...
}