-
Introduction 4
-
Lecture1.1
-
Lecture1.2
-
Lecture1.3
-
Lecture1.4
-
-
Production Possibilities Frontier 4
-
Lecture2.1
-
Lecture2.2
-
Lecture2.3
-
Lecture2.4
-
-
Trade 3
-
Lecture3.1
-
Lecture3.2
-
Lecture3.3
-
-
Demand 4
-
Lecture4.1
-
Lecture4.2
-
Lecture4.3
-
Lecture4.4
-
-
Supply 2
-
Lecture5.1
-
Lecture5.2
-
-
Equilibrium 4
-
Lecture6.1
-
Lecture6.2
-
Lecture6.3
-
Lecture6.4
-
-
Curve Movements 4
-
Lecture7.1
-
Lecture7.2
-
Lecture7.3
-
Lecture7.4
-
-
Elasticity and Revenue 5
-
Lecture8.1
-
Lecture8.2
-
Lecture8.3
-
Lecture8.4
-
Lecture8.5
-
-
Taxes 7
-
Lecture9.1
-
Lecture9.2
-
Lecture9.3
-
Lecture9.4
-
Lecture9.5
-
Lecture9.6
-
Lecture9.7
-
-
Consumer and Producer Surplus 8
-
Lecture10.1
-
Lecture10.2
-
Lecture10.3
-
Lecture10.4
-
Lecture10.5
-
Lecture10.6
-
Lecture10.7
-
Lecture10.8
-
-
Imports and Exports 4
-
Lecture11.1
-
Lecture11.2
-
Lecture11.3
-
Lecture11.4
-
-
Tariffs 2
-
Lecture12.1
-
Lecture12.2
-
Introducing Functions
One of the most important aspects of programming is functions. We use functions to write code that we want to be able to run multiple times. You’ve already been using them though; print() is an example of a function. The format of creating a function is as so:
def double(x):
print(x*2)
Def is how we start out a function. After that we give it a name, for our function we called it double. Directly after the function name there are parentheses in which we specify what arguments we are getting. We can have 0, or we can have multiple. The idea is that we are asking the user for certain data, in this case we are asking for an x value. These arguments are then used within the function, but only within the function. Also, like loops, anything that is indented is part of the function and once the indenting stops then the function’s code is over.
To use this function to get double 4, we would do the following:
double(4)
A common thing to do with functions is to return values. With returned values you can assign them to variables or use them in other functions, such as print. Let’s modify our function to return the value:
def double(x):
return x*2
To assign to a variable we would do this:
a = double(4)
print(a)
Or to just print it out we would do this:
print(double(4))
Challenge