Exercise 18: Names, Variables, Code, Functions
Big title, right? I am about to introduce you to the function! Dum dum dah! Every programmer will go on and on about functions and all the different ideas about how they work and what they do, but I will give you the simplest explanation you can use right now.
Functions do three things:
- They name pieces of code the way variables name strings and numbers.
- They take arguments the way your scripts take ARGV.
- Using 1 and 2 they let you make your own "mini-scripts" or "tiny commands."
You can create a function by using the word def in Ruby. I'm going to have you make four different functions that work like your scripts, and I'll then show you how each one is related.
Let's break down the first function, print_two, which is the most similar to what you already know from making scripts:
- First we tell Ruby we want to make a function using def for "define".
- On the same line as def we give the function a name. In this case we just called it "print_two" but it could also be "peanuts". It doesn't matter, except that your function should have a short name that says what it does.
- Then we tell it we want *args (asterisk args) which is a lot like your argv parameter but for functions.
- Then we end this line with a newline (ENTER key), and start indenting.
- After the newline all the lines up to the end line at the bottom will become attached to this name, print_two. Our first indented line is one that unpacks the arguments the same as with your scripts.
- To demonstrate how it works we print these arguments out, just like we would in a script.
The problem with print_two is that it's not the easiest way to make a function. In Ruby we can skip the whole unpacking arguments and just use the names we want right inside (). That's what print_two_again does.
After that you have an example of how you make a function that takes one argument in print_one.
Finally you have a function that has no arguments in print_none.
This is very important. Do not get discouraged right now if this doesn't quite make sense. We're going to do a few exercises linking functions to your scripts and show you how to make more. For now just keep thinking "mini-script" when I say "function" and keep playing with them.
What You Should See
If you run ex18.rb you should see:
Right away you can see how a function works. Notice that you used your functions the way you use things like exists, open, and other "commands." In fact, I've been tricking you because in Ruby those "commands" are just functions. This means you can make your own commands and use them in your scripts too.
Create a function checklist for later exercises. Write these checks on an index card and keep it by you while you complete the rest of these exercises or until you feel you do not need the index card anymore:
- Did you start your function definition with def?
- Does your function name have only characters and _ (underscore) characters?
- Did you put an open parenthesis ( right after the function name?
- Did you put your arguments after the parenthesis ( separated by commas?
- Did you make each argument unique (meaning no duplicated names)?
- Did you put a close parenthesis ) after the arguments?
- Did you indent all lines of code you want in the function two spaces?
- Did you end your function with end lined up with the def above?
When you run ("use" or "call") a function, check these things:
- Did you call/use/run this function by typing its name?
- Did you put the ( character after the name to run it?
- Did you put the values you want into the parenthesis separated by commas?
- Did you end the function call with a ) character?
- Functions that don't have parameters do not need the () after them, but would it be clearer if you wrote them anyway?
Use these two checklists on the remaining lessons until you do not need them anymore.
Finally, repeat this a few times to yourself:
"To 'run,' 'call,' or 'use' a function all mean the same thing."
Common Student Questions
- What's allowed for a function name?
- The same as variable names. Anything that doesn't start with a number, and is letters, numbers, and underscores will work.
- What does the * in *args do?
- That tells Ruby to take all the arguments to the function and then put them in args as a list. It's like argv that you've been using, but for functions. It's not normally used too often unless specifically needed.
- This feels really boring and monotonous.
- That's good. It means you're starting to get better at typing in the code and understanding what it does. To make it less boring, take everything I tell you to type in, and then break it on purpose.