返回功能不起作用。使用毕达哥拉斯定理

问题描述 投票:0回答:1
#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];

    }

}

这个程序将运行没有错误,但是当我运行它并打开调试器时,变量不起作用。我已经在smallbasic 中进行了编程,并且我知道这适用于它。我将其转换为 robots-c

c debugging robotics nxt
1个回答
1
投票

去掉

return
中的所有
getdistance
语句,除了最后的
return lengthformula
return
导致函数停止运行,并立即返回您指定的值,因此函数的其余部分永远不会运行,并且永远不会计算距离。

main()
中,需要将结果赋值给变量:

task main() {
    float distance = getdistance(0, 4, 0, 1);
    ...
}

然后您可以使用

distance
满足您的任何需要。

© www.soinside.com 2019 - 2024. All rights reserved.