根据参数循环的被调用函数 [英] Called function that loops depending on the argument
问题描述
我有一个函数 LED_MAIN()
,它的工作是通过调用另一个函数 LED_MAIN_LINE()
来点亮 LED,这是它的私有函数,这意味着我在测试时我只会调用 LED_MAIN()
的代码.
I have a function LED_MAIN()
whose work is to light up LEDs by calling upon another function LED_MAIN_LINE()
which is private to it, meaning when I am testing the code I will only call LED_MAIN()
.
void LED_MAIN (void) //main function (public) to light all LEDs
{
int i;
int LED_NO_UP = 10 ; //number of LEDs on line going up
int LED_NO_DOWN = 10; //number of LEDs on line coming down
void LED_MAIN_LINE ( int no ); //private function to light LEDs on either side i.e on line going up and on line coming down
for (i=0 ; 1<20 ; i++) //first eliminate any errors in all 20 LEDs
{
LED_ERROR [i] = 0;
}
for ( i=0 ; i<LED_NO_UP ; i++ ) //light LEDs on one side. MAX 10 LEDs. LEDs #1-10
{
LED_MAIN_LINE (i);
}
for ( i=0 ; i<LED_NO_DOWN ; i++ ) //light LEDs on the other side. LEDs #11-20
{
LED_MAIN_LINE (i+10);
}
}
void LED_MAIN_LINE ( int no ) //definition of the actual LED lighting function (private to void LED_MAIN (void))
{
//currently no reference to argument int no in this function
for (i = 10 ; i > 0 ; i++)
{
led_status [no][i] = led_status [no] [i-1]
}
// continued......
}
LED_MAIN_LINE()
应该为每个存在的 LED 执行.如果任一行上有 10 个 LED,它将循环 20 次 (10x2).如果有 5 个 LED,它只会执行 5 次 &很快.参数是 LED 的数量.然而,它被 void LED_MAIN()
调用,这就是循环发生的地方.
LED_MAIN_LINE()
is supposed to be executed for every LED that there is. If there are 10 LEDs on either line, it will loop 20 times (10x2). If there are 5 LEDs it will only be executed 5 times & so on. The argument is the number of LEDs. However, it is being called by void LED_MAIN()
and this is where the looping will happen.
但是,查看我对 void LED_MAIN_LINE()
的定义,我没有传递 LED 数量的参数.我已经在 LED_MAIN()
中做到了这一点.我想知道,这个定义是否正确?我应该在哪里传递预期使 LED_MAIN_LINE()
循环的 LED 数量?使用当前代码,我的测试结果不如预期.
Looking at my definition of void LED_MAIN_LINE()
however, I have not passed an argument for the number of LEDs. I have done this in LED_MAIN()
. I wonder, is this definition correct? Where should I pass the number of LEDs expected to make LED_MAIN_LINE()
loop? With the current code my test results are not as expected.
推荐答案
LED_NO_UP 和 LED_NO_DOWN 为 0.所以代码中的 for 循环永远不会执行,因为 i <当
是不可能的.i = 0
时,0
LED_NO_UP and LED_NO_DOWN are 0. So the for loops in your code are never executed because i < 0
is not possible when i = 0
.
在第一个 for 循环中还有 1<20
.
Also you have 1<20
in the first for loop.
这篇关于根据参数循环的被调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!