...
C++ | Blocks |
---|
Code Block |
---|
language | cpp |
---|
theme | Confluence |
---|
| while(a>0){
a = a-b;
/* executed over and over until a<0 or a==0 */
} |
|
|
To Do : Read up about for loop, do-while loop
Functions
- A function is a segment of code which can be invoked from different parts of the program in a parameterized manner
- A function involves 3 aspects
- Declaration (typically done in header files with extension .h)
int
Code Block |
---|
language | cpp |
---|
theme | Confluence |
---|
|
int add(int, |
- Definition (typically done by the library vendor)
...
Code Block |
---|
language | cpp |
---|
theme | Confluence |
---|
|
int add(int x, int y){ // x, y are function 'parameters' |
...
...
...
- Usage (usually, we need to do only this)
{
...
Code Block |
---|
language | cpp |
---|
theme | Confluence |
---|
|
{
… //other statements |
...
...
...
...
/* causes add() to be called with a, b passed as arguments. |
...
...
The value returned is assigned to c */
… //other statements |
...
Note : Blocks do not allow you to declare or define functions explicitly. However, some built-in functions such as digitalWrite() can be called using appropriate blocks, as shown below.
{
digitalWriteC++ | Blocks |
---|
Code Block |
---|
language | cpp |
---|
theme | Confluence |
---|
| {
digitalWrite(11, HIGH); |
|
Image Removed |
Image Added |
Setup() and loop() functions
- Setup and loop are functions called automatically (unlike all other functions needs to be called explicitly)
- setup() is called once, when you release the reset button / plug in your Arduino / upload a new code onto the board
- loop() is called repeatedly; anything inside the loop() gets executed over and over as long as the system remains powered on
Image Added
Note : When using blocks, any setup code required is automatically inserted into setup(). The main program logic which implemented using blocks goes into loop().
...
- Write detailed comments. This will be very useful later for you yourself or other people who read your code.
- Use descriptive variable names for variable etc. rather than short, generic names like i, j, k. This will help your code more readable and comprehensible.
- Indent your code properly. Indent with tabs, not with spaces. This also makes your code easier to read.
- Use constants/variables for values that appear at multiple places rather than hard-coding values.
Code Block |
---|
language | cpp |
---|
theme | Confluence |
---|
title | Poor Coding Style |
---|
|
void setup(){
//setup code
}
void loop()
{ // Poor position for opening brace.
int j = 100; // Non-descriptive variable name
analogWrite(5, j); /* Indented with spaces, and not aligned with the previous line.
Hard-coded values.
No comment to indicate what the line does.*/
} // Poor position for closing brace. |
Code Block |
---|
language | cpp |
---|
theme | Confluence |
---|
title | Good Coding Style |
---|
|
const int leftMotor = 5; // pin 5 used to control left motor of the robot
void setup(){
//setup code
}
void loop(){
int leftMotorSpeed = 100;
analogWrite(leftMotor, leftMotorSpeed); // to write the analog value 100 to pin 5.
} |