VPython Functions: Difference between revisions
Line 104: | Line 104: | ||
# exVector2 = vector(4, -2 ,8) | # exVector2 = vector(4, -2 ,8) | ||
crossExVector = cross(exVector, exVector2) | crossExVector = cross(exVector, exVector2) | ||
print crossExVector | print crossExVector # will print <26, 100, 12> | ||
== See also == | == See also == |
Revision as of 00:07, 25 November 2016
Written by Kevin Randrup
Claimed Chloe Choi
Introduction to functions in Python and applying them to write shorter and more understandable code. Assumes that VPython is installed and you understand the basics of programming in Python.
Summary
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also "arguments" or "parameters") and can give back 0 or 1 output values. Furthermore, they make code much easier to understand by encapsulating a task in a single line of code instead of being repeated over and over again.
Whenever you type print(5)
, you are calling a function called print
with an argument of 5
. This page will describe the basics of functions and how to
Basic of Functions
Basic Python function syntax
Functions can be defined with the def keyword followed by the function name and a colon.
The return
keyword can be used for a function to "give back" a result.
def functionName(argumentOne, argumentTwo): functionReturnValue = argumentOne + argumentTwo return functionReturnValue
Example function
# This function adds three numbers together and gives back the result using the return keyword def addNumbers(a, b, c): return a + b + c
Example use of a function
# Calls the function addNumbers with the arguments 1, 2 and 4 result = addNumbers(1, 2, 4)
Writing your own Functions
Writing a function for a task instead of copy and pasting code makes your program much easier to understand and debug. The best way to illustrate this is with an example.
For this example, we will create a function to calculate the electric field created by a point charge.
As a reminder, this is the formula we are modeling:
[math]\displaystyle{ \overrightarrow{E}=\frac{1}{4πε_0} \frac{q}{|\overrightarrow{r}|^2} \hat r }[/math]
First, we ask what do we need to calculate the electric field?
- [math]\displaystyle{ q }[/math] - the charge of the source
- [math]\displaystyle{ \overrightarrow{r} }[/math] - the distance from the source to the electric field
[math]\displaystyle{ q }[/math] and [math]\displaystyle{ \overrightarrow{r} }[/math] will be the two arguments to our function which will look like this:
# Calculates the electric field at a given vector away from the charge def electricField(q, r): electricConstant = 9e9 # 1/(4 * π * e0) return electricConstant * q * r.norm() / r.mag2
Using the function
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.
# Calculate the eletric field in two different places charge = 1.6e-19 origin = vector(0,0,0) point1 = vector(-10, 5, 15) point2 = vector(20, -5, 12) field1 = 9e9 * charge * point1.norm() / point1.mag2 field2 = 9e9 * charge * point2.norm() / point2.mag2
We can use a function in the last 2 lines to reduce the duplicated code to the following
field1 = electricField(charge, point1) field2 = electricField(charge, point2)
Frequently Used Functions
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors. To better illustrate these functions, let's say we have a vector called exVector.
exVector = vector(-10, 2 ,5)
mag()
# Calculates the magnitude of a vector magExVector = mag(exVector) print magExVector # will print 11.357
mag2(A)
# Calculates the magnitude squared of a vector mag2ExVector = mag2(exVector) print magExVector # will print 129
norm(A)
# Calculates the unit vector of a vector unitExVector = norm(exVector) print unitExVector # will print <-0.88, 0.17, 0.44>
dot(A, B)
# Calculates the scalar dot product between the two vectors # exVector2 = vector(4, -2 ,8) dotExVector = dot(exVector, exVector2) print dotExVector # will print 4
cross(A, B)
# Calculates the vector cross product between two vectors # exVector2 = vector(4, -2 ,8) crossExVector = cross(exVector, exVector2) print crossExVector # will print <26, 100, 12>
See also
Prerequisites
Further reading
VPython Common Errors and Troubleshooting
Python standard function library.