
More than anything else, programming is built on top of the idea of enclosed and reusable functionality. While it is possible to write a working program without the ability to enclose and reuse functionality, it would be a painful process.
Programming can seem like a complex and esoteric activity, but most parts of it are very straightforward. What a programmer does, essentially, is tell a computer the set of procedures it needs to do to accomplish a task. This is typically done in languages that are much more syntactically exacting than English, so it can be complicated, but the actual act is really simple: you give instructions which a computer will than replicate slavishly.
If you start to give a person instructions throughout their day, you’ll notice how frequently you’ll need to give a similar set of directives. Programming without functions is essentially limiting yourself to saying things like:
- Move your legs into a 90 degree angle at the knees, with your feet firmly on the floor
- If possible, brace and push up with your arms to begin to lift yourself
- Extend your knees while pushing off, such that your knees are at a 180 degree angle.
Hey look, you just got your human, rather awkwardly, into a standing position.
Encapsulated Functionality
If the instructions contained in that list didn’t give you sense or the frustration inherent in lacking the ability to say “Stand up” to your human, just trust me. You’d quickly tire of explaining to them how to walk out of the room: lift your left leg, swing it forward, place it on the ground, shift your weight to it, lift your right leg, etc…
This is why functions exist: they allow the conversation with a computer to take for granted the ideas like “stand up” and “walk forward”. By containing the procedures for those processes into functions we can call and have the computer follow, we save ourselves the time and frustration of repeating the individual steps.
PHP isn’t used for controlling humans, or human-like robots. That’s probably a good thing, but there are things that you want to do regularly on a web server that you don’t want to have to move through in a similarly explicitly way. That’s why PHP, like just about every other programming language in the world, has the idea of functions.
Anatomy of a function
Functions generally have three components: their name, their “parameters”, and their “body”. The name of a function should be simple, clear, and expressive. “Stand Up” is a pretty good function name. Though computers are weird about spaces, so depending on your specific preference you’d probably call it something like stand_up
or standUp
. (StandUp
could work, but it’s discouraged for reasons outside of the scope of this article.)
Here’s what a basic stand_up function might look like in PHP:
function stand_up($speed, $height = "full") {
// This analogy is breaking apart
// Pretend that I could sanely put a procedure here
}
Now “parameters” can sound pretty foreign, but essentially what it means is that there are specific traits of the way you’ll likely want the stand_up
function to occur. Perhaps you’d need to specify the speed at which the human would do it, or the final height they should achieve. You’ll want to convey this to the “body” of the function, so it’s procedure can know those goals. Parameters are the term for these sub instructions. These can also be optional, by providing in a function’s definitions some sane defaults. (A default of "full"
is defined for our $height
parameter.)
Finally the “body” of the function is the procedure. It’s where you’d likely put some version of the “Stand Up” instruction I outlined all those paragraphs ago. It may or may not depend on some parameters that are passed into the function when it’s called, and it may or may not return a value. Many functions, especially in the data-processing world or the web will return
values. This is how they pass a message back to the routine or function that called them about their success, failure, or result.
Functions are really the central concept of programming. At some level, as you’ve probably heard, all machine’s are just sending 1s and 0s around, but humans don’t speak binary very fluently, so programming languages were built to allow us to talk in something a little closer to our thoughts. Languages are essentially all built on top of each other into things that programmers are willing or able to speak on a consistent basis. But getting heavy into the trade-offs of various languages is definitely a topic for another time.
Image Credits: aka Kath
Pingback: WordPress Basics: Understanding And Making the Best Use of Your functions.php File | WPShout.com
Pingback: How to Link to Your WordPress Theme and Plugin Resources | WPShout