day-06 Introduction to Functions
day-06 Introduction to Functions
This code will not run, but it shows how functions are used in general.
Day-6
Defining a function
Introducing Functions Give the keyword def , which tells Python that you are about to define a function.
Give your function a name. A variable name tells you what kind of value the variable contains; a
function name should tell you what the function does.
One of the core principles of any programming language is, "Don't Repeat Yourself". If you have an action that Give names for each value the function needs in order to do its work.
should occur many times, you can define that action once and then call that code whenever you need to carry These are basically variable names, but they are only used in the function.
out that action. They can be different names than what you use in the rest of your program.
These are called the function's arguments.
We are already repeating ourselves in our code, so this is a good time to introduce simple functions. Functions Make sure the function definition line ends with a colon.
mean less work for us as programmers, and effective use of functions results in code that is less error-prone. Inside the function, write whatever code you need to make the function do its work.
General Syntax
A general function looks something like this:
---------------------------------------------------------------------------
In [4]: def thank_you(name): NameError Traceback (most recent call last)
# This function prints a two-line personalized thank you message. <ipython-input-1-a1b6b8373f44> in <module>()
print("\nYou are doing good work, %s!" % name) ----> 1 thank_you('Adriana')
print("Thank you very much for your efforts on this project.") 2 thank_you('Billy')
3 thank_you('Caroline')
thank_you('Adriana') 4
thank_you('Billy') 5 def thank_you(name):
thank_you('Caroline')
NameError: name 'thank_you' is not defined
You are doing good work, Adriana!
Thank you very much for your efforts on this project.
On the first line we ask Python to run the function thank_you(), but Python does not yet know how to do this
You are doing good work, Billy!
Thank you very much for your efforts on this project. function. We define our functions at the beginning of our programs, and then we can use them when we need to.
Now the rest of our program is cleaner, because it gets to focus on the things we are changing in the list, rather
than having code for printing the list. We define the list, then we sort it and call our function to print the list. We
sort it again, and then call the printing function a second time, with a different message. This is much more
readable code.
We write a set of instructions once. We save some work in this simple example, and we save even more
work in larger programs.
When our function works, we don't have to worry about that code anymore. Every time you repeat code in
your program, you introduce an opportunity to make a mistake. Writing a function means there is one place Returning a Value
to fix mistakes, and when those bugs are fixed, we can be confident that this function will continue to work
Each function you create can return a value. This can be in addition to the primary work the function does, or it
correctly.
can be the function's main job. The following function takes in a number, and returns the corresponding word for
that number:
You can think of functions as a way to "teach" Python some new behavior. In this case, we taught Python how to
create a list of students using hyphens; now we can tell Python to do this with our students whenever we want to.
# Let's try out our function. # This line will never execute, because the function has already
for current_number in range(0,6): # returned a value and stopped executing.
number_word = get_number_word(current_number) print("This message will never be printed.")
print(current_number, number_word)
# Let's try out our function.
for current_number in range(0,6):
0 zero
number_word = get_number_word(current_number)
1 one
print(current_number, number_word)
2 two
3 three 0 zero
4 I'm sorry, I don't know that number. 1 one
5 I'm sorry, I don't know that number. 2 two
3 three
4 I'm sorry, I don't know that number.
If you use a return statement in one of your functions, keep in mind that the function stops executing as soon as 5 I'm sorry, I don't know that number.
it hits a return statement. For example, we can add a line to the get_number_word() function that will never
execute, because it comes after the function has returned a value:
More Later
There is much more to learn about functions, but we will get to those details later. For now, feel free to use
functions whenever you find yourself writing the same code several times in a program. Some of the things you
will learn when we focus on functions:
top
Write a function that takes in a person's name, and prints out a greeting. # put your code here
The greeting must be at least three lines, and the person's name must be in each line.
Use your function to greet at least three different people. In [ ]: # Ex 4.5 : List Exercises - Functions
Bonus: Store your three people in a list, and call your function from a for loop.
# put your code here
Full Names
Write a function that takes in a first name and a last name, and prints out a nicely formatted full name, in a top
sentence. Your sentence could be as simple as, "Hello, full_name."
Call your function three times, with a different name each time.
Addition Calculator
Challenges
Write a function that takes in two numbers, and adds them together. Make your function print out a sentence Lyrics
showing the two numbers, and the result.
Call your function with three different sets of numbers. Many songs follow a familiar variation of the pattern of verse, refrain, verse, refrain. The verses are the parts
of the song that tell a story - they are not repeated in the song. The refrain is the part of the song that is
repeated throughout the song.
Return Calculator
Find the lyrics to a song you like (http://www.metrolyrics.com/dont-stop-believin-lyrics-journey.html) that
Modify Addition Calculator so that your function returns the sum of the two numbers. The printing should follows this pattern. Write a program that prints out the lyrics to this song, using as few lines of Python code
happen outside of the function. as possible. hint
In [ ]: # Ex 4.1 : Greeter
Lyrics
# put your code here
Define a function that prints out the lyrics of the refrain. Use this function any time the refrain comes up in
the song.
In [ ]: # Ex 4.2 : Full Names