how I feel all the time |
Functions are super awesome as they are what make everything happen. The simplest way to describe it is that a function is an action.
Continuing on with the jar and honey analogy from Part 1,
let’s say you have your jar. It’s filled with honey, so that’s good. But having
the jar and the information that it’s full of honey doesn’t really accomplish
much. In order to interact with that jar, and indeed for the jar to be useful, you
must perform actions, or functions. Here’s a function:
function openJar()
{
//code that lets us open the jar
}
{
//code that lets us open the jar
}
*sidebar: The two forward
slashes make what follows a comment, meaning that it will not be read by the
compiler; it is purely for the developer to see. Comments are fantastic and you
need to use them to describe your code, because when you come back to your code
after a month you will have no idea what you wrote or why you wrote it. So
comment often!*
So this function doesn’t actually do anything because no
code is written inside of it. In order for a function to…function, we need to
have information that it can use. Here’s a semi-working function:
bool closed = true;
void openJar()
{
if(closed == true)
{
//we open the jar by switching the value to false
closed = false;
}
else
{
//jar is already open; no need to open it again
}
}
{
if(closed == true)
{
//we open the jar by switching the value to false
closed = false;
}
else
{
//jar is already open; no need to open it again
}
}
Okay, let’s walk this through bit by bit:
bool closed = true;
This should
look familiar from Part 1, but with a different DATA TYPE. Remember that bool or boolean is a true or false value, which
numerically is 1 or 0. closed is the name of the boolean
variable. true is the value of our closed boolean variable.
Okay that
was simple. Let’s see what we can make of the next bit:
void openJar()
Funnily
enough, this has the same basic structure as we discussed with variables. void is the return value of the data
type. A return value is a value that, when the function is run, is returned to
the program. You can actually call a function and put that return value into a
variable, though that might not make sense yet. We haven’t talked about void before, but basically it means that
when this function is called it doesn’t return any sort of value, it just runs
through the function and that’s it.
openJar is the name of the function, just
like closed is the name of our variable. It’s
just a way to easily identify the function. Simple.
() is a new sign for us. () in this context is known as a CALL NOTATION. I don’t really
know what this does in the background, but it’s what lets the compiler know a
function is being called. Sorry I can’t go into more detail. I don’t know
enough about the inner workings, only how to use it. Just like I can watch TV
but I couldn’t explain exactly how it works.
Next up is {}. These brackets are the beginning and end of a function or
statement. All brackets need to be in pairs. This can get confusing when there
are nested statements (meaning sets of brackets within sets of brackets within
sets of brackets, etc), so always create a set of brackets, never just one.
Anyhoo, everything within these first brackets are the openJar function.
Next up is,
again, something new:
if(closed
== true)
{
closed = false;
}
else
{
}
{
closed = false;
}
else
{
}
This is
called an “if-else statement”. Basically, it asks a question about the value of
a variable. Then, based upon the answer, it will output one of a number of
actions. While there are () in this line, they do not have the
same purpose as the call notation. Here they are grouping together the two
sides of the question being asked. In this case we’re asking if (closed equals true), then do whatever is in the following
brackets. if (closed does not equal true) (this is the else part) then do whatever is in the next
set of brackets.
The two
equal signs (==) act as a comparison, or a
conditional, asking whether or not two things are the same. In variables one
equal sign (=) means we are assigning a value, saying
that closed is true. So
the if statement is looking to see if closed equals the same value as true. It checks the value of the boolean
variable closed, which we declared and assigned in
the beginning, with the value true. Because the value of closed is true, the program runs through and sets closed to false, which means that our jar is now
open. The second half, the else part of the statement, will not be
looked through, because the condition in the if part
of the statement was found to be true.
However, if
we were to run the function a second time in a row it would jump to the else part of the statement. Can you think of why this is?
Did you
think about it, or are just skipping ahead to find the answer?
The answer
is because we’ve changed the value of closed, meaning the if part of the statement will not be true, so it moves on to the next part (else).
This is a
very basic function, but it’s a lot to process, so we’ll leave it there for
now. If you think of functions like a word problem, you should have an easier
time. I tend to write out on paper in English what I want my functions to do,
then I put that into the syntax of the program. Sometimes it works how I
envisioned, but sometimes I have to rework it. That’s half the fun!
I’m not
sure what we’ll cover next time, but I hope you stay tuned for the next
installment of Basics of Programming.
Part 1: Variables | Part 3: Operators | Part 4: Loops
Part 1: Variables | Part 3: Operators | Part 4: Loops
No comments:
Post a Comment