Pages

Saturday, January 28, 2017

Python:Functions

In Python and in almost all programming languages there are these commands called functions. “In the context of programming, a function is a named sequence of statements that performs a computation” (Downey 23). This is very useful in programming since if there is a repetitive sequence of code that we want to execute, instead of repeating the same lines over and over again we can make a function for that sequence of code so that we can call on it when we need to. In Python, there are already many different functions that are built into the program. However, there is also a way where we can add a function into our script. This is called a function definition. When we define a function and its sequence of code that executes whenever the function is being used (Downey 25).   
An example of this is







In the code, there is a keyword called “def”. This means that this is a function definition, the keyword "def" defines the line of code as a function. The text that is next to the “def” is the name of the function. In this example, the name of the function is called “print_lyrics”. Finally, the text under it is what the function contains and what it will do when it is executed. In this example, this function will print out the lines. Finally, the line of code at the end of the function executes the function (print_lyrics()). (Downey 26).
“I’m a lumberjack and I’m okay.”
“I sleep all night and I work all day.”
 Using this new found knowledge we can write a program that can draw a grid.
In this program, I made two functions that printed out a line of the grid. Then making a final function I printed out the grid.









Question: When have you found yourself having to do something repetitive?

Downey, Allen B. Think Python: How to Think like a Computer Scientist. Sebastopol: O'Reilly Media, 2015. Print.

2 comments:

  1. Coding is very repetitive, which is probably why I don't do it. It takes a lot of dedication, patience, and passion to try and code something. Something I always find repetitive is editing video or editing photos, because the steps are usually the same and very repetitive. Something I don't really have patience for.

    ReplyDelete
    Replies
    1. Hi, Jordan
      I agree with you, programming can be really repetitive, but as I found out later on that there can be shortcuts that can help with the repetition. For video editing I don't know if there is anything of that sort. It is really interesting how video and photo editing can be similar to programming. Repetition can cause people to lose interest in things but at the same time it can be also interesting how people respond to it.

      Delete