owned this note
owned this note
Published
Linked with GitHub
# Functions signatures
:::info
Disclaimer. This is written in C++ but the concept of signature is similar in java and pseudocode. These are exercises for the students to check they proficiency in understanding functions when they are described.
:::
Reference:
https://developer.mozilla.org/en-US/docs/Glossary/Signature/Function
The concept of function signature (in general) includes
* name of method
* input (list of parameters and their type if it's a highly typed programming language)
* output type
* exceptions that can be passed or throwed (advanced concept)
* availability of the method in OOP (static, public, private)
:::warning
In IB the method signature is only the name, parameter list and their type.
:::
### Exercise 1
Write the signature (the definition of the function) of the following
findTheLeg is a function that given an hypotenuse and another leg is going to return a leg. Legs and hypothenuses are doubles.
:::spoiler
```cpp
double findTheLeg( double hypotenuse, double otherLeg)
```
:::
validateDNI is a function that given a string is going to return true if it's a valid DNI and false otherwise.
:::spoiler
```cpp
bool validateDNI(String x)
```
:::
createBigHouseInMinecraft is a function that it's going to try to create a big luxury house in survival in Minecraft. If it achieves it, the function is going to return true and is going to return false if there was an error
:::spoiler
```cpp
bool createBigHouseInMinecraft()
```
:::
createBigHouseInMinecraft is a function that it's going to try to create a big luxury house in survival in Minecraft. There is a parameter x that is the IP where the server is (the object IP stores IP addresses) If it achieves it, the function is going to return true and is going to return false if there was an error
:::spoiler
```cpp
bool createBigHouseInMinecraft(IP server)
```
:::
There is an object to store Vectors called PVector. We are going to implement a function to add 2 of these vectors returning the PVector resulted from them. The function is called addPVector
:::spoiler
```cpp
PVector addPVector (PVector x, PVector y)
```
:::
Define a function that is going to print the n number of the fibonacci series given as an integer. The name of the function is printFib
:::spoiler
```cpp
void printFib (int n)
```
:::
Define a function that is going to sound a scale given the number of the pin where the buzzer is attached. The name of the function is soundScale.
:::spoiler
```cpp
void soundScale (int pin)
```
:::
Define a function that is going to sound a scale given the number of the pin where the buzzer is attached and a boolean that is true if we want to play a chormatic scale and false otherwise. The name of the function is soundScale
:::spoiler
```cpp
void soundScale (int pin, bool chromatic)
void soundScale ( bool chromatic, int pin)
```
:::
Define a function that is given the milliseconds that have passed, is going to return if it's time to go to the next class or not. The name of the function is nextClassAlready. Milliseconds use the type long.
:::spoiler
```cpp
bool nextClassAlready(long milliseconds)
```
:::
Define a function that is given the milliseconds that have passed, is going to return if it's time to go to the next class or not. The coding for knowing if it's the next class is that is going to return 1 if we have to go to the next class and 0 otherwise. The name of the function is nextClassAlready. Milliseconds use the type long.
:::spoiler
```cpp
int nextClassAlready(long milliseconds)
```
:::
:::info
The function digitalRead(int pin) returns an int. 1 (HIGH) or 0 (LOW)
:::
Define the Function HTTPConnect that is going to return a code that is an integer for the status of the connection given the URL as a string
:::spoiler
```cpp
int HTTPConnect(string URL)
```
:::
### Exercises 2
Define the function startWithQuixote that if you send an String is going to return an string that is the same but it will start with "En un lugar de la Mancha... ".
:::spoiler
```cpp
String startWithQuixote(String s)
```
:::
Define a function that is going to output on a screen (or through serial) the inverse (1/x) of the integer number that you send to it. The name of the function is inverseNumber
:::spoiler
```cpp
void inverseNumber(int n)
```
:::
Define a function in C++ that is going to return true if it's a "good day" and false otherwise. The name of the function is "isItAGoodDay".
:::spoiler
```cpp
bool isItAGoodDay()
```
:::
Define a function in C++ that is going to return you the volume of a cilinder given it's radius and it's height. Numbers can be decimals. The name of the function is volumeCilinder.
double volumeCilinder(double radio, double height)
Define a function called "isThereAnyBeetlejuice" that is going to analyse a string given as a parameter and is going to return true if the name "Beetlejuice" is part of the string and false otherwise

:::spoiler
```cpp
bool isThereAnyBeetlejuice(string x)
```
:::
Define a function that is called notOver5 that given an integer is going to return the same integer if it's not over 5 and 5 if it's equal or bigger than five.
:::spoiler
```cpp
int notOver5 (int n)
```
:::
## Variations of lullaby
These are all functions that they do the same thing (mostly) but they are defined each somewhat differently so they have different parameters and return types. You can use this to notice different partterns.

_jigglypuff_
Define a function called singLullaby that is going to tone a lullaby when called.
:::spoiler
```cpp
void singLullaby()
```
:::
Define a function called singLullaby that is going to tone a lullaby when called from a number of possible lullaby songs. The number of the song has to be provided.
:::spoiler
```cpp
void singLullaby(int song)
```
:::
Define a function called singLullaby that is going to tone a lullaby when called from a number of possible lullaby songs. The number of the song has to be provided, also the pin where the buzzer is set.
:::spoiler
```cpp
void singLullaby(int song, int pin)
```
:::
Define a function called singLullaby that is going to tone a lullaby when called. Since singing is difficult, this method will return true if has achieved to sing the full lullaby and false if there was any kind of error during execution.
:::spoiler
```cpp
bool singLullaby()
```
:::
Define a function called singLullaby that is going to tone a lullaby when called. Since singing is difficult, this method will return 1 if has achieved to sing the full lullaby and 0 if there was any kind of error during execution.
:::spoiler
```cpp
int singLullaby()
```
:::
Define a function called singLullaby that is going to tone a lullaby when called. Since singing is difficult, this method will return a code that will go from 100 to 600 like if it was a http request.
:::spoiler
```cpp
int singLullaby()
```
:::
Define a function called singLullaby that is going to tone a lullaby when called from a number of possible lullaby songs. The number of the song has to be provided, also the pin where the buzzer is set. Since singing is difficult, this method will return a code that will go from 100 to 600 like if it was a http request.
:::spoiler
```cpp
int singLullaby(int song, int pin)
```
:::
Define a function called singLullaby that is going to tone a lullaby when called from a number of possible lullaby songs. The number of the song has to be provided, also the pin where the buzzer is set.
Since singing is difficult, this method will return true if has achieved to sing the full lullaby and false if there was any kind of error during execution.
:::spoiler
```cpp
bool singLullaby(int song, int pin)
```
:::
Define a function called singLullaby that is going to tone a lullaby when called. It will print "song is over" through serial when it finishes playing the song.
:::spoiler
```cpp
void singLullaby()
```
:::