Difference between revisions of "Functions default params"

From ROBOTC API Guide
Jump to: navigation, search
(Created page with "{{DISPLAYTITLE:Functions with Default Values for Parameters}} <yambe:breadcrumb self="Functions with Default Values for Parameters">Programming_Tips_Tricks|Programming Tips Tr...")
 
 
(5 intermediate revisions by one user not shown)
Line 2: Line 2:
 
<yambe:breadcrumb self="Functions with Default Values for Parameters">Programming_Tips_Tricks|Programming Tips Tricks</yambe:breadcrumb>
 
<yambe:breadcrumb self="Functions with Default Values for Parameters">Programming_Tips_Tricks|Programming Tips Tricks</yambe:breadcrumb>
 
<br />
 
<br />
 +
 +
Functions can have default values for their parameters, for example:<br />
 +
{|
 +
|-
 +
|<syntaxhighlight lang="ROBOTC">
 +
void forward( int speed = 100 )
 +
{
 +
    motor[ port2 ] = speed; 
 +
}
 +
 +
task main()
 +
{
 +
    // Forward at default
 +
    forward( );
 +
 
 +
    // Forward at speed 10
 +
    forward( 10 );
 +
 
 +
    // Do nothing
 +
    while( true ){
 +
      wait10Msec(500);
 +
    }
 +
}
 +
</syntaxhighlight>
 +
|}
 +
If the function "forward" is called with no parameters then the default of 100 is used.
 +
<br />
 +
  
 
{{tip-from-author|name=jpearman|link=http://www.vexforum.com/showpost.php?p=220744&postcount=1}}
 
{{tip-from-author|name=jpearman|link=http://www.vexforum.com/showpost.php?p=220744&postcount=1}}

Latest revision as of 19:58, 15 May 2012

General ProgrammingProgramming Tips Tricks → Functions with Default Values for Parameters


Functions can have default values for their parameters, for example:

void forward( int speed = 100 )
{
    motor[ port2 ] = speed;   
}
 
task main()
{
    // Forward at default
    forward( );
 
    // Forward at speed 10
    forward( 10 );
 
    // Do nothing
    while( true ){
       wait10Msec(500);
    }
}

If the function "forward" is called with no parameters then the default of 100 is used.


This tip was posted by jpearman over at http://www.vexforum.com/showpost.php?p=220744&postcount=1.