Part 2, More on Functions
And we’re back! Let another day of Python begin!! Here’s where we left off:
So we know how to make numbers, and we know how to make functions that do stuff to those numbers. But what about other kinds of variables, letters? words? sentences? paragraphs? In the programming world we call those Strings!
Here’s how we make those:
Some important things to note about that code:
- We can make strings with single or double quotation marks “ “ or ‘’
- Variable names (and function names too!) can be as long as we want as long as they dont have spaces between them:
- We have to be sure that when we print, we put a space between our variables so that it doesnt print out JacobAronoff
Notice how our function has two variables as ‘parameters’ or ‘arguments’ and we can name them whatever we want
As you see we can name those parameters whatever
Now this is a bit strange, we have a parameter which is equal to ‘ ‘, we can then use this variable inside our method. We call these default parameters. Just because they’re default though, doesnt mean that we have to keep them the same everytime.
Ok so now the last part of basic functions is the magic ‘return’ keyword. Just like in algebra if we plug something in to a function, we expect something back right?
Really cool and powerful. As we learn more, it’ll be even better. Next time, we’re gonna go over control flow and how we can do more based on our variables