Defining a Function
Thats where functions step in. To create a function, you start with the keyword def. Then you give it a
unique name the video suggests naming a function that sings happy birthday as, aptly, happy birthday.
Follow that with parentheses and a colon :. Everything that the function does goes underneath,
indented, like this:
python def happybirthday: # Code for one verse of the Happy Birthday song goes here printHappy
birthday to you...
Invoking a Function Calling It
Now, just defining the function doesnt do anything yet. You need to invoke it, which is just a fancy way
of saying run it. To do that, you type the functions name followed by parentheses: happybirthday.
The instructor uses a really fun analogy here imagine the parentheses are like a pair of telephones.
Youre essentially calling the happy birthday function and saying, Hey Execute your code So calling
happybirthday will run the code within that function once. Want to sing it three times Just call it three
times: happybirthday; happybirthday; happybirthday
Thats where functions step in. To create a function, you start with the keyword def. Then you give it a
unique name the video suggests naming a function that sings happy birthday as, aptly, happy birthday.
Follow that with parentheses and a colon :. Everything that the function does goes underneath,
indented, like this:
python def happybirthday: # Code for one verse of the Happy Birthday song goes here printHappy
birthday to you...
Invoking a Function Calling It
Now, just defining the function doesnt do anything yet. You need to invoke it, which is just a fancy way
of saying run it. To do that, you type the functions name followed by parentheses: happybirthday.
The instructor uses a really fun analogy here imagine the parentheses are like a pair of telephones.
Youre essentially calling the happy birthday function and saying, Hey Execute your code So calling
happybirthday will run the code within that function once. Want to sing it three times Just call it three
times: happybirthday; happybirthday; happybirthday