VEXcode Pro V5 में मान लौटाने के लिए फ़ंक्शन लिखना

The VEX Visual Studio Code Extension has replaced VEXcode Pro V5, which is now end-of-life.

VEXcode Blocks and VEXcode Text remain actively developed and supported for all VEX platforms.

उदाहरण: एक सरल महत्तम समापवर्तक (जीसीडी) फलन (यूक्लिड एल्गोरिदम के साथ)

निम्नलिखित नमूना यह भी दर्शाता है कि इसका उपयोग सरल लूप संरचना में किस प्रकार किया जा रहा है।

उपयोगकर्ता-परिभाषित फ़ंक्शन का उपयोग किए बिना:

int main() { 
  int remainder = 1;
  int a1, b1, a2, b2;
  vexcodeInit();
  a1 = a2 = 20;
  b1 = b2 = 64;
  while(remainder > 0){
    remainder = a1 % b1;
    a1 = b1;
    b1 = remainder;
  }
  Brain.Screen.printAt(5, 60, "GCD ( %d, %d ) = %d", a2, b2, a1);

  a1 = a2 = 60;
  b1 = b2 = 200;
  while(remainder > 0){
    remainder = a1 % b1;
    a1 = b1;
    b1 = remainder;
  }
  Brain.Screen.printAt(5, 60, "GCD ( %d, %d ) = %d", a2, b2, a1);
}

उपयोगकर्ता-परिभाषित फ़ंक्शन का उपयोग करना:

int getGCD(int a, int b){
  int शेषफल = 1;
  while(शेषफल>0){
    शेषफल = a % b;
    a = b;
    b = शेषफल;
  }
  a लौटाएँ;
}

int main() { 
  vexcodeInit();
  Brain.Screen.printAt(5, 60, "GCD ( %d, %d ) = %d",getGCD(20, 64));
  Brain.Screen.printAt(5, 60, "GCD ( %d, %d ) = %d",getGCD(60, 100));
}

नीचे दिया गया वीडियो दिखाता है कि कॉलर फ़ंक्शन main() से लॉजिक का क्रम कैसे आगे बढ़ता है:

उदाहरण: संख्याओं के एक समूह का औसत निकालना

float doAverage(int n1, int n2, int n3){
  float avg = (n1 + n2 + n3)/3;
  return avg;
}

int main() { 
  int n1, n2, n3;
  Brain.Screen.print("average(%d, %d, %d) = %d", 10, 20, 30); 
  doAverage(10, 20, 30);

  n1=10; n2=20; n3=40;
  Brain.Screen.print("average(%d, %d, %d) = %d", n1, n2, n3);
  doAverage(n1, n2, n3);
}

उदाहरण: परिवर्तनीय एनकोडर मान के साथ रोबोट को आगे बढ़ने में सक्षम बनाना

void reportMotionValues(){
  Brain.Screen.printAt(5,100, "%8.2lf%8.2lf%8.2f",
    LeftMotor.position(rev),
    LeftMotor.position(deg));
}

void goStraight(float totalEnc, float vel){
  LeftMotor.resetPosition();
  LeftMotor.setVelocity(50.0, percent);
  LeftMotor.spinToPosition(totalEnc, deg, true);
  return;
}

int main() { 
  int enc=1000; // >0: आगे, <0: पीछे
  vexcodeInit();

  for(int i=0; i<10; i++){
    enc *= -1;
    goStraight(enc, 100);
    Brain.Screen.printAt(5,60, "Done");
    reportMotionValues();

    //यह विलंब केवल आपको इन क्रियाओं को देखने के लिए है।
    यहां आपको विलंब लगाने की आवश्यकता नहीं है।
    प्रतीक्षा करें(2000, मिलीसेकंड);
  }
}

जैसा कि आप देख सकते हैं, रोबोट को अलग-अलग समय पर आगे और पीछे जाने में सक्षम बनाना लगभग असंभव है; क्योंकि यह केवल रन-टाइम पर ही निर्धारित किया जा सकता है।

For more information, help, and tips, check out the many resources at VEX Professional Development Plus

Last Updated: