This tutorial is both written in Dutch and English. We'll start with English.
Deze tutorial is zowel in het Engels als in het Nederlands geschreven. Eerst komt Engels.
ENGLISHINTRODUCTION
Definition : segmenting code into functions allows a programmer to create modular pieces of code that perform a definedtask and then return to the area of code from which the functionwas "called".... Functions codify one action in one place so that the functiononly has to be thought out and debugged once.
CREATE YOUR FIRST METHODTo create a method or function (it's the same) we first need to make a new sketch. Put it into a new folder named "Learn Functions" and rename the sketch to "First Method".
A method is alway declared outside of the voidloop()
or the voidsetup()
.
Here's a quick example of what it should look like:
void MyFunction()
{
// here comes the code of your function, for example :
// Serial.print("This is my function");
}
1) Declaration : This function is a void
, it performs a series of actions.
2) Naming : This method is named myFunction()
it must have braces at the end of it's name.
3) Code : Between the curly braces come's what the function should do.
"CALLING" YOUR FIRST METHODTo call your newly created function you must put a reference to it in the "voidloop()
". You call it by putting the name of your function in the "voidloop()
".
void loop()
{
MyFunction();
}
void MyFunction()
{
Serial.print("This is my function");
}
Wait a second, the "voidloop()
" looks like a function! Yes, your completely right, it is a built in method!
Now you've learnt about the "void
" type, we will introduce you to a comletely new type!
Now you are familiar with the "void
" - type, you'll learn a new way of using functions. But don't worry! It's not that different!
In this example we calculate a value with two variables who are already declared.
int x = 51;
int y = 7;
int outcome;
void setup()
{
}
void loop()
{
outcome = MultyplyNumbers();
}
int MultyplyNumbers()
{
return x * y;
}
So, let's list what's different from the "void
" - method:
- instead of "
void
" there's "int
"
- there's a "
return
" at the end of the method
The question is now: What does this all mean?
Well, the "int
" means it should return an integer, more about the "return
" later. What it basically means, is that the function will 'be' an integer. "outcome
" is an integer, so our function should also 'be' an integer.
The "return
" - statement is what the function's final result should be, in this case 357.
In the last example we multiplied two numbers, but what if we had a lot of number that should be multiplied, well there's a solution for that : arguments.
An argument is put between the braces in the name of your method.
int a = 1;
int b = 7;
int c = 5;
int d = 8;
int outcome1;
int outcome2;
int outcome3;
void setup()
{
}
void loop()
{
outcome1 = MultyplyNumbers(a , b); //outcome1 = 7
outcome2 = MultyplyNumbers(c , d); //outcome2 = 40
outcome3 = MultyplyNumbers(outcome1 , outcome2); //outcome3 = 280
}
int MultyplyNumbers(int x , int y)
{
return x * y;
}
Now we've added arguments, so our function can be used with different numbers. In the method x and y are the arguments, these can be filled in while calling the method.
CONCLUSIONI think we can all agree that functions make our code...
- More organized
- More compact
- More "errorless" because you have to write it only once
- More Readable
if you still have questions, make sure to leave a comment below!
NEDERLANDSINTRODUCTIE
Definitie : door code in functies op te delen, kan een programmeur modulaire stukjes code maken die een taak uitvoeren en vervolgens terugkeren naar het codegebied van waaruit de functie werd "geroepen" ... Functies coderen één actie op één plaats zodat de functie maar 1 keer moet worden uitgedacht en gedebugd.
MAAK JE EERSTE FUNCTIEOm een methode of functie te maken (het is hetzelfde), moeten we eerst een nieuwe schets maken. Zet het in een nieuwe map genaamd "Leer functies" en hernoem de schets naar "Eerste functie".
Een methode wordt altijd gedeclareerd buiten de "void loop()
" of de "void setup()"
.
Hier is een voorbeeld van hoe het eruit zou moeten zien:
void MijnFunctie()
{
Serial.print("dit is mijn functie");
}
1) Verklaring: deze functie is een "void
", het voert een reeks acties uit.
2) Naamgeving: deze methode heet MijnFunctie() en moet aan het einde van de naam haakjes hebben.
3) Code: Tussen de accolades komt wat de functie moet doen.
JE EERSTE FUNCTIE "ROEPEN"Om uw nieuw aangemaakte functie op te roepen, moet je er een verwijzing naar plaatsen in de "voidloop ()
". Je roept het door de naam van je functie in de "voidloop ()
" te zetten.
void loop()
{
MijnFunctie();
}
void MijnFunctie()
{
Serial.print ("Dit is mijn functie);
}
Wacht even, de "voidloop ()
" ziet eruit als een functie! Ja, je hebt helemaal gelijk, het is een ingebouwde methode!
Nu je hebt geleerd over het "void
" type, zullen we je voorstellen aan een compleet nieuw type!
Nu u bekend bent met het "void
" -type, leert u een nieuwe manier om functies te gebruiken. Maar maak je geen zorgen! Het is niet zó anders!
In dit voorbeeld berekenen we een waarde met twee variabelen die al gedeclareerd zijn.
int x = 51;
int y = 7;
int uitkomst;
void setup ()
{
}
void loop ()
{
uitkomst = VermenigvuldigNummers();
}
int VermenigvuldigNummers()
{
return x * y;
}
Dus laten we een lijst maken van wat er anders is dan de "void" -methode:
- in plaats van "
void
" is er "int
"
- er is een "
return
" aan het einde van de methode
De vraag is nu: wat betekent dit allemaal? Welnu, de "int
" betekent dat het een integer moet retourneren, later meer over de "return
". Wat het in feite betekent, is dat de functie een integer zal 'zijn'.
"uitkomst
" is een integer, dus onze functie moet ook een integer 'zijn'. Het "return
" - statement is wat het uiteindelijke resultaat van de functie zou moeten zijn, in dit geval 357.
In het laatste voorbeeld hebben we twee getallen vermenigvuldigd, maar wat als we veel getallen hadden die moeten worden vermenigvuldigd? Dan is daar een oplossing voor: argumenten.
In de naam van je methode wordt een argument tussen de accolades geplaatst.
int a = 1;
int b = 7;
int c = 5;
int d = 8;
int uitkomst1;
int uitkomst2;
int uitkomst3;
void setup ()
{
}
void loop ()
{
uitkomst1 = VermenigvuldigNummers(a, b); // uitkomst1 = 7
uitkomst2 = VermenigvuldigNummers(c, d); // uitkomst2 = 40
uitkomst3 = VermenigvuldigNummers(uitkomst1, uitkomst2); // uitkomst3 = 280
}
int VermenigvuldigNummers (int x, int y)
{
return x * y;
}
Nu hebben we argumenten toegevoegd zodat onze functie met verschillende getallen kan worden gebruikt. In de methode zijn x en y de argumenten, deze kunnen worden ingevuld tijdens het aanroepen van de methode.
CONCLUSIEIk denk dat we het er allemaal over eens zijn dat functies onze code...
- Meer georganiseerd maken
- Compacter maken
- "Foutlozer", omdat je het maar één keer hoeft te schrijven, maken
- Meer leesbaar maken
Als je nog vragen hebt, laat dan hieronder een reactie achter!
Comments
Please log in or sign up to comment.