Defining new Python functionsThe function print() also has arguments with default values. For example, the default separator between two things to print is a single space. But you can change that. ("" below is the empty string, the string with zero characters.)
You have seen builtin functions, like type(), int(), str(), and functions from packages, like math.sqrt(). In Python, you can also define functions yourself. This is useful when you need to do the same thing (or something very similar) again and again. Suppose that you are in a country where temperature is given in Celsius, but you are more familiar with Fahrenheit. Here is how you can figure out that 20 degrees Celsius are 68 degrees Fahrenheit, and 30 degrees Celsius are 86 degrees Fahrenheit:
If you need to convert temperatures very often, this gets tedious. Instead, you can define your own function, similar to the way it is done in mathematics.
Note also that function definition uses a variable, temp. Python uses this variable in much the same way as in function definitions in mathematics:
f(x) = x+1
There, you don't have to specify beforehand what x is. Rather, for each x, the function value is f(x) = x+1. In the same way, when you call your function saying celsius_2_fahrenheit(20), then at this time, Python stores the value 20 in the variable temp and executes the code of the function with temp set to 20.
Now we can use the function:
When you define your function, you have to indent the second line using either "tab" or spaces. Why is that? It is because the indentation is what Python uses to figure out the length of the function definition. You could in principle also define functions that take up more than one line (and in future, you often will). Here is a somewhat contrived example:
How does Python figure out how long the function definition is? By using the indentation. All the consecutive code that is indented the same number of spaces forms a block. It belongs together. In this case, it all forms a function definition together.
If you use different numbers of indentations, or if you don't use any indentation after the first line, Python complains
Many of the builtin functions that you have encountered return a value that you can store in a variable, or use further:
That is, you can use len("dynamic") in expressions like you would the number 7. The function len() returns an integer, which we can see by inspecting wordlen:
Our Celsius to Fahrenheit function, on the other hand, does not return any value:
This means that temp contains the special value None. If we want our function to return a value, we have to say so explicitly.
Note the new reserved Python word "return" in our function definition. It tells Python to return the given value. Now we can use the value returned by our temperature conversion function like we would any other floating point value:
The command "return" means that a value is returned, and the function is terminated. That is, lines after "return" are completely ignored:
As you can see, the string "this line is never executed" is not printed, so this line is, in fact, never executed.
Try it for yourself:
Functions with multiple arguments
So far we have only defined functions with one argument. Functions with multiple arguments are straightforward:
If you define a function and give a value for an argument in the definition, you can leave that argument off when calling the function. Or you can specify the argument to give it a different value from the default.
But you have to put all the arguments without default values before the ones with defaults.
Executing a whole program at once
Another way of running your Python program first.py is to use idle:
Whenever Python encounters the character #, it ignores the rest of the line. (Except when you use # inside a string, that is.) So this character is used to insert comments into code.
Where should you put comments?
You can also use comments to temporarily deactivate code that you don't want to use at the moment.