0

Soccer Robot C - Basic lessons - Functions

A warning: this is a difficult lesson. Take Your time and read many times the parts You do not understand.

As You try to put more and more functionality in Your program, loop() will grow. There could be many pages of text after some time and it would be very difficult to find the parts You need. Therefore, and for some other purposes, C++ offers functions. You can think of functions as blocks of code for a certain task. Any task. Like: "find a ball", "move the robot", "follow line", etc. The tasks may need some parameters, values that will determine how to do the task. For example, task "start motors" is usually not specific enough because the function has to know what the desired speed will be. Here, speed will be input parameter. On the other hand, task "measure distance" will not very useful, if the program doesn't let us know the result. The distance should be output parameter. Generally, functions have the following format:
outputParameter functionName(type1 inputParameter1, ...){
}
  • outputParameter - this is type of the output parameter. So, not the name, but the type, like: int, uint32_t, or bool.
  • functionName - this is a name You give to the function. It can by any You choose, as long as it follows C++ rules.
  • type1 - similar to output parameter, this is type of first input parameter
  • inputParameter1 - this is a name You give to first input parameter
You can have 0, 1, or more input parameters. An example:
void loop() {
}
We know this function already. Let's decipher its parts:
  • "void" - according to the explanation above, here should be the type of output parameter, but this isn't any. So we will extend our explanation: this is a place for type of output parameter or word "void". "void" means that there is no output parameter. The function has no information to return to the user ("caller").
  • "loop" is the name we chose. We could have written "loop", "loop_TEST", "strawberry", or (mostly) whatever we like here. However, it is advisable to choose a name that reflects well what the meaning of the block of code is.
  • "()" - the list of input parameters is empty - this function doesn't need any input information.
You probably noticed that this function was similar, but not the same, to the function we have been using by now:
void RobotSoccer::loop() {
}
We have a puzzling "RobotSoccer::" part here, which is out of scope of this short beginner's course. Think about it as "the next function will be part of RobotSoccer". How about a function that has input and output parameters? Here is a simple one:
int nextInteger(int inputNumber) {
	inputNumber++;
	return inputNumber;
}
Now we have both input and output parameters:
  • "int" - output will be an integer.
  • "nextInteger" - this name describes well intention of our function.
  • "(int inputNumber)" - we have 1 input parameter, an integer number, and its name is "inputNumber".
That was a header of the function, the part in which we describe inputs, outputs, and give it a name. The next part is body of the function, the part in which we put the code that takes input parameters (if any), does something, and returns output (if any). In our example:
                         {
	inputNumber++;
	return inputNumber;
}
  • "inputNumber++;" - this is the part that does something: it increases the input number by 1.
  • "return inputNumber;" - return is a new term. Its meaning is "return whatever follows out of the function". So, content of the variable "inputNumber" will go out of the function.
This is a rather difficult concept. Read the explanation till You understand it. Look at the extended example, too:
int nextInteger(int inputNumber) {
	inputNumber++;
	return inputNumber;
}

void RobotSoccer::loop() {
	int result = nextInteger(5);
	print("%i", result);
	end();
}
Now consider the program's flow. Let's assume that the program execution starts with loop().
  1. "int result = nextInteger(5);" - a variable named "result" will be an integer. C++ must first determine what the value of the part after the assignment ("=") sign is. It notices that this is a function and that the input parameter will be "5". The code execution of "loop()" is interrupted here (but will come back) and jumps to that function, "nextInteger()".
  2. "inputNumber" is set to value "5". This is an important step. Notice that this is in fact like executing "inputNumber = 5", although "5" comes from outside, as a way to inform "nextInteger()" what is our input to the function. Be sure that You understand this.
  3. "inputNumber++;" - an easy step, after which "inputNumber" will no more hold "5", but "6".
  4. "return inputNumber;" - another difficult concept: "return" will return "6" to the outside world (to the caller).
  5. "nextInteger()" ends here and the result, "6", travels back to the first line of "loop()".
  6. "loop()" continues execution and "result" is set to "6".
  7. The next line will print "6".
  8. Program ends.

Task: write a function.

Write a function that will return the difference between 2 numbers (common subtraction, "-").