9. Programming (4) Flashcards

1
Q

Why are functions useful?

A
  1. They let us split the code into smaller subsections which are easier to understand.
  2. Let us make units of code (our functions) that can easily be reused.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to define a function called myFunction?

A
int myFunction(int parameter1, float parameter2)
{
// write the function body in here
return intVal;
}
Where you can have as many or few parameters as you want and the return value has to be of the same type as the header.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to write a function that will add 2 numbers?

A
int add(int number1, int number2)
{
int answer;
answer = number1 + number2;
return answer;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Can you show the newly defined add function being used?

A

int sum;
sum = add(4,7); // sum is set to 11
sum = add(5,6,7); // ERROR - needs 2 parameters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to define a function that will flash an LED?

A
void flash(int interval)
{
digitalWrite(LEDpin, HIGH);
delay(interval);
digitalWrite(LEDpin, LOW);
delay(interval);
return; // this line is optional because no return value
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Example usages of flash function?

A

flash(1000); // a slow 1 sec flash
flash(100); // much faster
flash(); // ERROR - need a parameter

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does void return type mean?

A

No return statement is required.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what can the for loop be used for?

A
repeating an action a number of times:
for (A; B; C)
{
// things to be repeated
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

what are A B and C in the above question?

A
  • A is the variable initialisation for the loop
  • B is the continuation condition
  • C is the variable update
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

how does:
int x = 0;
for (int i=0; i

A

int i=0 - this initialises an integer variable called i to be 0.
i

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

how to work out factorial 5 using a for loop?

A

int x = 1;

for (int i=1; i

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what does for(; ;) do?

A

creates an infinite loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

how to get out of an infinite loop?

A

use break keyword; very useful if combined with an if statement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

what is a better way of setting up a loop which stops when something happens?

A
the do.. while() loop does something(s) as long as a condition holds. 
do
{ // things to repeat
}
while(condition)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

what are the numbers for false and true?

A

false - 0

true - 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly