Function Inputs and Outputs
Function Inputs and Outputs
If your function wants to receive input data, those data values should
be identified by names that are listed inside the parentheses in the
function "def" statement. You can list any number of names inside the
parentheses; each represents a data value that must be passed into the
function.
Each parameter name acts like a variable within the function. The
named parameters hold data and can be used in expressions and
assignment statements just like any other variable! These parameters
are how your function receives and processes data.
The illustration below shows the function greet() with two parameters,
name and town, defined inside the parentheses.
:
When these parameters are present, every time the greet() function is
called, two input values must be provided in the function call. We have
shown greet() being called with "Rudy" as the first value and
"Chicago" as the second value. The first value is automatically stored in
the first parameter (name) and the second value is stored in the
second parameter (town). We can then use name and town inside the
function body as regular variables that hold data.
The code below uses our greet() function example with two input
:
parameters. Try running it to confirm the expected output. You can
change the input values and watch how the output changes to match.
Try It Now
Console Output...
As you can see, inside the greet() function, the name and town
variables are used just like any other standard variables.
Default Parameters
Now, when someone calls the greet() function, they can choose to
pass in just the name, or to provide both name and town. If a value for
town is missing, "Atlanta" will be automatically assigned instead. Run
the code below to see default parameters in action!
:
Try It Now
Console Output...
Named Parameters
You may want to call a function and specify just one among a number
of optional parameters. Or, you may not remember the right order of
the positional parameters. Fortunately, in Python, you can make
function calls and provide the name of the parameter directly in the
call! That way, you are sure that a particular value goes to a specific
parameter, even if you've gotten things out of order.
Try It Now
:
Console Output...
Return Values
Often, functions are built to return some useful data to the calling
program. The data might be a simple number, string or Boolean value,
or it could be a list, tuple or more complex object. When your function
wants to send data back to the calling program, it will use the return
keyword.
When you use return on a line by itself with no other expression, then
nothing will be returned from the function. The program flow simply
transfers back to the calling location.
return
return 1.0
return results
return results + 1.0
You can actually use more than one return statement inside a
function. As your logic flows through the function, the first time it hits
any return statement, the function will end and any output data is sent
:
back immediately to the calling code. The remaining lines in the
function body are skipped.
Try It Now
10
11
:
12
13
14
15
16
17
Console Output...
When calling a function that returns a data value, your calling code
basically has three choices:
calculate_grade(85)
grade = calculate_grade(85)
print("Your letter grade is: ",calculate_grade(85))
Usually, when a function returns some data, you'll want to use that data
in some fashion. But the exact approach is up to you and the needs of
your program!
:
Work with Me: Getting the Day of the Week
Let's put your new function input and output skills to work. In this
exercise, you are going to write a get_day_of_week() function,
which will accept an input date in the format "YYYY-MM-DD" and
return a string containing the day of the week (like "Monday"). You
are going to use features of the datetime library that you've already
learned, so you may want to review those functions.
Here are some example function calls and the resulting output.
print(get_day_of_week("1776-07-04"))
print(get_day_of_week("1918-11-11"))
print(get_day_of_week("3-16-2001"))
When run, you should see the following output. Notice that the
function will handle invalid input (a date in the wrong format) with a
nice error message instead of throwing an exception and crashing.
Thursday
Monday
Invalid YYYY-MM-DD Date
The starting code below imports the datetime library for you, and it
already contains the main code that calls get_day_of_week()
several times. The results are printed directly to the screen. It's up to
you to write the full get_day_of_week() function by completing the
following steps.
1. Near the top of the code, after the import statement and before
:
the main code, add the "def" statement for
get_day_of_week(). The function should accept one input
parameter named target.
2. Next, start an exception-handling block with "try". It is possible
that the input string is not in the right format, and we want our
function to cleanly handle that situation without crashing. Inside
the try block:
1. Create a variable named thisDate and set it equal to the
results of parsing the target input string using
datetime.datetime.strptime(). The format string should
be "%Y-%m-%d" to handle input in the "YYYY-MM-DD"
format.
2. Create a variable named dayOfWeek and set it equal to
calling strftime() on thisDate. The format string should be
"%A", which will format the day of the week like "Monday".
3. "return" the dayOfWeek value to the calling code.
3. Add an "except" statement to mark the exception-handling
block. Statements in this block will run if the strptime()
function receives unexpected input and throws an exception.
Inside the "except" block:
1. "return" the hard-coded value "Invalid YYYY-MM-DD
Date"
Try It Now
code1.py
2
:
3
10
11
12
13
14
15
16
17
Console
:
Console Output...
When done, test your code and make sure it produces the expected
results. You should also test today's date as input and verify that
you get the right day of the week for today. It's always a good idea to
test your code with known inputs and outputs to make sure
everything is working correctly.
: