In this lecture we'll talk about functions. Functions are used extensively in programming languages just as in mass in programming a function takes an input as a list of arguments, does some calculations on the input and then gives back a single result. As we already saw they are many built in functions in Python, such as print or len. But you can also create your own functions. The functions you create are called user defined functions. But you'll see how it can do this later. Why would you create your own functions? Well, first of all, it allows you to reuse code. Once you write the function, you can call it many times in your program instead of rewriting its code every time you need it. Second, it is much easier to understand the program if it is presented as a sequence of steps. Each step can be a function that you know what it's doing, but not necessarily how, especially if you use functions written by other people. If you work in bioinformatics, you also realize that there are many operations that you need to do very frequently on DNA sequences. Examples of such operations are, for instance, computing the GC percentage of a sequence, checking if the DNA sequence has in frame stop codons. When you're first complimenting the DNA sequence. We will learn how to do the functions, how to write them, next in our lecture. The general syntax for defining a function in python follows the following structure. First, you write the keyword def, then the function name. Then a list of arguments followed by a colon. And on the next line by the block of code specific to the function. Just like four, this block of statement is defined by an indentation from the start of the function, definition on the first line. There are a few things that you should know about the way to write a block of code following the function definition. First, the very first statement in this code can be a string that documents the function. Then, we can write the code. We need to compute the output of the function, and the last statement in the function needs to be a return statement. We start with the key word return followed by the value we want output. Let's see, now, a real example of how to write a function to compute at GC percentage each of a DNA sequence. We saw how it could do this in the beginning of our python course, but you never put it in a function form. For this function, we want of course the input will be a DNA sequence, and the output is GC percentage. Let's call this function GC, and it's argument, DNA. We'll assume DNA to be a string variable. On the first line of the function, let's write a string that explains what GC does. In this case this function computes the GC percentage of a DNA sequence. If the DNA sequence has any undefined places. Meaning there are end characters in our input DNA string. We don't know what to do about them, so it's better just to ignore them. Lets count them using the method. Count of the string object in a variable called nbases. The GC percentage which will be of course, the number of C's plus the number of G's times 100 divided by the length of the DNA variable minus the number of undefined bases. Notice that because we don't know the case of the letters in the DNA stream, we counted both upper and lower case for all letters involved in our computations. We can store our result in a variable called GC percent and return it's value. We can call now the GC function from the main prompt by writing GC with a string argument that represents our DNA sequence. By then, we're limited to pre-end the output zero point four representing the GC percentage of the input string. Suppose someone else wrote the function GC and we want to check what it does. You need to type half of GC to do this. But I don't use documentation, the documentation string we brought in at the beginning of the function to print the whole message for the function GC, like it is shown here. Next, let me explain to you an important concept in programming that you need to remember every time you write a program. The scope of variable declaration. The scope of variable is the context within which the program remembers it or, in other words, the context where the program knows what the name of the variable refers to. A variable can either have a global or a local scope. A global variable is a variable declared in the main body of the source code outside all functions, while a local variable is undeclared within the body of a function or a block. If you define a variable inside the function, it will only be visible inside that function, therefore it will be local to that function. For instance, in our DC function, we declared the variable nbases to remember the number of undefined bases in DNA. This is a local variable to our function GC, and the computers know what this value is only inside the DC function. If you try to access it from the main prompt in the Python interpreter, like we do here, by typing print nbases. The computer will give you an error message. Next I will let Steven, our programming expert, tell you more about functions.