Challenge Activity: Functions
A function is a piece of code that you can invoke over and over again. So the idea here is that you will build a program by writing a sequence of functions and saving them to disk file. We will write and test our function but not save it.
A defined function will look like this at the prompt:
>>> def age( ):
There is a space between the parentheses. This function is now called age.
Now each line that follows will be the instructions that make up that function. A few key points to remember:
Indent at the beginning of each line by pressing the Tab key.
Press Enter at the end of each line except after the first def age ( ): line. (Notice that as we saw with the if/else-statements and for-loop statements, the prompt changes from >>> to ....)
Notice that two tabs are needed at the beginning of lines 8 and 11. (These are the lines after the if-statement and the for-loop.)
After you have entered the last command for the function, hit the Enter key twice.
Type the following program. (For year and yourBirth, use the current year and your birth year rather than what is listed.)
def age( ):
name=input("What is your name? ")
year=2015
yourBirth=2000
birthday=input("Have you had a birthday yet this year? Y or N? ")
yourAge=year-yourBirth
if birthday=='N':
yourAge=yourAge-1
print(name+", your age is")
for number in range(yourAge):
print(number)
print(yourAge)
Remember if you receive errors at any point, retype the function definition from the start. It may take you several attempts before you have it correctly entered.
When you are done, type:
age( )
Answer the questions for your name and birthday as prompted. Run the function two times, answering once with Y and once with N to make sure you get the proper output. Notice how you can write a function and then use it over and over.
To make the function age( )even more interesting, you could ask the user to enter his or her birth year using yourBirth=("Enter the year you were born: ") in place of the command that set yourBirth to a set year (2000, in our example). If you want to, try to rewrite the above function with this new statement. Now, every time you run the program, use a different combination of your birth year and whether you have had a birthday or not.
Congratulations! You just coded some of your own simple computer programs! Show your teacher the output when you are done.
Student Resource 13.6
Reading: The Software Development Process
In the earliest days of computing, software development was simply a single programmer writing code in order to solve a problem or computerize a process. However, today’s computer systems are so large and so complex that a team of people is commonly needed to build a successful software product.
A software development team can include product managers, project managers, systems analysts, software designers, programmers, testers, graphic designers, marketers, technical writers, and even software users like you to test the product. A set of agreed-upon principles, methods, and tools are used to coordinate the team’s efforts so that the software project meets its goals.
To help manage the software development process, the software development life cycle (SDLC) was created. The SDLC is an organized way to build a software system or product. Although some development teams break down the steps in the SDLC in slightly different ways, the following phases are typical:
Share with your friends: |