<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://www.physicsbook.gatech.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Dkim469</id>
	<title>Physics Book - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="http://www.physicsbook.gatech.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Dkim469"/>
	<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/Special:Contributions/Dkim469"/>
	<updated>2026-04-11T16:17:31Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.42.7</generator>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26423</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26423"/>
		<updated>2016-11-28T03:29:01Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Further reading */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
This is a simple function that creates a madlib with the 4 variables that are given as inputs.&lt;br /&gt;
&lt;br /&gt;
For example, if Sally was inputed as the name, lunch as noun1, pterodactyl as noun2, and flew as the verb, the the function will return the phrase &#039;&#039;Sally was looking at his lunch when a pterodactyl flew nearby&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; madlib(&amp;quot;Sally&amp;quot;, &amp;quot;lunch&amp;quot;, &amp;quot;pterodactyl&amp;quot;, &amp;quot;flew&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Sally was looking at his lunch when a pterodactyl flew nearby.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Returning the output given by a function===&lt;br /&gt;
&lt;br /&gt;
When a function takes an input and gives an output such as &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; in the madlib example, you cannot refer back to the output phrase &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;&lt;br /&gt;
If you would like to use this phrase again, then a function called &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is needed.&lt;br /&gt;
When &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is added at the end of a function, the output is passed back to the caller, which means that you can refer to the output now.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
   return result&lt;br /&gt;
&lt;br /&gt;
With this new code, if you need to refer to the sentence &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;, you can do so by by typing in &amp;lt;code&amp;gt;result&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Conditional Statement===&lt;br /&gt;
Lets say you have the following code:&lt;br /&gt;
&lt;br /&gt;
 # This function prints every letter in the input&lt;br /&gt;
 def printLetters(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
   print letter&lt;br /&gt;
&lt;br /&gt;
What if your &#039;&#039;evil&#039;&#039; TA asks you to only print the vowel letters given in the input?&lt;br /&gt;
This means that you need to add a condition to your function where the &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; function only works if the letter is a vowel.&lt;br /&gt;
This can be done by using &#039;&#039;&#039;conditional statement&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Condtional statement starts with &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 def justvowels(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
    if letter in &amp;quot;aeiou&amp;quot;:&lt;br /&gt;
      print letter&lt;br /&gt;
&lt;br /&gt;
Instead of printing every letter in the string, this code will only print &#039;&#039;&#039;if&#039;&#039;&#039; the letter in question can be found in the list &amp;quot;a,e,i,o,u&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
Writing a function for a task instead of copy and pasting code makes your program much easier to understand and debug. &lt;br /&gt;
&lt;br /&gt;
For example, to write a function that calculates the electric field created by a point charge, we first find the formula.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we ask ourselves what we need in order to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
 &lt;br /&gt;
We start by creating a sphere for a point charge&lt;br /&gt;
 p = sphere(pos=(-5e-11,0,0), radius=1e-11, color=color.red)&lt;br /&gt;
 &lt;br /&gt;
We also need to define the observation point where we calculate the electric field created by the point charge&lt;br /&gt;
 obslocation = vector(1, 1, 1)&lt;br /&gt;
&lt;br /&gt;
r, the distance between the obslocation and p&lt;br /&gt;
 r = obslocation - p.pos&lt;br /&gt;
&lt;br /&gt;
 # We can now write a function that calculates the electric field at a point away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
The final code will look like this:&lt;br /&gt;
&lt;br /&gt;
 from __future__ import division&lt;br /&gt;
 from visual import * &lt;br /&gt;
 p = sphere(pos=(-5e-11,0,0), radius=1e-11, color=color.red)&lt;br /&gt;
 obslocation = vector(1, 1, 1)&lt;br /&gt;
 r = obslocation - p.pos&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example of using these common functions==&lt;br /&gt;
&lt;br /&gt;
Remember this code from the &#039;&#039;&#039;Writing your own Functions&#039;&#039;&#039; section?&lt;br /&gt;
 &lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
We can now simplify this code using some of the common functions listed in &#039;&#039;&#039;Frequently Used Functions&#039;&#039;&#039; section.&lt;br /&gt;
&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     E = electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
It is also possible to skip defining the &amp;lt;code&amp;gt;electricConstant * q * r.norm() / r.mag2&amp;lt;/code&amp;gt; function and just return it.&lt;br /&gt;
&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
==Connectedness==&lt;br /&gt;
Functions are used everywhere in coding.&lt;br /&gt;
For example, in computational media, coding is used to modify pictures, sounds, and movies.&lt;br /&gt;
Of course, we now mostly rely on editing programs, but underneath that program, when you press a button to trace the edges of a picture, the program finds the function that it runs and returns the output.&lt;br /&gt;
Such a code made look like this:&lt;br /&gt;
&lt;br /&gt;
 def luminance(pixel):&lt;br /&gt;
   r = getRed(pixel)&lt;br /&gt;
   g = getGreen(pixel)&lt;br /&gt;
   b = getBlue(pixel)&lt;br /&gt;
   return (r+g+b)/3&lt;br /&gt;
&lt;br /&gt;
 def edgeDetect(picture):&lt;br /&gt;
   for p in getPixels(picture):&lt;br /&gt;
     x = getX(p)	&lt;br /&gt;
     y = getY(p)&lt;br /&gt;
     if y &amp;lt; getHeight(picture) - 1 and x &amp;lt; getWidth(picture) - 1:&lt;br /&gt;
       botrt = getPixel(picture, x+1, y+1)&lt;br /&gt;
       thislum = luminance(p)&lt;br /&gt;
       brlum = luminance(botrt)&lt;br /&gt;
       if abs(brlum - thislum) &amp;gt; 8:&lt;br /&gt;
         setColor(p, blue)&lt;br /&gt;
       if abs(brlum - thislum) &amp;lt;= 8:&lt;br /&gt;
         setColor(p, white)&lt;br /&gt;
&lt;br /&gt;
When this program is run with the picture on the left, the output is the picture on the right.&lt;br /&gt;
&lt;br /&gt;
[[File:HW 2 Picture Original.png]] [[File:eiffel2.png]]&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
[https://www.tutorialspoint.com/python/python_functions.htm Python Functions Tutorial]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26420</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26420"/>
		<updated>2016-11-28T03:28:14Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Further reading */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
This is a simple function that creates a madlib with the 4 variables that are given as inputs.&lt;br /&gt;
&lt;br /&gt;
For example, if Sally was inputed as the name, lunch as noun1, pterodactyl as noun2, and flew as the verb, the the function will return the phrase &#039;&#039;Sally was looking at his lunch when a pterodactyl flew nearby&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; madlib(&amp;quot;Sally&amp;quot;, &amp;quot;lunch&amp;quot;, &amp;quot;pterodactyl&amp;quot;, &amp;quot;flew&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Sally was looking at his lunch when a pterodactyl flew nearby.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Returning the output given by a function===&lt;br /&gt;
&lt;br /&gt;
When a function takes an input and gives an output such as &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; in the madlib example, you cannot refer back to the output phrase &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;&lt;br /&gt;
If you would like to use this phrase again, then a function called &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is needed.&lt;br /&gt;
When &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is added at the end of a function, the output is passed back to the caller, which means that you can refer to the output now.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
   return result&lt;br /&gt;
&lt;br /&gt;
With this new code, if you need to refer to the sentence &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;, you can do so by by typing in &amp;lt;code&amp;gt;result&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Conditional Statement===&lt;br /&gt;
Lets say you have the following code:&lt;br /&gt;
&lt;br /&gt;
 # This function prints every letter in the input&lt;br /&gt;
 def printLetters(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
   print letter&lt;br /&gt;
&lt;br /&gt;
What if your &#039;&#039;evil&#039;&#039; TA asks you to only print the vowel letters given in the input?&lt;br /&gt;
This means that you need to add a condition to your function where the &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; function only works if the letter is a vowel.&lt;br /&gt;
This can be done by using &#039;&#039;&#039;conditional statement&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Condtional statement starts with &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 def justvowels(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
    if letter in &amp;quot;aeiou&amp;quot;:&lt;br /&gt;
      print letter&lt;br /&gt;
&lt;br /&gt;
Instead of printing every letter in the string, this code will only print &#039;&#039;&#039;if&#039;&#039;&#039; the letter in question can be found in the list &amp;quot;a,e,i,o,u&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
Writing a function for a task instead of copy and pasting code makes your program much easier to understand and debug. &lt;br /&gt;
&lt;br /&gt;
For example, to write a function that calculates the electric field created by a point charge, we first find the formula.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we ask ourselves what we need in order to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
 &lt;br /&gt;
We start by creating a sphere for a point charge&lt;br /&gt;
 p = sphere(pos=(-5e-11,0,0), radius=1e-11, color=color.red)&lt;br /&gt;
 &lt;br /&gt;
We also need to define the observation point where we calculate the electric field created by the point charge&lt;br /&gt;
 obslocation = vector(1, 1, 1)&lt;br /&gt;
&lt;br /&gt;
r, the distance between the obslocation and p&lt;br /&gt;
 r = obslocation - p.pos&lt;br /&gt;
&lt;br /&gt;
 # We can now write a function that calculates the electric field at a point away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
The final code will look like this:&lt;br /&gt;
&lt;br /&gt;
 from __future__ import division&lt;br /&gt;
 from visual import * &lt;br /&gt;
 p = sphere(pos=(-5e-11,0,0), radius=1e-11, color=color.red)&lt;br /&gt;
 obslocation = vector(1, 1, 1)&lt;br /&gt;
 r = obslocation - p.pos&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example of using these common functions==&lt;br /&gt;
&lt;br /&gt;
Remember this code from the &#039;&#039;&#039;Writing your own Functions&#039;&#039;&#039; section?&lt;br /&gt;
 &lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
We can now simplify this code using some of the common functions listed in &#039;&#039;&#039;Frequently Used Functions&#039;&#039;&#039; section.&lt;br /&gt;
&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     E = electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
It is also possible to skip defining the &amp;lt;code&amp;gt;electricConstant * q * r.norm() / r.mag2&amp;lt;/code&amp;gt; function and just return it.&lt;br /&gt;
&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
==Connectedness==&lt;br /&gt;
Functions are used everywhere in coding.&lt;br /&gt;
For example, in computational media, coding is used to modify pictures, sounds, and movies.&lt;br /&gt;
Of course, we now mostly rely on editing programs, but underneath that program, when you press a button to trace the edges of a picture, the program finds the function that it runs and returns the output.&lt;br /&gt;
Such a code made look like this:&lt;br /&gt;
&lt;br /&gt;
 def luminance(pixel):&lt;br /&gt;
   r = getRed(pixel)&lt;br /&gt;
   g = getGreen(pixel)&lt;br /&gt;
   b = getBlue(pixel)&lt;br /&gt;
   return (r+g+b)/3&lt;br /&gt;
&lt;br /&gt;
 def edgeDetect(picture):&lt;br /&gt;
   for p in getPixels(picture):&lt;br /&gt;
     x = getX(p)	&lt;br /&gt;
     y = getY(p)&lt;br /&gt;
     if y &amp;lt; getHeight(picture) - 1 and x &amp;lt; getWidth(picture) - 1:&lt;br /&gt;
       botrt = getPixel(picture, x+1, y+1)&lt;br /&gt;
       thislum = luminance(p)&lt;br /&gt;
       brlum = luminance(botrt)&lt;br /&gt;
       if abs(brlum - thislum) &amp;gt; 8:&lt;br /&gt;
         setColor(p, blue)&lt;br /&gt;
       if abs(brlum - thislum) &amp;lt;= 8:&lt;br /&gt;
         setColor(p, white)&lt;br /&gt;
&lt;br /&gt;
When this program is run with the picture on the left, the output is the picture on the right.&lt;br /&gt;
&lt;br /&gt;
[[File:HW 2 Picture Original.png]] [[File:eiffel2.png]]&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
[https://www.tutorialspoint.com/python/python_functions.htm]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26411</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26411"/>
		<updated>2016-11-28T03:26:56Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Connectedness */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
This is a simple function that creates a madlib with the 4 variables that are given as inputs.&lt;br /&gt;
&lt;br /&gt;
For example, if Sally was inputed as the name, lunch as noun1, pterodactyl as noun2, and flew as the verb, the the function will return the phrase &#039;&#039;Sally was looking at his lunch when a pterodactyl flew nearby&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; madlib(&amp;quot;Sally&amp;quot;, &amp;quot;lunch&amp;quot;, &amp;quot;pterodactyl&amp;quot;, &amp;quot;flew&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Sally was looking at his lunch when a pterodactyl flew nearby.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Returning the output given by a function===&lt;br /&gt;
&lt;br /&gt;
When a function takes an input and gives an output such as &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; in the madlib example, you cannot refer back to the output phrase &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;&lt;br /&gt;
If you would like to use this phrase again, then a function called &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is needed.&lt;br /&gt;
When &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is added at the end of a function, the output is passed back to the caller, which means that you can refer to the output now.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
   return result&lt;br /&gt;
&lt;br /&gt;
With this new code, if you need to refer to the sentence &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;, you can do so by by typing in &amp;lt;code&amp;gt;result&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Conditional Statement===&lt;br /&gt;
Lets say you have the following code:&lt;br /&gt;
&lt;br /&gt;
 # This function prints every letter in the input&lt;br /&gt;
 def printLetters(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
   print letter&lt;br /&gt;
&lt;br /&gt;
What if your &#039;&#039;evil&#039;&#039; TA asks you to only print the vowel letters given in the input?&lt;br /&gt;
This means that you need to add a condition to your function where the &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; function only works if the letter is a vowel.&lt;br /&gt;
This can be done by using &#039;&#039;&#039;conditional statement&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Condtional statement starts with &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 def justvowels(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
    if letter in &amp;quot;aeiou&amp;quot;:&lt;br /&gt;
      print letter&lt;br /&gt;
&lt;br /&gt;
Instead of printing every letter in the string, this code will only print &#039;&#039;&#039;if&#039;&#039;&#039; the letter in question can be found in the list &amp;quot;a,e,i,o,u&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
Writing a function for a task instead of copy and pasting code makes your program much easier to understand and debug. &lt;br /&gt;
&lt;br /&gt;
For example, to write a function that calculates the electric field created by a point charge, we first find the formula.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we ask ourselves what we need in order to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
 &lt;br /&gt;
We start by creating a sphere for a point charge&lt;br /&gt;
 p = sphere(pos=(-5e-11,0,0), radius=1e-11, color=color.red)&lt;br /&gt;
 &lt;br /&gt;
We also need to define the observation point where we calculate the electric field created by the point charge&lt;br /&gt;
 obslocation = vector(1, 1, 1)&lt;br /&gt;
&lt;br /&gt;
r, the distance between the obslocation and p&lt;br /&gt;
 r = obslocation - p.pos&lt;br /&gt;
&lt;br /&gt;
 # We can now write a function that calculates the electric field at a point away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
The final code will look like this:&lt;br /&gt;
&lt;br /&gt;
 from __future__ import division&lt;br /&gt;
 from visual import * &lt;br /&gt;
 p = sphere(pos=(-5e-11,0,0), radius=1e-11, color=color.red)&lt;br /&gt;
 obslocation = vector(1, 1, 1)&lt;br /&gt;
 r = obslocation - p.pos&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example of using these common functions==&lt;br /&gt;
&lt;br /&gt;
Remember this code from the &#039;&#039;&#039;Writing your own Functions&#039;&#039;&#039; section?&lt;br /&gt;
 &lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
We can now simplify this code using some of the common functions listed in &#039;&#039;&#039;Frequently Used Functions&#039;&#039;&#039; section.&lt;br /&gt;
&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     E = electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
It is also possible to skip defining the &amp;lt;code&amp;gt;electricConstant * q * r.norm() / r.mag2&amp;lt;/code&amp;gt; function and just return it.&lt;br /&gt;
&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
==Connectedness==&lt;br /&gt;
Functions are used everywhere in coding.&lt;br /&gt;
For example, in computational media, coding is used to modify pictures, sounds, and movies.&lt;br /&gt;
Of course, we now mostly rely on editing programs, but underneath that program, when you press a button to trace the edges of a picture, the program finds the function that it runs and returns the output.&lt;br /&gt;
Such a code made look like this:&lt;br /&gt;
&lt;br /&gt;
 def luminance(pixel):&lt;br /&gt;
   r = getRed(pixel)&lt;br /&gt;
   g = getGreen(pixel)&lt;br /&gt;
   b = getBlue(pixel)&lt;br /&gt;
   return (r+g+b)/3&lt;br /&gt;
&lt;br /&gt;
 def edgeDetect(picture):&lt;br /&gt;
   for p in getPixels(picture):&lt;br /&gt;
     x = getX(p)	&lt;br /&gt;
     y = getY(p)&lt;br /&gt;
     if y &amp;lt; getHeight(picture) - 1 and x &amp;lt; getWidth(picture) - 1:&lt;br /&gt;
       botrt = getPixel(picture, x+1, y+1)&lt;br /&gt;
       thislum = luminance(p)&lt;br /&gt;
       brlum = luminance(botrt)&lt;br /&gt;
       if abs(brlum - thislum) &amp;gt; 8:&lt;br /&gt;
         setColor(p, blue)&lt;br /&gt;
       if abs(brlum - thislum) &amp;lt;= 8:&lt;br /&gt;
         setColor(p, white)&lt;br /&gt;
&lt;br /&gt;
When this program is run with the picture on the left, the output is the picture on the right.&lt;br /&gt;
&lt;br /&gt;
[[File:HW 2 Picture Original.png]] [[File:eiffel2.png]]&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26408</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26408"/>
		<updated>2016-11-28T03:26:47Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Connectedness */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
This is a simple function that creates a madlib with the 4 variables that are given as inputs.&lt;br /&gt;
&lt;br /&gt;
For example, if Sally was inputed as the name, lunch as noun1, pterodactyl as noun2, and flew as the verb, the the function will return the phrase &#039;&#039;Sally was looking at his lunch when a pterodactyl flew nearby&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; madlib(&amp;quot;Sally&amp;quot;, &amp;quot;lunch&amp;quot;, &amp;quot;pterodactyl&amp;quot;, &amp;quot;flew&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Sally was looking at his lunch when a pterodactyl flew nearby.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Returning the output given by a function===&lt;br /&gt;
&lt;br /&gt;
When a function takes an input and gives an output such as &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; in the madlib example, you cannot refer back to the output phrase &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;&lt;br /&gt;
If you would like to use this phrase again, then a function called &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is needed.&lt;br /&gt;
When &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is added at the end of a function, the output is passed back to the caller, which means that you can refer to the output now.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
   return result&lt;br /&gt;
&lt;br /&gt;
With this new code, if you need to refer to the sentence &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;, you can do so by by typing in &amp;lt;code&amp;gt;result&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Conditional Statement===&lt;br /&gt;
Lets say you have the following code:&lt;br /&gt;
&lt;br /&gt;
 # This function prints every letter in the input&lt;br /&gt;
 def printLetters(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
   print letter&lt;br /&gt;
&lt;br /&gt;
What if your &#039;&#039;evil&#039;&#039; TA asks you to only print the vowel letters given in the input?&lt;br /&gt;
This means that you need to add a condition to your function where the &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; function only works if the letter is a vowel.&lt;br /&gt;
This can be done by using &#039;&#039;&#039;conditional statement&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Condtional statement starts with &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 def justvowels(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
    if letter in &amp;quot;aeiou&amp;quot;:&lt;br /&gt;
      print letter&lt;br /&gt;
&lt;br /&gt;
Instead of printing every letter in the string, this code will only print &#039;&#039;&#039;if&#039;&#039;&#039; the letter in question can be found in the list &amp;quot;a,e,i,o,u&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
Writing a function for a task instead of copy and pasting code makes your program much easier to understand and debug. &lt;br /&gt;
&lt;br /&gt;
For example, to write a function that calculates the electric field created by a point charge, we first find the formula.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we ask ourselves what we need in order to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
 &lt;br /&gt;
We start by creating a sphere for a point charge&lt;br /&gt;
 p = sphere(pos=(-5e-11,0,0), radius=1e-11, color=color.red)&lt;br /&gt;
 &lt;br /&gt;
We also need to define the observation point where we calculate the electric field created by the point charge&lt;br /&gt;
 obslocation = vector(1, 1, 1)&lt;br /&gt;
&lt;br /&gt;
r, the distance between the obslocation and p&lt;br /&gt;
 r = obslocation - p.pos&lt;br /&gt;
&lt;br /&gt;
 # We can now write a function that calculates the electric field at a point away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
The final code will look like this:&lt;br /&gt;
&lt;br /&gt;
 from __future__ import division&lt;br /&gt;
 from visual import * &lt;br /&gt;
 p = sphere(pos=(-5e-11,0,0), radius=1e-11, color=color.red)&lt;br /&gt;
 obslocation = vector(1, 1, 1)&lt;br /&gt;
 r = obslocation - p.pos&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example of using these common functions==&lt;br /&gt;
&lt;br /&gt;
Remember this code from the &#039;&#039;&#039;Writing your own Functions&#039;&#039;&#039; section?&lt;br /&gt;
 &lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
We can now simplify this code using some of the common functions listed in &#039;&#039;&#039;Frequently Used Functions&#039;&#039;&#039; section.&lt;br /&gt;
&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     E = electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
It is also possible to skip defining the &amp;lt;code&amp;gt;electricConstant * q * r.norm() / r.mag2&amp;lt;/code&amp;gt; function and just return it.&lt;br /&gt;
&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
==Connectedness==&lt;br /&gt;
Functions are used everywhere in coding.&lt;br /&gt;
For example, in computational media, coding is used to modify pictures, sounds, and movies.&lt;br /&gt;
Of course, we now mostly rely on editing programs, but underneath that program, when you press a button to trace the edges of a picture, the program finds the function that it runs and returns the output.&lt;br /&gt;
Such a code made look like this:&lt;br /&gt;
&lt;br /&gt;
 def luminance(pixel):&lt;br /&gt;
   r = getRed(pixel)&lt;br /&gt;
   g = getGreen(pixel)&lt;br /&gt;
   b = getBlue(pixel)&lt;br /&gt;
   return (r+g+b)/3&lt;br /&gt;
&lt;br /&gt;
 def edgeDetect(picture):&lt;br /&gt;
   for p in getPixels(picture):&lt;br /&gt;
     x = getX(p)	&lt;br /&gt;
     y = getY(p)&lt;br /&gt;
     if y &amp;lt; getHeight(picture) - 1 and x &amp;lt; getWidth(picture) - 1:&lt;br /&gt;
       botrt = getPixel(picture, x+1, y+1)&lt;br /&gt;
       thislum = luminance(p)&lt;br /&gt;
       brlum = luminance(botrt)&lt;br /&gt;
       if abs(brlum - thislum) &amp;gt; 8:&lt;br /&gt;
         setColor(p, blue)&lt;br /&gt;
       if abs(brlum - thislum) &amp;lt;= 8:&lt;br /&gt;
         setColor(p, white)&lt;br /&gt;
&lt;br /&gt;
When this program is run with the picture on the left, the output is the picture on the right.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:HW 2 Picture Original.png]] [[File:eiffel2.png]]&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26405</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26405"/>
		<updated>2016-11-28T03:26:38Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Connectedness */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
This is a simple function that creates a madlib with the 4 variables that are given as inputs.&lt;br /&gt;
&lt;br /&gt;
For example, if Sally was inputed as the name, lunch as noun1, pterodactyl as noun2, and flew as the verb, the the function will return the phrase &#039;&#039;Sally was looking at his lunch when a pterodactyl flew nearby&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; madlib(&amp;quot;Sally&amp;quot;, &amp;quot;lunch&amp;quot;, &amp;quot;pterodactyl&amp;quot;, &amp;quot;flew&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Sally was looking at his lunch when a pterodactyl flew nearby.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Returning the output given by a function===&lt;br /&gt;
&lt;br /&gt;
When a function takes an input and gives an output such as &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; in the madlib example, you cannot refer back to the output phrase &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;&lt;br /&gt;
If you would like to use this phrase again, then a function called &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is needed.&lt;br /&gt;
When &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is added at the end of a function, the output is passed back to the caller, which means that you can refer to the output now.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
   return result&lt;br /&gt;
&lt;br /&gt;
With this new code, if you need to refer to the sentence &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;, you can do so by by typing in &amp;lt;code&amp;gt;result&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Conditional Statement===&lt;br /&gt;
Lets say you have the following code:&lt;br /&gt;
&lt;br /&gt;
 # This function prints every letter in the input&lt;br /&gt;
 def printLetters(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
   print letter&lt;br /&gt;
&lt;br /&gt;
What if your &#039;&#039;evil&#039;&#039; TA asks you to only print the vowel letters given in the input?&lt;br /&gt;
This means that you need to add a condition to your function where the &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; function only works if the letter is a vowel.&lt;br /&gt;
This can be done by using &#039;&#039;&#039;conditional statement&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Condtional statement starts with &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 def justvowels(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
    if letter in &amp;quot;aeiou&amp;quot;:&lt;br /&gt;
      print letter&lt;br /&gt;
&lt;br /&gt;
Instead of printing every letter in the string, this code will only print &#039;&#039;&#039;if&#039;&#039;&#039; the letter in question can be found in the list &amp;quot;a,e,i,o,u&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
Writing a function for a task instead of copy and pasting code makes your program much easier to understand and debug. &lt;br /&gt;
&lt;br /&gt;
For example, to write a function that calculates the electric field created by a point charge, we first find the formula.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we ask ourselves what we need in order to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
 &lt;br /&gt;
We start by creating a sphere for a point charge&lt;br /&gt;
 p = sphere(pos=(-5e-11,0,0), radius=1e-11, color=color.red)&lt;br /&gt;
 &lt;br /&gt;
We also need to define the observation point where we calculate the electric field created by the point charge&lt;br /&gt;
 obslocation = vector(1, 1, 1)&lt;br /&gt;
&lt;br /&gt;
r, the distance between the obslocation and p&lt;br /&gt;
 r = obslocation - p.pos&lt;br /&gt;
&lt;br /&gt;
 # We can now write a function that calculates the electric field at a point away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
The final code will look like this:&lt;br /&gt;
&lt;br /&gt;
 from __future__ import division&lt;br /&gt;
 from visual import * &lt;br /&gt;
 p = sphere(pos=(-5e-11,0,0), radius=1e-11, color=color.red)&lt;br /&gt;
 obslocation = vector(1, 1, 1)&lt;br /&gt;
 r = obslocation - p.pos&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example of using these common functions==&lt;br /&gt;
&lt;br /&gt;
Remember this code from the &#039;&#039;&#039;Writing your own Functions&#039;&#039;&#039; section?&lt;br /&gt;
 &lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
We can now simplify this code using some of the common functions listed in &#039;&#039;&#039;Frequently Used Functions&#039;&#039;&#039; section.&lt;br /&gt;
&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     E = electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
It is also possible to skip defining the &amp;lt;code&amp;gt;electricConstant * q * r.norm() / r.mag2&amp;lt;/code&amp;gt; function and just return it.&lt;br /&gt;
&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
==Connectedness==&lt;br /&gt;
Functions are used everywhere in coding.&lt;br /&gt;
For example, in computational media, coding is used to modify pictures, sounds, and movies.&lt;br /&gt;
Of course, we now mostly rely on editing programs, but underneath that program, when you press a button to trace the edges of a picture, the program finds the function that it runs and returns the output.&lt;br /&gt;
Such a code made look like this:&lt;br /&gt;
&lt;br /&gt;
 def luminance(pixel):&lt;br /&gt;
   r = getRed(pixel)&lt;br /&gt;
   g = getGreen(pixel)&lt;br /&gt;
   b = getBlue(pixel)&lt;br /&gt;
   return (r+g+b)/3&lt;br /&gt;
&lt;br /&gt;
 def edgeDetect(picture):&lt;br /&gt;
   for p in getPixels(picture):&lt;br /&gt;
     x = getX(p)	&lt;br /&gt;
     y = getY(p)&lt;br /&gt;
     if y &amp;lt; getHeight(picture) - 1 and x &amp;lt; getWidth(picture) - 1:&lt;br /&gt;
       botrt = getPixel(picture, x+1, y+1)&lt;br /&gt;
       thislum = luminance(p)&lt;br /&gt;
       brlum = luminance(botrt)&lt;br /&gt;
       if abs(brlum - thislum) &amp;gt; 8:&lt;br /&gt;
         setColor(p, blue)&lt;br /&gt;
       if abs(brlum - thislum) &amp;lt;= 8:&lt;br /&gt;
         setColor(p, white)&lt;br /&gt;
&lt;br /&gt;
When this program is run with the picture on the left, the output is the picture on the right.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:HW 2 Picture Original.png]] [[File:eiffel2.png]]&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26403</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26403"/>
		<updated>2016-11-28T03:26:24Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Connectedness */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
This is a simple function that creates a madlib with the 4 variables that are given as inputs.&lt;br /&gt;
&lt;br /&gt;
For example, if Sally was inputed as the name, lunch as noun1, pterodactyl as noun2, and flew as the verb, the the function will return the phrase &#039;&#039;Sally was looking at his lunch when a pterodactyl flew nearby&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; madlib(&amp;quot;Sally&amp;quot;, &amp;quot;lunch&amp;quot;, &amp;quot;pterodactyl&amp;quot;, &amp;quot;flew&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Sally was looking at his lunch when a pterodactyl flew nearby.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Returning the output given by a function===&lt;br /&gt;
&lt;br /&gt;
When a function takes an input and gives an output such as &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; in the madlib example, you cannot refer back to the output phrase &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;&lt;br /&gt;
If you would like to use this phrase again, then a function called &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is needed.&lt;br /&gt;
When &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is added at the end of a function, the output is passed back to the caller, which means that you can refer to the output now.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
   return result&lt;br /&gt;
&lt;br /&gt;
With this new code, if you need to refer to the sentence &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;, you can do so by by typing in &amp;lt;code&amp;gt;result&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Conditional Statement===&lt;br /&gt;
Lets say you have the following code:&lt;br /&gt;
&lt;br /&gt;
 # This function prints every letter in the input&lt;br /&gt;
 def printLetters(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
   print letter&lt;br /&gt;
&lt;br /&gt;
What if your &#039;&#039;evil&#039;&#039; TA asks you to only print the vowel letters given in the input?&lt;br /&gt;
This means that you need to add a condition to your function where the &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; function only works if the letter is a vowel.&lt;br /&gt;
This can be done by using &#039;&#039;&#039;conditional statement&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Condtional statement starts with &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 def justvowels(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
    if letter in &amp;quot;aeiou&amp;quot;:&lt;br /&gt;
      print letter&lt;br /&gt;
&lt;br /&gt;
Instead of printing every letter in the string, this code will only print &#039;&#039;&#039;if&#039;&#039;&#039; the letter in question can be found in the list &amp;quot;a,e,i,o,u&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
Writing a function for a task instead of copy and pasting code makes your program much easier to understand and debug. &lt;br /&gt;
&lt;br /&gt;
For example, to write a function that calculates the electric field created by a point charge, we first find the formula.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we ask ourselves what we need in order to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
 &lt;br /&gt;
We start by creating a sphere for a point charge&lt;br /&gt;
 p = sphere(pos=(-5e-11,0,0), radius=1e-11, color=color.red)&lt;br /&gt;
 &lt;br /&gt;
We also need to define the observation point where we calculate the electric field created by the point charge&lt;br /&gt;
 obslocation = vector(1, 1, 1)&lt;br /&gt;
&lt;br /&gt;
r, the distance between the obslocation and p&lt;br /&gt;
 r = obslocation - p.pos&lt;br /&gt;
&lt;br /&gt;
 # We can now write a function that calculates the electric field at a point away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
The final code will look like this:&lt;br /&gt;
&lt;br /&gt;
 from __future__ import division&lt;br /&gt;
 from visual import * &lt;br /&gt;
 p = sphere(pos=(-5e-11,0,0), radius=1e-11, color=color.red)&lt;br /&gt;
 obslocation = vector(1, 1, 1)&lt;br /&gt;
 r = obslocation - p.pos&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example of using these common functions==&lt;br /&gt;
&lt;br /&gt;
Remember this code from the &#039;&#039;&#039;Writing your own Functions&#039;&#039;&#039; section?&lt;br /&gt;
 &lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
We can now simplify this code using some of the common functions listed in &#039;&#039;&#039;Frequently Used Functions&#039;&#039;&#039; section.&lt;br /&gt;
&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     E = electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
It is also possible to skip defining the &amp;lt;code&amp;gt;electricConstant * q * r.norm() / r.mag2&amp;lt;/code&amp;gt; function and just return it.&lt;br /&gt;
&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
==Connectedness==&lt;br /&gt;
Functions are used everywhere in coding.&lt;br /&gt;
For example, in computational media, coding is used to modify pictures, sounds, and movies.&lt;br /&gt;
Of course, we now mostly rely on editing programs, but underneath that program, when you press a button to trace the edges of a picture, the program finds the function that it runs and returns the output.&lt;br /&gt;
Such a code made look like this:&lt;br /&gt;
&lt;br /&gt;
 def luminance(pixel):&lt;br /&gt;
   r = getRed(pixel)&lt;br /&gt;
   g = getGreen(pixel)&lt;br /&gt;
   b = getBlue(pixel)&lt;br /&gt;
   return (r+g+b)/3&lt;br /&gt;
&lt;br /&gt;
 def edgeDetect(picture):&lt;br /&gt;
   for p in getPixels(picture):&lt;br /&gt;
     x = getX(p)	&lt;br /&gt;
     y = getY(p)&lt;br /&gt;
     if y &amp;lt; getHeight(picture) - 1 and x &amp;lt; getWidth(picture) - 1:&lt;br /&gt;
       botrt = getPixel(picture, x+1, y+1)&lt;br /&gt;
       thislum = luminance(p)&lt;br /&gt;
       brlum = luminance(botrt)&lt;br /&gt;
       if abs(brlum - thislum) &amp;gt; 8:&lt;br /&gt;
         setColor(p, blue)&lt;br /&gt;
       if abs(brlum - thislum) &amp;lt;= 8:&lt;br /&gt;
         setColor(p, white)&lt;br /&gt;
&lt;br /&gt;
When this program is run with the picture on the left, the output is the picture on the right.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:HW 2 Picture Original.png]] [[File:eiffel2.png]]&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=File:Eiffel2.png&amp;diff=26400</id>
		<title>File:Eiffel2.png</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=File:Eiffel2.png&amp;diff=26400"/>
		<updated>2016-11-28T03:25:41Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: This is the output from the code.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This is the output from the code.&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26397</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26397"/>
		<updated>2016-11-28T03:25:16Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Connectedness */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
This is a simple function that creates a madlib with the 4 variables that are given as inputs.&lt;br /&gt;
&lt;br /&gt;
For example, if Sally was inputed as the name, lunch as noun1, pterodactyl as noun2, and flew as the verb, the the function will return the phrase &#039;&#039;Sally was looking at his lunch when a pterodactyl flew nearby&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; madlib(&amp;quot;Sally&amp;quot;, &amp;quot;lunch&amp;quot;, &amp;quot;pterodactyl&amp;quot;, &amp;quot;flew&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Sally was looking at his lunch when a pterodactyl flew nearby.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Returning the output given by a function===&lt;br /&gt;
&lt;br /&gt;
When a function takes an input and gives an output such as &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; in the madlib example, you cannot refer back to the output phrase &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;&lt;br /&gt;
If you would like to use this phrase again, then a function called &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is needed.&lt;br /&gt;
When &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is added at the end of a function, the output is passed back to the caller, which means that you can refer to the output now.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
   return result&lt;br /&gt;
&lt;br /&gt;
With this new code, if you need to refer to the sentence &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;, you can do so by by typing in &amp;lt;code&amp;gt;result&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Conditional Statement===&lt;br /&gt;
Lets say you have the following code:&lt;br /&gt;
&lt;br /&gt;
 # This function prints every letter in the input&lt;br /&gt;
 def printLetters(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
   print letter&lt;br /&gt;
&lt;br /&gt;
What if your &#039;&#039;evil&#039;&#039; TA asks you to only print the vowel letters given in the input?&lt;br /&gt;
This means that you need to add a condition to your function where the &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; function only works if the letter is a vowel.&lt;br /&gt;
This can be done by using &#039;&#039;&#039;conditional statement&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Condtional statement starts with &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 def justvowels(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
    if letter in &amp;quot;aeiou&amp;quot;:&lt;br /&gt;
      print letter&lt;br /&gt;
&lt;br /&gt;
Instead of printing every letter in the string, this code will only print &#039;&#039;&#039;if&#039;&#039;&#039; the letter in question can be found in the list &amp;quot;a,e,i,o,u&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
Writing a function for a task instead of copy and pasting code makes your program much easier to understand and debug. &lt;br /&gt;
&lt;br /&gt;
For example, to write a function that calculates the electric field created by a point charge, we first find the formula.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we ask ourselves what we need in order to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
 &lt;br /&gt;
We start by creating a sphere for a point charge&lt;br /&gt;
 p = sphere(pos=(-5e-11,0,0), radius=1e-11, color=color.red)&lt;br /&gt;
 &lt;br /&gt;
We also need to define the observation point where we calculate the electric field created by the point charge&lt;br /&gt;
 obslocation = vector(1, 1, 1)&lt;br /&gt;
&lt;br /&gt;
r, the distance between the obslocation and p&lt;br /&gt;
 r = obslocation - p.pos&lt;br /&gt;
&lt;br /&gt;
 # We can now write a function that calculates the electric field at a point away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
The final code will look like this:&lt;br /&gt;
&lt;br /&gt;
 from __future__ import division&lt;br /&gt;
 from visual import * &lt;br /&gt;
 p = sphere(pos=(-5e-11,0,0), radius=1e-11, color=color.red)&lt;br /&gt;
 obslocation = vector(1, 1, 1)&lt;br /&gt;
 r = obslocation - p.pos&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example of using these common functions==&lt;br /&gt;
&lt;br /&gt;
Remember this code from the &#039;&#039;&#039;Writing your own Functions&#039;&#039;&#039; section?&lt;br /&gt;
 &lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
We can now simplify this code using some of the common functions listed in &#039;&#039;&#039;Frequently Used Functions&#039;&#039;&#039; section.&lt;br /&gt;
&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     E = electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
It is also possible to skip defining the &amp;lt;code&amp;gt;electricConstant * q * r.norm() / r.mag2&amp;lt;/code&amp;gt; function and just return it.&lt;br /&gt;
&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
==Connectedness==&lt;br /&gt;
Functions are used everywhere in coding.&lt;br /&gt;
For example, in computational media, coding is used to modify pictures, sounds, and movies.&lt;br /&gt;
Of course, we now mostly rely on editing programs, but underneath that program, when you press a button to trace the edges of a picture, the program finds the function that it runs and returns the output.&lt;br /&gt;
Such a code made look like this:&lt;br /&gt;
&lt;br /&gt;
 def luminance(pixel):&lt;br /&gt;
   r = getRed(pixel)&lt;br /&gt;
   g = getGreen(pixel)&lt;br /&gt;
   b = getBlue(pixel)&lt;br /&gt;
   return (r+g+b)/3&lt;br /&gt;
&lt;br /&gt;
 def edgeDetect(picture):&lt;br /&gt;
   for p in getPixels(picture):&lt;br /&gt;
     x = getX(p)	&lt;br /&gt;
     y = getY(p)&lt;br /&gt;
     if y &amp;lt; getHeight(picture) - 1 and x &amp;lt; getWidth(picture) - 1:&lt;br /&gt;
       botrt = getPixel(picture, x+1, y+1)&lt;br /&gt;
       thislum = luminance(p)&lt;br /&gt;
       brlum = luminance(botrt)&lt;br /&gt;
       if abs(brlum - thislum) &amp;gt; 8:&lt;br /&gt;
         setColor(p, blue)&lt;br /&gt;
       if abs(brlum - thislum) &amp;lt;= 8:&lt;br /&gt;
         setColor(p, white)&lt;br /&gt;
&lt;br /&gt;
When this program is run with the picture on the left, the output is the picture on the right.&lt;br /&gt;
[[File:HW 2 Picture Original.png]] [[File:eiffel2.png]]&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=File:HW_2_Picture_Original.png&amp;diff=26392</id>
		<title>File:HW 2 Picture Original.png</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=File:HW_2_Picture_Original.png&amp;diff=26392"/>
		<updated>2016-11-28T03:24:25Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: Original Picture&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Original Picture&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26389</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26389"/>
		<updated>2016-11-28T03:23:59Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Connectedness */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
This is a simple function that creates a madlib with the 4 variables that are given as inputs.&lt;br /&gt;
&lt;br /&gt;
For example, if Sally was inputed as the name, lunch as noun1, pterodactyl as noun2, and flew as the verb, the the function will return the phrase &#039;&#039;Sally was looking at his lunch when a pterodactyl flew nearby&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; madlib(&amp;quot;Sally&amp;quot;, &amp;quot;lunch&amp;quot;, &amp;quot;pterodactyl&amp;quot;, &amp;quot;flew&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Sally was looking at his lunch when a pterodactyl flew nearby.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Returning the output given by a function===&lt;br /&gt;
&lt;br /&gt;
When a function takes an input and gives an output such as &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; in the madlib example, you cannot refer back to the output phrase &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;&lt;br /&gt;
If you would like to use this phrase again, then a function called &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is needed.&lt;br /&gt;
When &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is added at the end of a function, the output is passed back to the caller, which means that you can refer to the output now.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
   return result&lt;br /&gt;
&lt;br /&gt;
With this new code, if you need to refer to the sentence &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;, you can do so by by typing in &amp;lt;code&amp;gt;result&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Conditional Statement===&lt;br /&gt;
Lets say you have the following code:&lt;br /&gt;
&lt;br /&gt;
 # This function prints every letter in the input&lt;br /&gt;
 def printLetters(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
   print letter&lt;br /&gt;
&lt;br /&gt;
What if your &#039;&#039;evil&#039;&#039; TA asks you to only print the vowel letters given in the input?&lt;br /&gt;
This means that you need to add a condition to your function where the &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; function only works if the letter is a vowel.&lt;br /&gt;
This can be done by using &#039;&#039;&#039;conditional statement&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Condtional statement starts with &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 def justvowels(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
    if letter in &amp;quot;aeiou&amp;quot;:&lt;br /&gt;
      print letter&lt;br /&gt;
&lt;br /&gt;
Instead of printing every letter in the string, this code will only print &#039;&#039;&#039;if&#039;&#039;&#039; the letter in question can be found in the list &amp;quot;a,e,i,o,u&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
Writing a function for a task instead of copy and pasting code makes your program much easier to understand and debug. &lt;br /&gt;
&lt;br /&gt;
For example, to write a function that calculates the electric field created by a point charge, we first find the formula.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we ask ourselves what we need in order to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
 &lt;br /&gt;
We start by creating a sphere for a point charge&lt;br /&gt;
 p = sphere(pos=(-5e-11,0,0), radius=1e-11, color=color.red)&lt;br /&gt;
 &lt;br /&gt;
We also need to define the observation point where we calculate the electric field created by the point charge&lt;br /&gt;
 obslocation = vector(1, 1, 1)&lt;br /&gt;
&lt;br /&gt;
r, the distance between the obslocation and p&lt;br /&gt;
 r = obslocation - p.pos&lt;br /&gt;
&lt;br /&gt;
 # We can now write a function that calculates the electric field at a point away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
The final code will look like this:&lt;br /&gt;
&lt;br /&gt;
 from __future__ import division&lt;br /&gt;
 from visual import * &lt;br /&gt;
 p = sphere(pos=(-5e-11,0,0), radius=1e-11, color=color.red)&lt;br /&gt;
 obslocation = vector(1, 1, 1)&lt;br /&gt;
 r = obslocation - p.pos&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example of using these common functions==&lt;br /&gt;
&lt;br /&gt;
Remember this code from the &#039;&#039;&#039;Writing your own Functions&#039;&#039;&#039; section?&lt;br /&gt;
 &lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
We can now simplify this code using some of the common functions listed in &#039;&#039;&#039;Frequently Used Functions&#039;&#039;&#039; section.&lt;br /&gt;
&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     E = electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
It is also possible to skip defining the &amp;lt;code&amp;gt;electricConstant * q * r.norm() / r.mag2&amp;lt;/code&amp;gt; function and just return it.&lt;br /&gt;
&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
==Connectedness==&lt;br /&gt;
Functions are used everywhere in coding.&lt;br /&gt;
For example, in computational media, coding is used to modify pictures, sounds, and movies.&lt;br /&gt;
Of course, we now mostly rely on editing programs, but underneath that program, when you press a button to trace the edges of a picture, the program finds the function that it runs and returns the output.&lt;br /&gt;
Such a code made look like this:&lt;br /&gt;
&lt;br /&gt;
 def luminance(pixel):&lt;br /&gt;
   r = getRed(pixel)&lt;br /&gt;
   g = getGreen(pixel)&lt;br /&gt;
   b = getBlue(pixel)&lt;br /&gt;
   return (r+g+b)/3&lt;br /&gt;
&lt;br /&gt;
 def edgeDetect(picture):&lt;br /&gt;
   for p in getPixels(picture):&lt;br /&gt;
     x = getX(p)	&lt;br /&gt;
     y = getY(p)&lt;br /&gt;
     if y &amp;lt; getHeight(picture) - 1 and x &amp;lt; getWidth(picture) - 1:&lt;br /&gt;
       botrt = getPixel(picture, x+1, y+1)&lt;br /&gt;
       thislum = luminance(p)&lt;br /&gt;
       brlum = luminance(botrt)&lt;br /&gt;
       if abs(brlum - thislum) &amp;gt; 8:&lt;br /&gt;
         setColor(p, blue)&lt;br /&gt;
       if abs(brlum - thislum) &amp;lt;= 8:&lt;br /&gt;
         setColor(p, white)&lt;br /&gt;
&lt;br /&gt;
When this program is run with the picture on the left, the output is the picture on the right.&lt;br /&gt;
[[File:HW 2 Picture Original.png]]&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26384</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26384"/>
		<updated>2016-11-28T03:21:45Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Connectedness */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
This is a simple function that creates a madlib with the 4 variables that are given as inputs.&lt;br /&gt;
&lt;br /&gt;
For example, if Sally was inputed as the name, lunch as noun1, pterodactyl as noun2, and flew as the verb, the the function will return the phrase &#039;&#039;Sally was looking at his lunch when a pterodactyl flew nearby&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; madlib(&amp;quot;Sally&amp;quot;, &amp;quot;lunch&amp;quot;, &amp;quot;pterodactyl&amp;quot;, &amp;quot;flew&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Sally was looking at his lunch when a pterodactyl flew nearby.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Returning the output given by a function===&lt;br /&gt;
&lt;br /&gt;
When a function takes an input and gives an output such as &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; in the madlib example, you cannot refer back to the output phrase &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;&lt;br /&gt;
If you would like to use this phrase again, then a function called &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is needed.&lt;br /&gt;
When &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is added at the end of a function, the output is passed back to the caller, which means that you can refer to the output now.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
   return result&lt;br /&gt;
&lt;br /&gt;
With this new code, if you need to refer to the sentence &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;, you can do so by by typing in &amp;lt;code&amp;gt;result&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Conditional Statement===&lt;br /&gt;
Lets say you have the following code:&lt;br /&gt;
&lt;br /&gt;
 # This function prints every letter in the input&lt;br /&gt;
 def printLetters(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
   print letter&lt;br /&gt;
&lt;br /&gt;
What if your &#039;&#039;evil&#039;&#039; TA asks you to only print the vowel letters given in the input?&lt;br /&gt;
This means that you need to add a condition to your function where the &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; function only works if the letter is a vowel.&lt;br /&gt;
This can be done by using &#039;&#039;&#039;conditional statement&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Condtional statement starts with &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 def justvowels(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
    if letter in &amp;quot;aeiou&amp;quot;:&lt;br /&gt;
      print letter&lt;br /&gt;
&lt;br /&gt;
Instead of printing every letter in the string, this code will only print &#039;&#039;&#039;if&#039;&#039;&#039; the letter in question can be found in the list &amp;quot;a,e,i,o,u&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
Writing a function for a task instead of copy and pasting code makes your program much easier to understand and debug. &lt;br /&gt;
&lt;br /&gt;
For example, to write a function that calculates the electric field created by a point charge, we first find the formula.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we ask ourselves what we need in order to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
 &lt;br /&gt;
We start by creating a sphere for a point charge&lt;br /&gt;
 p = sphere(pos=(-5e-11,0,0), radius=1e-11, color=color.red)&lt;br /&gt;
 &lt;br /&gt;
We also need to define the observation point where we calculate the electric field created by the point charge&lt;br /&gt;
 obslocation = vector(1, 1, 1)&lt;br /&gt;
&lt;br /&gt;
r, the distance between the obslocation and p&lt;br /&gt;
 r = obslocation - p.pos&lt;br /&gt;
&lt;br /&gt;
 # We can now write a function that calculates the electric field at a point away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
The final code will look like this:&lt;br /&gt;
&lt;br /&gt;
 from __future__ import division&lt;br /&gt;
 from visual import * &lt;br /&gt;
 p = sphere(pos=(-5e-11,0,0), radius=1e-11, color=color.red)&lt;br /&gt;
 obslocation = vector(1, 1, 1)&lt;br /&gt;
 r = obslocation - p.pos&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example of using these common functions==&lt;br /&gt;
&lt;br /&gt;
Remember this code from the &#039;&#039;&#039;Writing your own Functions&#039;&#039;&#039; section?&lt;br /&gt;
 &lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
We can now simplify this code using some of the common functions listed in &#039;&#039;&#039;Frequently Used Functions&#039;&#039;&#039; section.&lt;br /&gt;
&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     E = electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
It is also possible to skip defining the &amp;lt;code&amp;gt;electricConstant * q * r.norm() / r.mag2&amp;lt;/code&amp;gt; function and just return it.&lt;br /&gt;
&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
==Connectedness==&lt;br /&gt;
Functions are used everywhere in coding.&lt;br /&gt;
For example, in computational media, coding is used to modify pictures, sounds, and movies.&lt;br /&gt;
Of course, we now mostly rely on editing programs, but underneath that program, when you press a button to trace the edges of a picture, the program finds the function that it runs and returns the output.&lt;br /&gt;
Such a code made look like this:&lt;br /&gt;
&lt;br /&gt;
 def luminance(pixel):&lt;br /&gt;
   r = getRed(pixel)&lt;br /&gt;
   g = getGreen(pixel)&lt;br /&gt;
   b = getBlue(pixel)&lt;br /&gt;
   return (r+g+b)/3&lt;br /&gt;
&lt;br /&gt;
 def edgeDetect(picture):&lt;br /&gt;
   for p in getPixels(picture):&lt;br /&gt;
     x = getX(p)	&lt;br /&gt;
     y = getY(p)&lt;br /&gt;
     if y &amp;lt; getHeight(picture) - 1 and x &amp;lt; getWidth(picture) - 1:&lt;br /&gt;
       botrt = getPixel(picture, x+1, y+1)&lt;br /&gt;
       thislum = luminance(p)&lt;br /&gt;
       brlum = luminance(botrt)&lt;br /&gt;
       if abs(brlum - thislum) &amp;gt; 8:&lt;br /&gt;
         setColor(p, blue)&lt;br /&gt;
       if abs(brlum - thislum) &amp;lt;= 8:&lt;br /&gt;
         setColor(p, white)&lt;br /&gt;
&lt;br /&gt;
When this program is run with the picture on the left, the output is the picture on the right.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26377</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26377"/>
		<updated>2016-11-28T03:20:21Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Connectedness */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
This is a simple function that creates a madlib with the 4 variables that are given as inputs.&lt;br /&gt;
&lt;br /&gt;
For example, if Sally was inputed as the name, lunch as noun1, pterodactyl as noun2, and flew as the verb, the the function will return the phrase &#039;&#039;Sally was looking at his lunch when a pterodactyl flew nearby&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; madlib(&amp;quot;Sally&amp;quot;, &amp;quot;lunch&amp;quot;, &amp;quot;pterodactyl&amp;quot;, &amp;quot;flew&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Sally was looking at his lunch when a pterodactyl flew nearby.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Returning the output given by a function===&lt;br /&gt;
&lt;br /&gt;
When a function takes an input and gives an output such as &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; in the madlib example, you cannot refer back to the output phrase &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;&lt;br /&gt;
If you would like to use this phrase again, then a function called &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is needed.&lt;br /&gt;
When &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is added at the end of a function, the output is passed back to the caller, which means that you can refer to the output now.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
   return result&lt;br /&gt;
&lt;br /&gt;
With this new code, if you need to refer to the sentence &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;, you can do so by by typing in &amp;lt;code&amp;gt;result&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Conditional Statement===&lt;br /&gt;
Lets say you have the following code:&lt;br /&gt;
&lt;br /&gt;
 # This function prints every letter in the input&lt;br /&gt;
 def printLetters(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
   print letter&lt;br /&gt;
&lt;br /&gt;
What if your &#039;&#039;evil&#039;&#039; TA asks you to only print the vowel letters given in the input?&lt;br /&gt;
This means that you need to add a condition to your function where the &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; function only works if the letter is a vowel.&lt;br /&gt;
This can be done by using &#039;&#039;&#039;conditional statement&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Condtional statement starts with &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 def justvowels(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
    if letter in &amp;quot;aeiou&amp;quot;:&lt;br /&gt;
      print letter&lt;br /&gt;
&lt;br /&gt;
Instead of printing every letter in the string, this code will only print &#039;&#039;&#039;if&#039;&#039;&#039; the letter in question can be found in the list &amp;quot;a,e,i,o,u&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
Writing a function for a task instead of copy and pasting code makes your program much easier to understand and debug. &lt;br /&gt;
&lt;br /&gt;
For example, to write a function that calculates the electric field created by a point charge, we first find the formula.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we ask ourselves what we need in order to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
 &lt;br /&gt;
We start by creating a sphere for a point charge&lt;br /&gt;
 p = sphere(pos=(-5e-11,0,0), radius=1e-11, color=color.red)&lt;br /&gt;
 &lt;br /&gt;
We also need to define the observation point where we calculate the electric field created by the point charge&lt;br /&gt;
 obslocation = vector(1, 1, 1)&lt;br /&gt;
&lt;br /&gt;
r, the distance between the obslocation and p&lt;br /&gt;
 r = obslocation - p.pos&lt;br /&gt;
&lt;br /&gt;
 # We can now write a function that calculates the electric field at a point away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
The final code will look like this:&lt;br /&gt;
&lt;br /&gt;
 from __future__ import division&lt;br /&gt;
 from visual import * &lt;br /&gt;
 p = sphere(pos=(-5e-11,0,0), radius=1e-11, color=color.red)&lt;br /&gt;
 obslocation = vector(1, 1, 1)&lt;br /&gt;
 r = obslocation - p.pos&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example of using these common functions==&lt;br /&gt;
&lt;br /&gt;
Remember this code from the &#039;&#039;&#039;Writing your own Functions&#039;&#039;&#039; section?&lt;br /&gt;
 &lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
We can now simplify this code using some of the common functions listed in &#039;&#039;&#039;Frequently Used Functions&#039;&#039;&#039; section.&lt;br /&gt;
&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     E = electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
It is also possible to skip defining the &amp;lt;code&amp;gt;electricConstant * q * r.norm() / r.mag2&amp;lt;/code&amp;gt; function and just return it.&lt;br /&gt;
&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
==Connectedness==&lt;br /&gt;
Functions are used everywhere in coding.&lt;br /&gt;
For example, in computational media, coding is used to modify pictures, sounds, and movies.&lt;br /&gt;
Of course, we now mostly rely on editing programs, but underneath that program, when you press a button to trace the edges of a picture, the program finds the function that it runs and returns the output.&lt;br /&gt;
Such a code made look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 def luminance(pixel):&lt;br /&gt;
   r = getRed(pixel)&lt;br /&gt;
   g = getGreen(pixel)&lt;br /&gt;
   b = getBlue(pixel)&lt;br /&gt;
   return (r+g+b)/3&lt;br /&gt;
&lt;br /&gt;
 def edgeDetect(picture):&lt;br /&gt;
   for p in getPixels(picture):&lt;br /&gt;
     x = getX(p)	&lt;br /&gt;
     y = getY(p)&lt;br /&gt;
     if y &amp;lt; getHeight(picture) - 1 and x &amp;lt; getWidth(picture) - 1:&lt;br /&gt;
       botrt = getPixel(picture, x+1, y+1)&lt;br /&gt;
       thislum = luminance(p)&lt;br /&gt;
       brlum = luminance(botrt)&lt;br /&gt;
       if abs(brlum - thislum) &amp;gt; 8:&lt;br /&gt;
         setColor(p, blue)&lt;br /&gt;
       if abs(brlum - thislum) &amp;lt;= 8:&lt;br /&gt;
         setColor(p, white)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26374</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26374"/>
		<updated>2016-11-28T03:19:45Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Example of using these common functions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
This is a simple function that creates a madlib with the 4 variables that are given as inputs.&lt;br /&gt;
&lt;br /&gt;
For example, if Sally was inputed as the name, lunch as noun1, pterodactyl as noun2, and flew as the verb, the the function will return the phrase &#039;&#039;Sally was looking at his lunch when a pterodactyl flew nearby&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; madlib(&amp;quot;Sally&amp;quot;, &amp;quot;lunch&amp;quot;, &amp;quot;pterodactyl&amp;quot;, &amp;quot;flew&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Sally was looking at his lunch when a pterodactyl flew nearby.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Returning the output given by a function===&lt;br /&gt;
&lt;br /&gt;
When a function takes an input and gives an output such as &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; in the madlib example, you cannot refer back to the output phrase &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;&lt;br /&gt;
If you would like to use this phrase again, then a function called &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is needed.&lt;br /&gt;
When &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is added at the end of a function, the output is passed back to the caller, which means that you can refer to the output now.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
   return result&lt;br /&gt;
&lt;br /&gt;
With this new code, if you need to refer to the sentence &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;, you can do so by by typing in &amp;lt;code&amp;gt;result&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Conditional Statement===&lt;br /&gt;
Lets say you have the following code:&lt;br /&gt;
&lt;br /&gt;
 # This function prints every letter in the input&lt;br /&gt;
 def printLetters(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
   print letter&lt;br /&gt;
&lt;br /&gt;
What if your &#039;&#039;evil&#039;&#039; TA asks you to only print the vowel letters given in the input?&lt;br /&gt;
This means that you need to add a condition to your function where the &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; function only works if the letter is a vowel.&lt;br /&gt;
This can be done by using &#039;&#039;&#039;conditional statement&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Condtional statement starts with &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 def justvowels(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
    if letter in &amp;quot;aeiou&amp;quot;:&lt;br /&gt;
      print letter&lt;br /&gt;
&lt;br /&gt;
Instead of printing every letter in the string, this code will only print &#039;&#039;&#039;if&#039;&#039;&#039; the letter in question can be found in the list &amp;quot;a,e,i,o,u&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
Writing a function for a task instead of copy and pasting code makes your program much easier to understand and debug. &lt;br /&gt;
&lt;br /&gt;
For example, to write a function that calculates the electric field created by a point charge, we first find the formula.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we ask ourselves what we need in order to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
 &lt;br /&gt;
We start by creating a sphere for a point charge&lt;br /&gt;
 p = sphere(pos=(-5e-11,0,0), radius=1e-11, color=color.red)&lt;br /&gt;
 &lt;br /&gt;
We also need to define the observation point where we calculate the electric field created by the point charge&lt;br /&gt;
 obslocation = vector(1, 1, 1)&lt;br /&gt;
&lt;br /&gt;
r, the distance between the obslocation and p&lt;br /&gt;
 r = obslocation - p.pos&lt;br /&gt;
&lt;br /&gt;
 # We can now write a function that calculates the electric field at a point away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
The final code will look like this:&lt;br /&gt;
&lt;br /&gt;
 from __future__ import division&lt;br /&gt;
 from visual import * &lt;br /&gt;
 p = sphere(pos=(-5e-11,0,0), radius=1e-11, color=color.red)&lt;br /&gt;
 obslocation = vector(1, 1, 1)&lt;br /&gt;
 r = obslocation - p.pos&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example of using these common functions==&lt;br /&gt;
&lt;br /&gt;
Remember this code from the &#039;&#039;&#039;Writing your own Functions&#039;&#039;&#039; section?&lt;br /&gt;
 &lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
We can now simplify this code using some of the common functions listed in &#039;&#039;&#039;Frequently Used Functions&#039;&#039;&#039; section.&lt;br /&gt;
&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     E = electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
It is also possible to skip defining the &amp;lt;code&amp;gt;electricConstant * q * r.norm() / r.mag2&amp;lt;/code&amp;gt; function and just return it.&lt;br /&gt;
&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
==Connectedness==&lt;br /&gt;
Functions are used everywhere in coding.&lt;br /&gt;
For example, in computational media, coding is used to modify pictures, sounds, and movies.&lt;br /&gt;
Of course, we now mostly rely on editing programs, but underneath that program, when you press a button to trace the edges of a picture, the program finds the function that it runs and returns the output.&lt;br /&gt;
Such a code made look like this:&lt;br /&gt;
&lt;br /&gt;
 def luminance(pixel):&lt;br /&gt;
   r = getRed(pixel)&lt;br /&gt;
   g = getGreen(pixel)&lt;br /&gt;
   b = getBlue(pixel)&lt;br /&gt;
   return (r+g+b)/3&lt;br /&gt;
&lt;br /&gt;
 def edgeDetect(picture):&lt;br /&gt;
   for p in getPixels(picture):&lt;br /&gt;
     x = getX(p)	&lt;br /&gt;
     y = getY(p)&lt;br /&gt;
     if y &amp;lt; getHeight(picture) - 1 and x &amp;lt; getWidth(picture) - 1:&lt;br /&gt;
       botrt = getPixel(picture, x+1, y+1)&lt;br /&gt;
       thislum = luminance(p)&lt;br /&gt;
       brlum = luminance(botrt)&lt;br /&gt;
       if abs(brlum - thislum) &amp;gt; 8:&lt;br /&gt;
         setColor(p, blue)&lt;br /&gt;
       if abs(brlum - thislum) &amp;lt;= 8:&lt;br /&gt;
         setColor(p, white)&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26331</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26331"/>
		<updated>2016-11-28T03:11:08Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Example of using these common functions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
This is a simple function that creates a madlib with the 4 variables that are given as inputs.&lt;br /&gt;
&lt;br /&gt;
For example, if Sally was inputed as the name, lunch as noun1, pterodactyl as noun2, and flew as the verb, the the function will return the phrase &#039;&#039;Sally was looking at his lunch when a pterodactyl flew nearby&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; madlib(&amp;quot;Sally&amp;quot;, &amp;quot;lunch&amp;quot;, &amp;quot;pterodactyl&amp;quot;, &amp;quot;flew&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Sally was looking at his lunch when a pterodactyl flew nearby.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Returning the output given by a function===&lt;br /&gt;
&lt;br /&gt;
When a function takes an input and gives an output such as &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; in the madlib example, you cannot refer back to the output phrase &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;&lt;br /&gt;
If you would like to use this phrase again, then a function called &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is needed.&lt;br /&gt;
When &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is added at the end of a function, the output is passed back to the caller, which means that you can refer to the output now.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
   return result&lt;br /&gt;
&lt;br /&gt;
With this new code, if you need to refer to the sentence &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;, you can do so by by typing in &amp;lt;code&amp;gt;result&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Conditional Statement===&lt;br /&gt;
Lets say you have the following code:&lt;br /&gt;
&lt;br /&gt;
 # This function prints every letter in the input&lt;br /&gt;
 def printLetters(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
   print letter&lt;br /&gt;
&lt;br /&gt;
What if your &#039;&#039;evil&#039;&#039; TA asks you to only print the vowel letters given in the input?&lt;br /&gt;
This means that you need to add a condition to your function where the &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; function only works if the letter is a vowel.&lt;br /&gt;
This can be done by using &#039;&#039;&#039;conditional statement&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Condtional statement starts with &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 def justvowels(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
    if letter in &amp;quot;aeiou&amp;quot;:&lt;br /&gt;
      print letter&lt;br /&gt;
&lt;br /&gt;
Instead of printing every letter in the string, this code will only print &#039;&#039;&#039;if&#039;&#039;&#039; the letter in question can be found in the list &amp;quot;a,e,i,o,u&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
Writing a function for a task instead of copy and pasting code makes your program much easier to understand and debug. &lt;br /&gt;
&lt;br /&gt;
For example, to write a function that calculates the electric field created by a point charge, we first find the formula.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we ask ourselves what we need in order to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
 &lt;br /&gt;
We start by creating a sphere for a point charge&lt;br /&gt;
 p = sphere(pos=(-5e-11,0,0), radius=1e-11, color=color.red)&lt;br /&gt;
 &lt;br /&gt;
We also need to define the observation point where we calculate the electric field created by the point charge&lt;br /&gt;
 obslocation = vector(1, 1, 1)&lt;br /&gt;
&lt;br /&gt;
r, the distance between the obslocation and p&lt;br /&gt;
 r = obslocation - p.pos&lt;br /&gt;
&lt;br /&gt;
 # We can now write a function that calculates the electric field at a point away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
The final code will look like this:&lt;br /&gt;
&lt;br /&gt;
 from __future__ import division&lt;br /&gt;
 from visual import * &lt;br /&gt;
 p = sphere(pos=(-5e-11,0,0), radius=1e-11, color=color.red)&lt;br /&gt;
 obslocation = vector(1, 1, 1)&lt;br /&gt;
 r = obslocation - p.pos&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example of using these common functions==&lt;br /&gt;
&lt;br /&gt;
Remember this code from the &#039;&#039;&#039;Writing your own Functions&#039;&#039;&#039; section?&lt;br /&gt;
 &lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
We can now simplify this code using some of the common functions listed in &#039;&#039;&#039;Frequently Used Functions&#039;&#039;&#039; section.&lt;br /&gt;
&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     E = electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
It is also possible to skip defining the &amp;lt;code&amp;gt;electricConstant * q * r.norm() / r.mag2&amp;lt;/code&amp;gt; function and just return it.&lt;br /&gt;
&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26327</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26327"/>
		<updated>2016-11-28T03:10:19Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Example of using these common functions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
This is a simple function that creates a madlib with the 4 variables that are given as inputs.&lt;br /&gt;
&lt;br /&gt;
For example, if Sally was inputed as the name, lunch as noun1, pterodactyl as noun2, and flew as the verb, the the function will return the phrase &#039;&#039;Sally was looking at his lunch when a pterodactyl flew nearby&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; madlib(&amp;quot;Sally&amp;quot;, &amp;quot;lunch&amp;quot;, &amp;quot;pterodactyl&amp;quot;, &amp;quot;flew&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Sally was looking at his lunch when a pterodactyl flew nearby.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Returning the output given by a function===&lt;br /&gt;
&lt;br /&gt;
When a function takes an input and gives an output such as &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; in the madlib example, you cannot refer back to the output phrase &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;&lt;br /&gt;
If you would like to use this phrase again, then a function called &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is needed.&lt;br /&gt;
When &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is added at the end of a function, the output is passed back to the caller, which means that you can refer to the output now.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
   return result&lt;br /&gt;
&lt;br /&gt;
With this new code, if you need to refer to the sentence &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;, you can do so by by typing in &amp;lt;code&amp;gt;result&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Conditional Statement===&lt;br /&gt;
Lets say you have the following code:&lt;br /&gt;
&lt;br /&gt;
 # This function prints every letter in the input&lt;br /&gt;
 def printLetters(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
   print letter&lt;br /&gt;
&lt;br /&gt;
What if your &#039;&#039;evil&#039;&#039; TA asks you to only print the vowel letters given in the input?&lt;br /&gt;
This means that you need to add a condition to your function where the &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; function only works if the letter is a vowel.&lt;br /&gt;
This can be done by using &#039;&#039;&#039;conditional statement&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Condtional statement starts with &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 def justvowels(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
    if letter in &amp;quot;aeiou&amp;quot;:&lt;br /&gt;
      print letter&lt;br /&gt;
&lt;br /&gt;
Instead of printing every letter in the string, this code will only print &#039;&#039;&#039;if&#039;&#039;&#039; the letter in question can be found in the list &amp;quot;a,e,i,o,u&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
Writing a function for a task instead of copy and pasting code makes your program much easier to understand and debug. &lt;br /&gt;
&lt;br /&gt;
For example, to write a function that calculates the electric field created by a point charge, we first find the formula.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we ask ourselves what we need in order to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
 &lt;br /&gt;
We start by creating a sphere for a point charge&lt;br /&gt;
 p = sphere(pos=(-5e-11,0,0), radius=1e-11, color=color.red)&lt;br /&gt;
 &lt;br /&gt;
We also need to define the observation point where we calculate the electric field created by the point charge&lt;br /&gt;
 obslocation = vector(1, 1, 1)&lt;br /&gt;
&lt;br /&gt;
r, the distance between the obslocation and p&lt;br /&gt;
 r = obslocation - p.pos&lt;br /&gt;
&lt;br /&gt;
 # We can now write a function that calculates the electric field at a point away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
The final code will look like this:&lt;br /&gt;
&lt;br /&gt;
 from __future__ import division&lt;br /&gt;
 from visual import * &lt;br /&gt;
 p = sphere(pos=(-5e-11,0,0), radius=1e-11, color=color.red)&lt;br /&gt;
 obslocation = vector(1, 1, 1)&lt;br /&gt;
 r = obslocation - p.pos&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example of using these common functions==&lt;br /&gt;
&lt;br /&gt;
Remember this code from the &#039;&#039;&#039;Writing your own Functions&#039;&#039;&#039; section?&lt;br /&gt;
 &lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
We can now simplify this code using some of the common functions listed above.&lt;br /&gt;
&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     E = electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
It is also possible to skip defining the &amp;lt;code&amp;gt;electricConstant * q * r.norm() / r.mag2&amp;lt;/code&amp;gt; function and just return it.&lt;br /&gt;
&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26325</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26325"/>
		<updated>2016-11-28T03:10:00Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Example of using these common functions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
This is a simple function that creates a madlib with the 4 variables that are given as inputs.&lt;br /&gt;
&lt;br /&gt;
For example, if Sally was inputed as the name, lunch as noun1, pterodactyl as noun2, and flew as the verb, the the function will return the phrase &#039;&#039;Sally was looking at his lunch when a pterodactyl flew nearby&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; madlib(&amp;quot;Sally&amp;quot;, &amp;quot;lunch&amp;quot;, &amp;quot;pterodactyl&amp;quot;, &amp;quot;flew&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Sally was looking at his lunch when a pterodactyl flew nearby.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Returning the output given by a function===&lt;br /&gt;
&lt;br /&gt;
When a function takes an input and gives an output such as &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; in the madlib example, you cannot refer back to the output phrase &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;&lt;br /&gt;
If you would like to use this phrase again, then a function called &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is needed.&lt;br /&gt;
When &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is added at the end of a function, the output is passed back to the caller, which means that you can refer to the output now.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
   return result&lt;br /&gt;
&lt;br /&gt;
With this new code, if you need to refer to the sentence &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;, you can do so by by typing in &amp;lt;code&amp;gt;result&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Conditional Statement===&lt;br /&gt;
Lets say you have the following code:&lt;br /&gt;
&lt;br /&gt;
 # This function prints every letter in the input&lt;br /&gt;
 def printLetters(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
   print letter&lt;br /&gt;
&lt;br /&gt;
What if your &#039;&#039;evil&#039;&#039; TA asks you to only print the vowel letters given in the input?&lt;br /&gt;
This means that you need to add a condition to your function where the &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; function only works if the letter is a vowel.&lt;br /&gt;
This can be done by using &#039;&#039;&#039;conditional statement&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Condtional statement starts with &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 def justvowels(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
    if letter in &amp;quot;aeiou&amp;quot;:&lt;br /&gt;
      print letter&lt;br /&gt;
&lt;br /&gt;
Instead of printing every letter in the string, this code will only print &#039;&#039;&#039;if&#039;&#039;&#039; the letter in question can be found in the list &amp;quot;a,e,i,o,u&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
Writing a function for a task instead of copy and pasting code makes your program much easier to understand and debug. &lt;br /&gt;
&lt;br /&gt;
For example, to write a function that calculates the electric field created by a point charge, we first find the formula.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we ask ourselves what we need in order to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
 &lt;br /&gt;
We start by creating a sphere for a point charge&lt;br /&gt;
 p = sphere(pos=(-5e-11,0,0), radius=1e-11, color=color.red)&lt;br /&gt;
 &lt;br /&gt;
We also need to define the observation point where we calculate the electric field created by the point charge&lt;br /&gt;
 obslocation = vector(1, 1, 1)&lt;br /&gt;
&lt;br /&gt;
r, the distance between the obslocation and p&lt;br /&gt;
 r = obslocation - p.pos&lt;br /&gt;
&lt;br /&gt;
 # We can now write a function that calculates the electric field at a point away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
The final code will look like this:&lt;br /&gt;
&lt;br /&gt;
 from __future__ import division&lt;br /&gt;
 from visual import * &lt;br /&gt;
 p = sphere(pos=(-5e-11,0,0), radius=1e-11, color=color.red)&lt;br /&gt;
 obslocation = vector(1, 1, 1)&lt;br /&gt;
 r = obslocation - p.pos&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example of using these common functions==&lt;br /&gt;
&lt;br /&gt;
Remember this code from the &#039;&#039;&#039;Writing your Own Functions&#039;&#039;&#039; section?&lt;br /&gt;
 &lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
We can now simplify this code using some of the common functions listed above.&lt;br /&gt;
&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     E = electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
It is also possible to skip defining the &amp;lt;code&amp;gt;electricConstant * q * r.norm() / r.mag2&amp;lt;/code&amp;gt; function and just return it.&lt;br /&gt;
&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26319</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26319"/>
		<updated>2016-11-28T03:07:35Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Example of using these common functions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
This is a simple function that creates a madlib with the 4 variables that are given as inputs.&lt;br /&gt;
&lt;br /&gt;
For example, if Sally was inputed as the name, lunch as noun1, pterodactyl as noun2, and flew as the verb, the the function will return the phrase &#039;&#039;Sally was looking at his lunch when a pterodactyl flew nearby&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; madlib(&amp;quot;Sally&amp;quot;, &amp;quot;lunch&amp;quot;, &amp;quot;pterodactyl&amp;quot;, &amp;quot;flew&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Sally was looking at his lunch when a pterodactyl flew nearby.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Returning the output given by a function===&lt;br /&gt;
&lt;br /&gt;
When a function takes an input and gives an output such as &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; in the madlib example, you cannot refer back to the output phrase &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;&lt;br /&gt;
If you would like to use this phrase again, then a function called &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is needed.&lt;br /&gt;
When &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is added at the end of a function, the output is passed back to the caller, which means that you can refer to the output now.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
   return result&lt;br /&gt;
&lt;br /&gt;
With this new code, if you need to refer to the sentence &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;, you can do so by by typing in &amp;lt;code&amp;gt;result&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Conditional Statement===&lt;br /&gt;
Lets say you have the following code:&lt;br /&gt;
&lt;br /&gt;
 # This function prints every letter in the input&lt;br /&gt;
 def printLetters(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
   print letter&lt;br /&gt;
&lt;br /&gt;
What if your &#039;&#039;evil&#039;&#039; TA asks you to only print the vowel letters given in the input?&lt;br /&gt;
This means that you need to add a condition to your function where the &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; function only works if the letter is a vowel.&lt;br /&gt;
This can be done by using &#039;&#039;&#039;conditional statement&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Condtional statement starts with &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 def justvowels(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
    if letter in &amp;quot;aeiou&amp;quot;:&lt;br /&gt;
      print letter&lt;br /&gt;
&lt;br /&gt;
Instead of printing every letter in the string, this code will only print &#039;&#039;&#039;if&#039;&#039;&#039; the letter in question can be found in the list &amp;quot;a,e,i,o,u&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
Writing a function for a task instead of copy and pasting code makes your program much easier to understand and debug. &lt;br /&gt;
&lt;br /&gt;
For example, to write a function that calculates the electric field created by a point charge, we first find the formula.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we ask ourselves what we need in order to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
 &lt;br /&gt;
We start by creating a sphere for a point charge&lt;br /&gt;
 p = sphere(pos=(-5e-11,0,0), radius=1e-11, color=color.red)&lt;br /&gt;
 &lt;br /&gt;
We also need to define the observation point where we calculate the electric field created by the point charge&lt;br /&gt;
 obslocation = vector(1, 1, 1)&lt;br /&gt;
&lt;br /&gt;
r, the distance between the obslocation and p&lt;br /&gt;
 r = obslocation - p.pos&lt;br /&gt;
&lt;br /&gt;
 # We can now write a function that calculates the electric field at a point away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
The final code will look like this:&lt;br /&gt;
&lt;br /&gt;
 from __future__ import division&lt;br /&gt;
 from visual import * &lt;br /&gt;
 p = sphere(pos=(-5e-11,0,0), radius=1e-11, color=color.red)&lt;br /&gt;
 obslocation = vector(1, 1, 1)&lt;br /&gt;
 r = obslocation - p.pos&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example of using these common functions==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 # Calculates the electric field at a given vector away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     E = electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
It is also possible to skip defining the &amp;lt;code&amp;gt;electricConstant * q * r.norm() / r.mag2&amp;lt;/code&amp;gt; function and just return it.&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26314</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26314"/>
		<updated>2016-11-28T03:06:08Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Writing your own Functions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
This is a simple function that creates a madlib with the 4 variables that are given as inputs.&lt;br /&gt;
&lt;br /&gt;
For example, if Sally was inputed as the name, lunch as noun1, pterodactyl as noun2, and flew as the verb, the the function will return the phrase &#039;&#039;Sally was looking at his lunch when a pterodactyl flew nearby&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; madlib(&amp;quot;Sally&amp;quot;, &amp;quot;lunch&amp;quot;, &amp;quot;pterodactyl&amp;quot;, &amp;quot;flew&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Sally was looking at his lunch when a pterodactyl flew nearby.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Returning the output given by a function===&lt;br /&gt;
&lt;br /&gt;
When a function takes an input and gives an output such as &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; in the madlib example, you cannot refer back to the output phrase &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;&lt;br /&gt;
If you would like to use this phrase again, then a function called &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is needed.&lt;br /&gt;
When &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is added at the end of a function, the output is passed back to the caller, which means that you can refer to the output now.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
   return result&lt;br /&gt;
&lt;br /&gt;
With this new code, if you need to refer to the sentence &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;, you can do so by by typing in &amp;lt;code&amp;gt;result&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Conditional Statement===&lt;br /&gt;
Lets say you have the following code:&lt;br /&gt;
&lt;br /&gt;
 # This function prints every letter in the input&lt;br /&gt;
 def printLetters(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
   print letter&lt;br /&gt;
&lt;br /&gt;
What if your &#039;&#039;evil&#039;&#039; TA asks you to only print the vowel letters given in the input?&lt;br /&gt;
This means that you need to add a condition to your function where the &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; function only works if the letter is a vowel.&lt;br /&gt;
This can be done by using &#039;&#039;&#039;conditional statement&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Condtional statement starts with &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 def justvowels(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
    if letter in &amp;quot;aeiou&amp;quot;:&lt;br /&gt;
      print letter&lt;br /&gt;
&lt;br /&gt;
Instead of printing every letter in the string, this code will only print &#039;&#039;&#039;if&#039;&#039;&#039; the letter in question can be found in the list &amp;quot;a,e,i,o,u&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
Writing a function for a task instead of copy and pasting code makes your program much easier to understand and debug. &lt;br /&gt;
&lt;br /&gt;
For example, to write a function that calculates the electric field created by a point charge, we first find the formula.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we ask ourselves what we need in order to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
 &lt;br /&gt;
We start by creating a sphere for a point charge&lt;br /&gt;
 p = sphere(pos=(-5e-11,0,0), radius=1e-11, color=color.red)&lt;br /&gt;
 &lt;br /&gt;
We also need to define the observation point where we calculate the electric field created by the point charge&lt;br /&gt;
 obslocation = vector(1, 1, 1)&lt;br /&gt;
&lt;br /&gt;
r, the distance between the obslocation and p&lt;br /&gt;
 r = obslocation - p.pos&lt;br /&gt;
&lt;br /&gt;
 # We can now write a function that calculates the electric field at a point away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
The final code will look like this:&lt;br /&gt;
&lt;br /&gt;
 from __future__ import division&lt;br /&gt;
 from visual import * &lt;br /&gt;
 p = sphere(pos=(-5e-11,0,0), radius=1e-11, color=color.red)&lt;br /&gt;
 obslocation = vector(1, 1, 1)&lt;br /&gt;
 r = obslocation - p.pos&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example of using these common functions==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The best way to illustrate this is with an example.&lt;br /&gt;
&lt;br /&gt;
For this example, we will create a function to calculate the electric field created by a point charge.&lt;br /&gt;
&lt;br /&gt;
As a reminder, this is the formula we are modeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First, we ask what do we need to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
&lt;br /&gt;
 # Calculates the electric field at a given vector away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     E = electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
It is also possible to skip defining the &amp;lt;code&amp;gt;electricConstant * q * r.norm() / r.mag2&amp;lt;/code&amp;gt; function and just return it.&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26304</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26304"/>
		<updated>2016-11-28T03:04:39Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Writing your own Functions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
This is a simple function that creates a madlib with the 4 variables that are given as inputs.&lt;br /&gt;
&lt;br /&gt;
For example, if Sally was inputed as the name, lunch as noun1, pterodactyl as noun2, and flew as the verb, the the function will return the phrase &#039;&#039;Sally was looking at his lunch when a pterodactyl flew nearby&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; madlib(&amp;quot;Sally&amp;quot;, &amp;quot;lunch&amp;quot;, &amp;quot;pterodactyl&amp;quot;, &amp;quot;flew&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Sally was looking at his lunch when a pterodactyl flew nearby.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Returning the output given by a function===&lt;br /&gt;
&lt;br /&gt;
When a function takes an input and gives an output such as &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; in the madlib example, you cannot refer back to the output phrase &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;&lt;br /&gt;
If you would like to use this phrase again, then a function called &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is needed.&lt;br /&gt;
When &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is added at the end of a function, the output is passed back to the caller, which means that you can refer to the output now.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
   return result&lt;br /&gt;
&lt;br /&gt;
With this new code, if you need to refer to the sentence &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;, you can do so by by typing in &amp;lt;code&amp;gt;result&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Conditional Statement===&lt;br /&gt;
Lets say you have the following code:&lt;br /&gt;
&lt;br /&gt;
 # This function prints every letter in the input&lt;br /&gt;
 def printLetters(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
   print letter&lt;br /&gt;
&lt;br /&gt;
What if your &#039;&#039;evil&#039;&#039; TA asks you to only print the vowel letters given in the input?&lt;br /&gt;
This means that you need to add a condition to your function where the &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; function only works if the letter is a vowel.&lt;br /&gt;
This can be done by using &#039;&#039;&#039;conditional statement&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Condtional statement starts with &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 def justvowels(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
    if letter in &amp;quot;aeiou&amp;quot;:&lt;br /&gt;
      print letter&lt;br /&gt;
&lt;br /&gt;
Instead of printing every letter in the string, this code will only print &#039;&#039;&#039;if&#039;&#039;&#039; the letter in question can be found in the list &amp;quot;a,e,i,o,u&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
Writing a function for a task instead of copy and pasting code makes your program much easier to understand and debug. &lt;br /&gt;
&lt;br /&gt;
For example, to write a function that calculates the electric field created by a point charge, we first find the formula.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we ask ourselves what we need in order to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
 &lt;br /&gt;
 #We start by creating a sphere for a point charge&lt;br /&gt;
 p = sphere(pos=(-5e-11,0,0), radius=1e-11, color=color.red)&lt;br /&gt;
 &lt;br /&gt;
 #We also need to define the observation point where we calculate the electric field created by the point charge&lt;br /&gt;
 obslocation = vector(1, 1, 1)&lt;br /&gt;
&lt;br /&gt;
 #r, the distance between the obslocation and p&lt;br /&gt;
 r = obslocation - p.pos&lt;br /&gt;
&lt;br /&gt;
 #We can now write a function that calculates the electric field at a point away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     rmag = sqrt((ap.x)**2+(ap.y)**2+(ap.z)**2)&lt;br /&gt;
     rhat = r / rmag&lt;br /&gt;
     E = (electricConstant * q / rmag**2)*rhat&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example of using these common functions==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The best way to illustrate this is with an example.&lt;br /&gt;
&lt;br /&gt;
For this example, we will create a function to calculate the electric field created by a point charge.&lt;br /&gt;
&lt;br /&gt;
As a reminder, this is the formula we are modeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First, we ask what do we need to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
&lt;br /&gt;
 # Calculates the electric field at a given vector away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     E = electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
It is also possible to skip defining the &amp;lt;code&amp;gt;electricConstant * q * r.norm() / r.mag2&amp;lt;/code&amp;gt; function and just return it.&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26187</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26187"/>
		<updated>2016-11-28T02:50:30Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Writing your own Functions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
This is a simple function that creates a madlib with the 4 variables that are given as inputs.&lt;br /&gt;
&lt;br /&gt;
For example, if Sally was inputed as the name, lunch as noun1, pterodactyl as noun2, and flew as the verb, the the function will return the phrase &#039;&#039;Sally was looking at his lunch when a pterodactyl flew nearby&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; madlib(&amp;quot;Sally&amp;quot;, &amp;quot;lunch&amp;quot;, &amp;quot;pterodactyl&amp;quot;, &amp;quot;flew&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Sally was looking at his lunch when a pterodactyl flew nearby.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Returning the output given by a function===&lt;br /&gt;
&lt;br /&gt;
When a function takes an input and gives an output such as &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; in the madlib example, you cannot refer back to the output phrase &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;&lt;br /&gt;
If you would like to use this phrase again, then a function called &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is needed.&lt;br /&gt;
When &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is added at the end of a function, the output is passed back to the caller, which means that you can refer to the output now.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
   return result&lt;br /&gt;
&lt;br /&gt;
With this new code, if you need to refer to the sentence &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;, you can do so by by typing in &amp;lt;code&amp;gt;result&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Conditional Statement===&lt;br /&gt;
Lets say you have the following code:&lt;br /&gt;
&lt;br /&gt;
 # This function prints every letter in the input&lt;br /&gt;
 def printLetters(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
   print letter&lt;br /&gt;
&lt;br /&gt;
What if your &#039;&#039;evil&#039;&#039; TA asks you to only print the vowel letters given in the input?&lt;br /&gt;
This means that you need to add a condition to your function where the &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; function only works if the letter is a vowel.&lt;br /&gt;
This can be done by using &#039;&#039;&#039;conditional statement&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Condtional statement starts with &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 def justvowels(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
    if letter in &amp;quot;aeiou&amp;quot;:&lt;br /&gt;
      print letter&lt;br /&gt;
&lt;br /&gt;
Instead of printing every letter in the string, this code will only print &#039;&#039;&#039;if&#039;&#039;&#039; the letter in question can be found in the list &amp;quot;a,e,i,o,u&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
Writing a function for a task instead of copy and pasting code makes your program much easier to understand and debug. &lt;br /&gt;
&lt;br /&gt;
For example, to write a function that takes in &lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example of using these common functions==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The best way to illustrate this is with an example.&lt;br /&gt;
&lt;br /&gt;
For this example, we will create a function to calculate the electric field created by a point charge.&lt;br /&gt;
&lt;br /&gt;
As a reminder, this is the formula we are modeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First, we ask what do we need to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
&lt;br /&gt;
 # Calculates the electric field at a given vector away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     E = electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
It is also possible to skip defining the &amp;lt;code&amp;gt;electricConstant * q * r.norm() / r.mag2&amp;lt;/code&amp;gt; function and just return it.&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26172</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26172"/>
		<updated>2016-11-28T02:49:12Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Example of using these common functions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
This is a simple function that creates a madlib with the 4 variables that are given as inputs.&lt;br /&gt;
&lt;br /&gt;
For example, if Sally was inputed as the name, lunch as noun1, pterodactyl as noun2, and flew as the verb, the the function will return the phrase &#039;&#039;Sally was looking at his lunch when a pterodactyl flew nearby&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; madlib(&amp;quot;Sally&amp;quot;, &amp;quot;lunch&amp;quot;, &amp;quot;pterodactyl&amp;quot;, &amp;quot;flew&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Sally was looking at his lunch when a pterodactyl flew nearby.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Returning the output given by a function===&lt;br /&gt;
&lt;br /&gt;
When a function takes an input and gives an output such as &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; in the madlib example, you cannot refer back to the output phrase &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;&lt;br /&gt;
If you would like to use this phrase again, then a function called &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is needed.&lt;br /&gt;
When &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is added at the end of a function, the output is passed back to the caller, which means that you can refer to the output now.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
   return result&lt;br /&gt;
&lt;br /&gt;
With this new code, if you need to refer to the sentence &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;, you can do so by by typing in &amp;lt;code&amp;gt;result&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Conditional Statement===&lt;br /&gt;
Lets say you have the following code:&lt;br /&gt;
&lt;br /&gt;
 # This function prints every letter in the input&lt;br /&gt;
 def printLetters(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
   print letter&lt;br /&gt;
&lt;br /&gt;
What if your &#039;&#039;evil&#039;&#039; TA asks you to only print the vowel letters given in the input?&lt;br /&gt;
This means that you need to add a condition to your function where the &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; function only works if the letter is a vowel.&lt;br /&gt;
This can be done by using &#039;&#039;&#039;conditional statement&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Condtional statement starts with &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 def justvowels(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
    if letter in &amp;quot;aeiou&amp;quot;:&lt;br /&gt;
      print letter&lt;br /&gt;
&lt;br /&gt;
Instead of printing every letter in the string, this code will only print &#039;&#039;&#039;if&#039;&#039;&#039; the letter in question can be found in the list &amp;quot;a,e,i,o,u&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
Writing a function for a task instead of copy and pasting code makes your program much easier to understand and debug. &lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example of using these common functions==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The best way to illustrate this is with an example.&lt;br /&gt;
&lt;br /&gt;
For this example, we will create a function to calculate the electric field created by a point charge.&lt;br /&gt;
&lt;br /&gt;
As a reminder, this is the formula we are modeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First, we ask what do we need to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
&lt;br /&gt;
 # Calculates the electric field at a given vector away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     E = electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
It is also possible to skip defining the &amp;lt;code&amp;gt;electricConstant * q * r.norm() / r.mag2&amp;lt;/code&amp;gt; function and just return it.&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26165</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26165"/>
		<updated>2016-11-28T02:48:40Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Writing your own Functions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
This is a simple function that creates a madlib with the 4 variables that are given as inputs.&lt;br /&gt;
&lt;br /&gt;
For example, if Sally was inputed as the name, lunch as noun1, pterodactyl as noun2, and flew as the verb, the the function will return the phrase &#039;&#039;Sally was looking at his lunch when a pterodactyl flew nearby&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; madlib(&amp;quot;Sally&amp;quot;, &amp;quot;lunch&amp;quot;, &amp;quot;pterodactyl&amp;quot;, &amp;quot;flew&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Sally was looking at his lunch when a pterodactyl flew nearby.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Returning the output given by a function===&lt;br /&gt;
&lt;br /&gt;
When a function takes an input and gives an output such as &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; in the madlib example, you cannot refer back to the output phrase &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;&lt;br /&gt;
If you would like to use this phrase again, then a function called &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is needed.&lt;br /&gt;
When &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is added at the end of a function, the output is passed back to the caller, which means that you can refer to the output now.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
   return result&lt;br /&gt;
&lt;br /&gt;
With this new code, if you need to refer to the sentence &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;, you can do so by by typing in &amp;lt;code&amp;gt;result&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Conditional Statement===&lt;br /&gt;
Lets say you have the following code:&lt;br /&gt;
&lt;br /&gt;
 # This function prints every letter in the input&lt;br /&gt;
 def printLetters(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
   print letter&lt;br /&gt;
&lt;br /&gt;
What if your &#039;&#039;evil&#039;&#039; TA asks you to only print the vowel letters given in the input?&lt;br /&gt;
This means that you need to add a condition to your function where the &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; function only works if the letter is a vowel.&lt;br /&gt;
This can be done by using &#039;&#039;&#039;conditional statement&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Condtional statement starts with &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 def justvowels(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
    if letter in &amp;quot;aeiou&amp;quot;:&lt;br /&gt;
      print letter&lt;br /&gt;
&lt;br /&gt;
Instead of printing every letter in the string, this code will only print &#039;&#039;&#039;if&#039;&#039;&#039; the letter in question can be found in the list &amp;quot;a,e,i,o,u&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
Writing a function for a task instead of copy and pasting code makes your program much easier to understand and debug. &lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example of using these common functions==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26152</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26152"/>
		<updated>2016-11-28T02:47:13Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Example of using these common functions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
This is a simple function that creates a madlib with the 4 variables that are given as inputs.&lt;br /&gt;
&lt;br /&gt;
For example, if Sally was inputed as the name, lunch as noun1, pterodactyl as noun2, and flew as the verb, the the function will return the phrase &#039;&#039;Sally was looking at his lunch when a pterodactyl flew nearby&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; madlib(&amp;quot;Sally&amp;quot;, &amp;quot;lunch&amp;quot;, &amp;quot;pterodactyl&amp;quot;, &amp;quot;flew&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Sally was looking at his lunch when a pterodactyl flew nearby.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Returning the output given by a function===&lt;br /&gt;
&lt;br /&gt;
When a function takes an input and gives an output such as &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; in the madlib example, you cannot refer back to the output phrase &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;&lt;br /&gt;
If you would like to use this phrase again, then a function called &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is needed.&lt;br /&gt;
When &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is added at the end of a function, the output is passed back to the caller, which means that you can refer to the output now.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
   return result&lt;br /&gt;
&lt;br /&gt;
With this new code, if you need to refer to the sentence &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;, you can do so by by typing in &amp;lt;code&amp;gt;result&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Conditional Statement===&lt;br /&gt;
Lets say you have the following code:&lt;br /&gt;
&lt;br /&gt;
 # This function prints every letter in the input&lt;br /&gt;
 def printLetters(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
   print letter&lt;br /&gt;
&lt;br /&gt;
What if your &#039;&#039;evil&#039;&#039; TA asks you to only print the vowel letters given in the input?&lt;br /&gt;
This means that you need to add a condition to your function where the &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; function only works if the letter is a vowel.&lt;br /&gt;
This can be done by using &#039;&#039;&#039;conditional statement&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Condtional statement starts with &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 def justvowels(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
    if letter in &amp;quot;aeiou&amp;quot;:&lt;br /&gt;
      print letter&lt;br /&gt;
&lt;br /&gt;
Instead of printing every letter in the string, this code will only print &#039;&#039;&#039;if&#039;&#039;&#039; the letter in question can be found in the list &amp;quot;a,e,i,o,u&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
For this example, we will create a function to calculate the electric field created by a point charge.&lt;br /&gt;
&lt;br /&gt;
As a reminder, this is the formula we are modeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First, we ask what do we need to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
&lt;br /&gt;
 # Calculates the electric field at a given vector away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     E = electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
It is also possible to skip defining the &amp;lt;code&amp;gt;electricConstant * q * r.norm() / r.mag2&amp;lt;/code&amp;gt; function and just return it.&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example of using these common functions==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26148</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26148"/>
		<updated>2016-11-28T02:46:24Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Frequently Used Functions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
This is a simple function that creates a madlib with the 4 variables that are given as inputs.&lt;br /&gt;
&lt;br /&gt;
For example, if Sally was inputed as the name, lunch as noun1, pterodactyl as noun2, and flew as the verb, the the function will return the phrase &#039;&#039;Sally was looking at his lunch when a pterodactyl flew nearby&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; madlib(&amp;quot;Sally&amp;quot;, &amp;quot;lunch&amp;quot;, &amp;quot;pterodactyl&amp;quot;, &amp;quot;flew&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Sally was looking at his lunch when a pterodactyl flew nearby.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Returning the output given by a function===&lt;br /&gt;
&lt;br /&gt;
When a function takes an input and gives an output such as &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; in the madlib example, you cannot refer back to the output phrase &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;&lt;br /&gt;
If you would like to use this phrase again, then a function called &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is needed.&lt;br /&gt;
When &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is added at the end of a function, the output is passed back to the caller, which means that you can refer to the output now.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
   return result&lt;br /&gt;
&lt;br /&gt;
With this new code, if you need to refer to the sentence &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;, you can do so by by typing in &amp;lt;code&amp;gt;result&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Conditional Statement===&lt;br /&gt;
Lets say you have the following code:&lt;br /&gt;
&lt;br /&gt;
 # This function prints every letter in the input&lt;br /&gt;
 def printLetters(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
   print letter&lt;br /&gt;
&lt;br /&gt;
What if your &#039;&#039;evil&#039;&#039; TA asks you to only print the vowel letters given in the input?&lt;br /&gt;
This means that you need to add a condition to your function where the &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; function only works if the letter is a vowel.&lt;br /&gt;
This can be done by using &#039;&#039;&#039;conditional statement&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Condtional statement starts with &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 def justvowels(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
    if letter in &amp;quot;aeiou&amp;quot;:&lt;br /&gt;
      print letter&lt;br /&gt;
&lt;br /&gt;
Instead of printing every letter in the string, this code will only print &#039;&#039;&#039;if&#039;&#039;&#039; the letter in question can be found in the list &amp;quot;a,e,i,o,u&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
For this example, we will create a function to calculate the electric field created by a point charge.&lt;br /&gt;
&lt;br /&gt;
As a reminder, this is the formula we are modeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First, we ask what do we need to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
&lt;br /&gt;
 # Calculates the electric field at a given vector away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     E = electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
It is also possible to skip defining the &amp;lt;code&amp;gt;electricConstant * q * r.norm() / r.mag2&amp;lt;/code&amp;gt; function and just return it.&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example of using these common functions==&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26003</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=26003"/>
		<updated>2016-11-28T02:10:21Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Writing your own Functions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
This is a simple function that creates a madlib with the 4 variables that are given as inputs.&lt;br /&gt;
&lt;br /&gt;
For example, if Sally was inputed as the name, lunch as noun1, pterodactyl as noun2, and flew as the verb, the the function will return the phrase &#039;&#039;Sally was looking at his lunch when a pterodactyl flew nearby&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; madlib(&amp;quot;Sally&amp;quot;, &amp;quot;lunch&amp;quot;, &amp;quot;pterodactyl&amp;quot;, &amp;quot;flew&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Sally was looking at his lunch when a pterodactyl flew nearby.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Returning the output given by a function===&lt;br /&gt;
&lt;br /&gt;
When a function takes an input and gives an output such as &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; in the madlib example, you cannot refer back to the output phrase &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;&lt;br /&gt;
If you would like to use this phrase again, then a function called &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is needed.&lt;br /&gt;
When &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is added at the end of a function, the output is passed back to the caller, which means that you can refer to the output now.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
   return result&lt;br /&gt;
&lt;br /&gt;
With this new code, if you need to refer to the sentence &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;, you can do so by by typing in &amp;lt;code&amp;gt;result&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Conditional Statement===&lt;br /&gt;
Lets say you have the following code:&lt;br /&gt;
&lt;br /&gt;
 # This function prints every letter in the input&lt;br /&gt;
 def printLetters(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
   print letter&lt;br /&gt;
&lt;br /&gt;
What if your &#039;&#039;evil&#039;&#039; TA asks you to only print the vowel letters given in the input?&lt;br /&gt;
This means that you need to add a condition to your function where the &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; function only works if the letter is a vowel.&lt;br /&gt;
This can be done by using &#039;&#039;&#039;conditional statement&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Condtional statement starts with &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 def justvowels(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
    if letter in &amp;quot;aeiou&amp;quot;:&lt;br /&gt;
      print letter&lt;br /&gt;
&lt;br /&gt;
Instead of printing every letter in the string, this code will only print &#039;&#039;&#039;if&#039;&#039;&#039; the letter in question can be found in the list &amp;quot;a,e,i,o,u&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
For this example, we will create a function to calculate the electric field created by a point charge.&lt;br /&gt;
&lt;br /&gt;
As a reminder, this is the formula we are modeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First, we ask what do we need to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
&lt;br /&gt;
 # Calculates the electric field at a given vector away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     E = electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
     return E&lt;br /&gt;
&lt;br /&gt;
It is also possible to skip defining the &amp;lt;code&amp;gt;electricConstant * q * r.norm() / r.mag2&amp;lt;/code&amp;gt; function and just return it.&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25986</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25986"/>
		<updated>2016-11-28T02:05:19Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Example functions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
This is a simple function that creates a madlib with the 4 variables that are given as inputs.&lt;br /&gt;
&lt;br /&gt;
For example, if Sally was inputed as the name, lunch as noun1, pterodactyl as noun2, and flew as the verb, the the function will return the phrase &#039;&#039;Sally was looking at his lunch when a pterodactyl flew nearby&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; madlib(&amp;quot;Sally&amp;quot;, &amp;quot;lunch&amp;quot;, &amp;quot;pterodactyl&amp;quot;, &amp;quot;flew&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Sally was looking at his lunch when a pterodactyl flew nearby.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Returning the output given by a function===&lt;br /&gt;
&lt;br /&gt;
When a function takes an input and gives an output such as &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; in the madlib example, you cannot refer back to the output phrase &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;&lt;br /&gt;
If you would like to use this phrase again, then a function called &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is needed.&lt;br /&gt;
When &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is added at the end of a function, the output is passed back to the caller, which means that you can refer to the output now.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
   return result&lt;br /&gt;
&lt;br /&gt;
With this new code, if you need to refer to the sentence &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;, you can do so by by typing in &amp;lt;code&amp;gt;result&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Conditional Statement===&lt;br /&gt;
Lets say you have the following code:&lt;br /&gt;
&lt;br /&gt;
 # This function prints every letter in the input&lt;br /&gt;
 def printLetters(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
   print letter&lt;br /&gt;
&lt;br /&gt;
What if your &#039;&#039;evil&#039;&#039; TA asks you to only print the vowel letters given in the input?&lt;br /&gt;
This means that you need to add a condition to your function where the &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; function only works if the letter is a vowel.&lt;br /&gt;
This can be done by using &#039;&#039;&#039;conditional statement&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Condtional statement starts with &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 def justvowels(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
    if letter in &amp;quot;aeiou&amp;quot;:&lt;br /&gt;
      print letter&lt;br /&gt;
&lt;br /&gt;
Instead of printing every letter in the string, this code will only print &#039;&#039;&#039;if&#039;&#039;&#039; the letter in question can be found in the list &amp;quot;a,e,i,o,u&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
For this example, we will create a function to calculate the electric field created by a point charge.&lt;br /&gt;
&lt;br /&gt;
As a reminder, this is the formula we are modeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First, we ask what do we need to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
&lt;br /&gt;
 # Calculates the electric field at a given vector away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9 # 1/(4 * π * e0)&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25967</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25967"/>
		<updated>2016-11-28T02:00:42Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Example functions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
This is a simple function that creates a madlib with the 4 variables that are given as inputs.&lt;br /&gt;
&lt;br /&gt;
For example, if Sally was inputed as the name, lunch as noun1, pterodactyl as noun2, and flew as the verb, the the function will return the phrase &#039;&#039;Sally was looking at his lunch when a pterodactyl flew nearby&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; madlib(&amp;quot;Sally&amp;quot;, &amp;quot;lunch&amp;quot;, &amp;quot;pterodactyl&amp;quot;, &amp;quot;flew&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Sally was looking at his lunch when a pterodactyl flew nearby.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Returning the output given by a function===&lt;br /&gt;
&lt;br /&gt;
When a function takes an input and gives an output such as &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; in the madlib example, you cannot refer back to the output phrase &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;&lt;br /&gt;
If you would like to use this phrase again, then a function called &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is needed.&lt;br /&gt;
When &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is added at the end of a function, the output is passed back to the caller, which means that you can refer to the output now.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
   return result&lt;br /&gt;
&lt;br /&gt;
With this new code, if you need to refer to the sentence &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;, you can do so by by typing in &amp;lt;code&amp;gt;result&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Example functions===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
 # This function prints every letter in the input&lt;br /&gt;
 def printLetters(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
   print letter&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
For this example, we will create a function to calculate the electric field created by a point charge.&lt;br /&gt;
&lt;br /&gt;
As a reminder, this is the formula we are modeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First, we ask what do we need to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
&lt;br /&gt;
 # Calculates the electric field at a given vector away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9 # 1/(4 * π * e0)&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25964</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25964"/>
		<updated>2016-11-28T02:00:27Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Example function */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
This is a simple function that creates a madlib with the 4 variables that are given as inputs.&lt;br /&gt;
&lt;br /&gt;
For example, if Sally was inputed as the name, lunch as noun1, pterodactyl as noun2, and flew as the verb, the the function will return the phrase &#039;&#039;Sally was looking at his lunch when a pterodactyl flew nearby&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; madlib(&amp;quot;Sally&amp;quot;, &amp;quot;lunch&amp;quot;, &amp;quot;pterodactyl&amp;quot;, &amp;quot;flew&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Sally was looking at his lunch when a pterodactyl flew nearby.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Returning the output given by a function===&lt;br /&gt;
&lt;br /&gt;
When a function takes an input and gives an output such as &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; in the madlib example, you cannot refer back to the output phrase &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;&lt;br /&gt;
If you would like to use this phrase again, then a function called &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is needed.&lt;br /&gt;
When &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is added at the end of a function, the output is passed back to the caller, which means that you can refer to the output now.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
   return result&lt;br /&gt;
&lt;br /&gt;
With this new code, if you need to refer to the sentence &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;, you can do so by by typing in &amp;lt;code&amp;gt;result&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Example functions===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
# This function prints every letter in the input&lt;br /&gt;
 def printLetters(string):&lt;br /&gt;
  for letter in string:&lt;br /&gt;
   print letter&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
For this example, we will create a function to calculate the electric field created by a point charge.&lt;br /&gt;
&lt;br /&gt;
As a reminder, this is the formula we are modeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First, we ask what do we need to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
&lt;br /&gt;
 # Calculates the electric field at a given vector away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9 # 1/(4 * π * e0)&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25949</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25949"/>
		<updated>2016-11-28T01:58:13Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Example use of a function */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
This is a simple function that creates a madlib with the 4 variables that are given as inputs.&lt;br /&gt;
&lt;br /&gt;
For example, if Sally was inputed as the name, lunch as noun1, pterodactyl as noun2, and flew as the verb, the the function will return the phrase &#039;&#039;Sally was looking at his lunch when a pterodactyl flew nearby&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; madlib(&amp;quot;Sally&amp;quot;, &amp;quot;lunch&amp;quot;, &amp;quot;pterodactyl&amp;quot;, &amp;quot;flew&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Sally was looking at his lunch when a pterodactyl flew nearby.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Returning the output given by a function===&lt;br /&gt;
&lt;br /&gt;
When a function takes an input and gives an output such as &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; in the madlib example, you cannot refer back to the output phrase &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;&lt;br /&gt;
If you would like to use this phrase again, then a function called &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is needed.&lt;br /&gt;
When &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is added at the end of a function, the output is passed back to the caller, which means that you can refer to the output now.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
   return result&lt;br /&gt;
&lt;br /&gt;
With this new code, if you need to refer to the sentence &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;, you can do so by by typing in &amp;lt;code&amp;gt;result&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 # Calls the function addNumbers with the arguments 1, 2 and 4&lt;br /&gt;
 result = addNumbers(1, 2, 4)&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
For this example, we will create a function to calculate the electric field created by a point charge.&lt;br /&gt;
&lt;br /&gt;
As a reminder, this is the formula we are modeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First, we ask what do we need to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
&lt;br /&gt;
 # Calculates the electric field at a given vector away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9 # 1/(4 * π * e0)&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25947</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25947"/>
		<updated>2016-11-28T01:57:53Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Returning the output given by a function */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
This is a simple function that creates a madlib with the 4 variables that are given as inputs.&lt;br /&gt;
&lt;br /&gt;
For example, if Sally was inputed as the name, lunch as noun1, pterodactyl as noun2, and flew as the verb, the the function will return the phrase &#039;&#039;Sally was looking at his lunch when a pterodactyl flew nearby&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; madlib(&amp;quot;Sally&amp;quot;, &amp;quot;lunch&amp;quot;, &amp;quot;pterodactyl&amp;quot;, &amp;quot;flew&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Sally was looking at his lunch when a pterodactyl flew nearby.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Returning the output given by a function===&lt;br /&gt;
&lt;br /&gt;
When a function takes an input and gives an output such as &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; in the madlib example, you cannot refer back to the output phrase &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;&lt;br /&gt;
If you would like to use this phrase again, then a function called &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is needed.&lt;br /&gt;
When &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is added at the end of a function, the output is passed back to the caller, which means that you can refer to the output now.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
   return result&lt;br /&gt;
&lt;br /&gt;
With this new code, if you need to refer to the sentence &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;, you can do so by by typing in &amp;lt;code&amp;gt;result&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Example use of a function===&lt;br /&gt;
 # Calls the function addNumbers with the arguments 1, 2 and 4&lt;br /&gt;
 result = addNumbers(1, 2, 4)&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
For this example, we will create a function to calculate the electric field created by a point charge.&lt;br /&gt;
&lt;br /&gt;
As a reminder, this is the formula we are modeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First, we ask what do we need to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
&lt;br /&gt;
 # Calculates the electric field at a given vector away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9 # 1/(4 * π * e0)&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25942</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25942"/>
		<updated>2016-11-28T01:56:40Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Returning the output given by a function */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
This is a simple function that creates a madlib with the 4 variables that are given as inputs.&lt;br /&gt;
&lt;br /&gt;
For example, if Sally was inputed as the name, lunch as noun1, pterodactyl as noun2, and flew as the verb, the the function will return the phrase &#039;&#039;Sally was looking at his lunch when a pterodactyl flew nearby&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; madlib(&amp;quot;Sally&amp;quot;, &amp;quot;lunch&amp;quot;, &amp;quot;pterodactyl&amp;quot;, &amp;quot;flew&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Sally was looking at his lunch when a pterodactyl flew nearby.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Returning the output given by a function===&lt;br /&gt;
&lt;br /&gt;
When a function takes an input and gives an output such as &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; in the madlib example, you cannot refer back to the output phrase &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;&lt;br /&gt;
If you would like to use this phrase again, then a function called &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is needed.&lt;br /&gt;
When &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is added at the end of a function, the output is passed back to the caller, which means that you can refer to the output now.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
   return result&lt;br /&gt;
&lt;br /&gt;
With this new code, you can bring up the sentence &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt; by typing in &amp;lt;code&amp;gt;result&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Example use of a function===&lt;br /&gt;
 # Calls the function addNumbers with the arguments 1, 2 and 4&lt;br /&gt;
 result = addNumbers(1, 2, 4)&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
For this example, we will create a function to calculate the electric field created by a point charge.&lt;br /&gt;
&lt;br /&gt;
As a reminder, this is the formula we are modeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First, we ask what do we need to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
&lt;br /&gt;
 # Calculates the electric field at a given vector away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9 # 1/(4 * π * e0)&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25937</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25937"/>
		<updated>2016-11-28T01:55:47Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Basic of Functions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
This is a simple function that creates a madlib with the 4 variables that are given as inputs.&lt;br /&gt;
&lt;br /&gt;
For example, if Sally was inputed as the name, lunch as noun1, pterodactyl as noun2, and flew as the verb, the the function will return the phrase &#039;&#039;Sally was looking at his lunch when a pterodactyl flew nearby&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; madlib(&amp;quot;Sally&amp;quot;, &amp;quot;lunch&amp;quot;, &amp;quot;pterodactyl&amp;quot;, &amp;quot;flew&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Sally was looking at his lunch when a pterodactyl flew nearby.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Returning the output given by a function===&lt;br /&gt;
&lt;br /&gt;
When a function takes an input and gives an output such as &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; in the madlib example, you cannot refer back to the output phrase &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;&lt;br /&gt;
If you would like to use this phrase again, then a function called &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is needed.&lt;br /&gt;
When &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is added at the end of a function, the output is passed back to the caller, which means that you can refer to the output now.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
   return result&lt;br /&gt;
&lt;br /&gt;
With this new code, you can bring up the phrase &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby&amp;lt;/code&amp;gt; by typing in &amp;lt;code&amp;gt;result&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Example use of a function===&lt;br /&gt;
 # Calls the function addNumbers with the arguments 1, 2 and 4&lt;br /&gt;
 result = addNumbers(1, 2, 4)&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
For this example, we will create a function to calculate the electric field created by a point charge.&lt;br /&gt;
&lt;br /&gt;
As a reminder, this is the formula we are modeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First, we ask what do we need to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
&lt;br /&gt;
 # Calculates the electric field at a given vector away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9 # 1/(4 * π * e0)&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25925</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25925"/>
		<updated>2016-11-28T01:53:08Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Returning the output given by a function */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
This is a simple function that creates a madlib with the 4 variables that are given as inputs.&lt;br /&gt;
&lt;br /&gt;
For example, if Sally was inputed as the name, lunch as noun1, pterodactyl as noun2, and flew as the verb, the the function will return the phrase &#039;&#039;Sally was looking at his lunch when a pterodactyl flew nearby&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; madlib(&amp;quot;Sally&amp;quot;, &amp;quot;lunch&amp;quot;, &amp;quot;pterodactyl&amp;quot;, &amp;quot;flew&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Sally was looking at his lunch when a pterodactyl flew nearby.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Returning the output given by a function===&lt;br /&gt;
&lt;br /&gt;
When a function takes an input and gives an output such as &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; in the madlib example, you cannot refer back to the output phrase &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;&lt;br /&gt;
If you would like to use this phrase again, then a function called &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is needed.&lt;br /&gt;
When &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is added at the end of a function, the output is passed back to the caller, which means that you can refer to the output now.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 def functionName(argumentOne, argumentTwo):&lt;br /&gt;
     functionReturnValue = argumentOne + argumentTwo&lt;br /&gt;
     return functionReturnValue&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Example use of a function===&lt;br /&gt;
 # Calls the function addNumbers with the arguments 1, 2 and 4&lt;br /&gt;
 result = addNumbers(1, 2, 4)&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
For this example, we will create a function to calculate the electric field created by a point charge.&lt;br /&gt;
&lt;br /&gt;
As a reminder, this is the formula we are modeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First, we ask what do we need to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
&lt;br /&gt;
 # Calculates the electric field at a given vector away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9 # 1/(4 * π * e0)&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25922</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25922"/>
		<updated>2016-11-28T01:52:52Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Returning the output given by a function */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
This is a simple function that creates a madlib with the 4 variables that are given as inputs.&lt;br /&gt;
&lt;br /&gt;
For example, if Sally was inputed as the name, lunch as noun1, pterodactyl as noun2, and flew as the verb, the the function will return the phrase &#039;&#039;Sally was looking at his lunch when a pterodactyl flew nearby&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; madlib(&amp;quot;Sally&amp;quot;, &amp;quot;lunch&amp;quot;, &amp;quot;pterodactyl&amp;quot;, &amp;quot;flew&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Sally was looking at his lunch when a pterodactyl flew nearby.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Returning the output given by a function=&lt;br /&gt;
&lt;br /&gt;
When a function takes an input and gives an output such as &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; in the madlib example, you cannot refer back to the output phrase &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;&lt;br /&gt;
If you would like to use this phrase again, then a function called &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is needed.&lt;br /&gt;
When &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is added at the end of a function, the output is passed back to the caller, which means that you can refer to the output now.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 def functionName(argumentOne, argumentTwo):&lt;br /&gt;
     functionReturnValue = argumentOne + argumentTwo&lt;br /&gt;
     return functionReturnValue&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Example use of a function===&lt;br /&gt;
 # Calls the function addNumbers with the arguments 1, 2 and 4&lt;br /&gt;
 result = addNumbers(1, 2, 4)&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
For this example, we will create a function to calculate the electric field created by a point charge.&lt;br /&gt;
&lt;br /&gt;
As a reminder, this is the formula we are modeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First, we ask what do we need to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
&lt;br /&gt;
 # Calculates the electric field at a given vector away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9 # 1/(4 * π * e0)&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25918</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25918"/>
		<updated>2016-11-28T01:52:36Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Returning the output given by a function */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
This is a simple function that creates a madlib with the 4 variables that are given as inputs.&lt;br /&gt;
&lt;br /&gt;
For example, if Sally was inputed as the name, lunch as noun1, pterodactyl as noun2, and flew as the verb, the the function will return the phrase &#039;&#039;Sally was looking at his lunch when a pterodactyl flew nearby&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; madlib(&amp;quot;Sally&amp;quot;, &amp;quot;lunch&amp;quot;, &amp;quot;pterodactyl&amp;quot;, &amp;quot;flew&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Sally was looking at his lunch when a pterodactyl flew nearby.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Returning the output given by a function==&lt;br /&gt;
&lt;br /&gt;
When a function takes an input and gives an output such as &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; in the madlib example, you cannot refer back to the output phrase &amp;lt;code&amp;gt;Sally was looking at his lunch when a pterodactyl flew nearby.&amp;lt;/code&amp;gt;&lt;br /&gt;
If you would like to use this phrase again, then a function called &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is needed.&lt;br /&gt;
When &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is added at the end of a function, the output is passed back to the caller, which means that you can refer to the output now.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 def functionName(argumentOne, argumentTwo):&lt;br /&gt;
     functionReturnValue = argumentOne + argumentTwo&lt;br /&gt;
     return functionReturnValue&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Example use of a function===&lt;br /&gt;
 # Calls the function addNumbers with the arguments 1, 2 and 4&lt;br /&gt;
 result = addNumbers(1, 2, 4)&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
For this example, we will create a function to calculate the electric field created by a point charge.&lt;br /&gt;
&lt;br /&gt;
As a reminder, this is the formula we are modeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First, we ask what do we need to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
&lt;br /&gt;
 # Calculates the electric field at a given vector away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9 # 1/(4 * π * e0)&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25905</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25905"/>
		<updated>2016-11-28T01:48:11Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Basic Python function syntax */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
This is a simple function that creates a madlib with the 4 variables that are given as inputs.&lt;br /&gt;
&lt;br /&gt;
For example, if Sally was inputed as the name, lunch as noun1, pterodactyl as noun2, and flew as the verb, the the function will return the phrase &#039;&#039;Sally was looking at his lunch when a pterodactyl flew nearby&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; madlib(&amp;quot;Sally&amp;quot;, &amp;quot;lunch&amp;quot;, &amp;quot;pterodactyl&amp;quot;, &amp;quot;flew&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Sally was looking at his lunch when a pterodactyl flew nearby.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Returning the output given by a function==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; keyword can be used for a function to &amp;quot;give back&amp;quot; a result.&lt;br /&gt;
&lt;br /&gt;
 def functionName(argumentOne, argumentTwo):&lt;br /&gt;
     functionReturnValue = argumentOne + argumentTwo&lt;br /&gt;
     return functionReturnValue&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Example use of a function===&lt;br /&gt;
 # Calls the function addNumbers with the arguments 1, 2 and 4&lt;br /&gt;
 result = addNumbers(1, 2, 4)&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
For this example, we will create a function to calculate the electric field created by a point charge.&lt;br /&gt;
&lt;br /&gt;
As a reminder, this is the formula we are modeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First, we ask what do we need to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
&lt;br /&gt;
 # Calculates the electric field at a given vector away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9 # 1/(4 * π * e0)&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25902</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25902"/>
		<updated>2016-11-28T01:46:49Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Basic Python function syntax */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
This is a simple function that creates a madlib with the 4 variables that are given as inputs.&lt;br /&gt;
&lt;br /&gt;
For example, if Sally was inputed as the name, lunch as noun1, pterodactyl as noun2, and flew as the verb, the the function will return the phrase &#039;&#039;Sally was looking at his lunch when a pterodactyl flew nearby&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; madlib(&amp;quot;Sally&amp;quot;, &amp;quot;lunch&amp;quot;, &amp;quot;pterodactyl&amp;quot;, &amp;quot;flew&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Sally was looking at his lunch when a pterodactyl flew nearby.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; keyword can be used for a function to &amp;quot;give back&amp;quot; a result.&lt;br /&gt;
&lt;br /&gt;
 def functionName(argumentOne, argumentTwo):&lt;br /&gt;
     functionReturnValue = argumentOne + argumentTwo&lt;br /&gt;
     return functionReturnValue&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Example use of a function===&lt;br /&gt;
 # Calls the function addNumbers with the arguments 1, 2 and 4&lt;br /&gt;
 result = addNumbers(1, 2, 4)&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
For this example, we will create a function to calculate the electric field created by a point charge.&lt;br /&gt;
&lt;br /&gt;
As a reminder, this is the formula we are modeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First, we ask what do we need to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
&lt;br /&gt;
 # Calculates the electric field at a given vector away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9 # 1/(4 * π * e0)&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25900</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25900"/>
		<updated>2016-11-28T01:46:35Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Basic Python function syntax */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
This is a simple function that creates a madlib with the 4 variables that are given as inputs.&lt;br /&gt;
&lt;br /&gt;
For example, if Sally was inputed as the name, lunch as noun1, pterodactyl as noun2, and flew as the verb, the the function will return the phrase &#039;&#039;Sally was looking at his lunch when a pterodactyl flew nearby&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; madlib(&amp;quot;Sally&amp;quot;, &amp;quot;lunch&amp;quot;, &amp;quot;pterodactyl&amp;quot;, &amp;quot;flew&amp;quot;)&lt;br /&gt;
Sally was looking at his lunch when a pterodactyl flew nearby.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; keyword can be used for a function to &amp;quot;give back&amp;quot; a result.&lt;br /&gt;
&lt;br /&gt;
 def functionName(argumentOne, argumentTwo):&lt;br /&gt;
     functionReturnValue = argumentOne + argumentTwo&lt;br /&gt;
     return functionReturnValue&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Example use of a function===&lt;br /&gt;
 # Calls the function addNumbers with the arguments 1, 2 and 4&lt;br /&gt;
 result = addNumbers(1, 2, 4)&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
For this example, we will create a function to calculate the electric field created by a point charge.&lt;br /&gt;
&lt;br /&gt;
As a reminder, this is the formula we are modeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First, we ask what do we need to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
&lt;br /&gt;
 # Calculates the electric field at a given vector away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9 # 1/(4 * π * e0)&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25898</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25898"/>
		<updated>2016-11-28T01:46:26Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Basic Python function syntax */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
This is a simple function that creates a madlib with the 4 variables that are given as inputs.&lt;br /&gt;
&lt;br /&gt;
For example, if Sally was inputed as the name, lunch as noun1, pterodactyl as noun2, and flew as the verb, the the function will return the phrase &#039;&#039;Sally was looking at his lunch when a pterodactyl flew nearby&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; madlib(&amp;quot;Sally&amp;quot;, &amp;quot;lunch&amp;quot;, &amp;quot;pterodactyl&amp;quot;, &amp;quot;flew&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Sally was looking at his lunch when a pterodactyl flew nearby.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; keyword can be used for a function to &amp;quot;give back&amp;quot; a result.&lt;br /&gt;
&lt;br /&gt;
 def functionName(argumentOne, argumentTwo):&lt;br /&gt;
     functionReturnValue = argumentOne + argumentTwo&lt;br /&gt;
     return functionReturnValue&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Example use of a function===&lt;br /&gt;
 # Calls the function addNumbers with the arguments 1, 2 and 4&lt;br /&gt;
 result = addNumbers(1, 2, 4)&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
For this example, we will create a function to calculate the electric field created by a point charge.&lt;br /&gt;
&lt;br /&gt;
As a reminder, this is the formula we are modeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First, we ask what do we need to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
&lt;br /&gt;
 # Calculates the electric field at a given vector away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9 # 1/(4 * π * e0)&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25895</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25895"/>
		<updated>2016-11-28T01:45:32Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Basic of Functions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
This is a simple function that creates a madlib with the 4 variables that are given as inputs.&lt;br /&gt;
&lt;br /&gt;
For example, if Sally was inputed as the name, lunch as noun1, pterodactyl as noun2, and flew as the verb, the the function will return the phrase &#039;&#039;Sally was looking at his lunch when a pterodactyl flew nearby&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; madlib(&amp;quot;Sally&amp;quot;, &amp;quot;lunch&amp;quot;, &amp;quot;pterodactyl&amp;quot;, &amp;quot;flew&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Sally was looking at his lunch when a pterodactyl flew nearby.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; keyword can be used for a function to &amp;quot;give back&amp;quot; a result.&lt;br /&gt;
&lt;br /&gt;
 def functionName(argumentOne, argumentTwo):&lt;br /&gt;
     functionReturnValue = argumentOne + argumentTwo&lt;br /&gt;
     return functionReturnValue&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Example use of a function===&lt;br /&gt;
 # Calls the function addNumbers with the arguments 1, 2 and 4&lt;br /&gt;
 result = addNumbers(1, 2, 4)&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
For this example, we will create a function to calculate the electric field created by a point charge.&lt;br /&gt;
&lt;br /&gt;
As a reminder, this is the formula we are modeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First, we ask what do we need to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
&lt;br /&gt;
 # Calculates the electric field at a given vector away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9 # 1/(4 * π * e0)&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25892</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25892"/>
		<updated>2016-11-28T01:45:06Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Loading Program */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
This is a simple function that creates a madlib with the 4 variables that are given as inputs.&lt;br /&gt;
&lt;br /&gt;
For example, if Sally was inputed as the name, lunch as noun1, pterodactyl as noun2, and flew as the verb, the the function will return the phrase &#039;&#039;Sally was looking at his lunch when a pterodactyl flew nearby&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; madlib(&amp;quot;Sally&amp;quot;, &amp;quot;lunch&amp;quot;, &amp;quot;pterodactyl&amp;quot;, &amp;quot;flew&amp;quot;)&lt;br /&gt;
Sally was looking at his lunch when a pterodactyl flew nearby.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; keyword can be used for a function to &amp;quot;give back&amp;quot; a result.&lt;br /&gt;
&lt;br /&gt;
 def functionName(argumentOne, argumentTwo):&lt;br /&gt;
     functionReturnValue = argumentOne + argumentTwo&lt;br /&gt;
     return functionReturnValue&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Example use of a function===&lt;br /&gt;
 # Calls the function addNumbers with the arguments 1, 2 and 4&lt;br /&gt;
 result = addNumbers(1, 2, 4)&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
For this example, we will create a function to calculate the electric field created by a point charge.&lt;br /&gt;
&lt;br /&gt;
As a reminder, this is the formula we are modeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First, we ask what do we need to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
&lt;br /&gt;
 # Calculates the electric field at a given vector away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9 # 1/(4 * π * e0)&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25889</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25889"/>
		<updated>2016-11-28T01:44:37Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Basic Python function syntax */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
This is a simple function that creates a madlib with the 4 variables that are given as inputs.&lt;br /&gt;
&lt;br /&gt;
For example, if Sally was inputed as the name, lunch as noun1, pterodactyl as noun2, and flew as the verb, the the function will return the phrase &#039;&#039;Sally was looking at his lunch when a pterodactyl flew nearby&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
======= Loading Program =======&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; madlib(&amp;quot;Sally&amp;quot;, &amp;quot;lunch&amp;quot;, &amp;quot;pterodactyl&amp;quot;, &amp;quot;flew&amp;quot;)&lt;br /&gt;
Sally was looking at his lunch when a pterodactyl flew nearby.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; keyword can be used for a function to &amp;quot;give back&amp;quot; a result.&lt;br /&gt;
&lt;br /&gt;
 def functionName(argumentOne, argumentTwo):&lt;br /&gt;
     functionReturnValue = argumentOne + argumentTwo&lt;br /&gt;
     return functionReturnValue&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Example use of a function===&lt;br /&gt;
 # Calls the function addNumbers with the arguments 1, 2 and 4&lt;br /&gt;
 result = addNumbers(1, 2, 4)&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
For this example, we will create a function to calculate the electric field created by a point charge.&lt;br /&gt;
&lt;br /&gt;
As a reminder, this is the formula we are modeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First, we ask what do we need to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
&lt;br /&gt;
 # Calculates the electric field at a given vector away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9 # 1/(4 * π * e0)&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25880</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25880"/>
		<updated>2016-11-28T01:41:27Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Summary */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; keyword can be used for a function to &amp;quot;give back&amp;quot; a result.&lt;br /&gt;
&lt;br /&gt;
 def functionName(argumentOne, argumentTwo):&lt;br /&gt;
     functionReturnValue = argumentOne + argumentTwo&lt;br /&gt;
     return functionReturnValue&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Example use of a function===&lt;br /&gt;
 # Calls the function addNumbers with the arguments 1, 2 and 4&lt;br /&gt;
 result = addNumbers(1, 2, 4)&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
For this example, we will create a function to calculate the electric field created by a point charge.&lt;br /&gt;
&lt;br /&gt;
As a reminder, this is the formula we are modeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First, we ask what do we need to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
&lt;br /&gt;
 # Calculates the electric field at a given vector away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9 # 1/(4 * π * e0)&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25875</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25875"/>
		<updated>2016-11-28T01:40:37Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Basic of Functions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
 def madlib(name, noun1, noun2, verb):&lt;br /&gt;
   result = name + &amp;quot; was looking at his &amp;quot; + noun1 + &amp;quot; when a &amp;quot; + noun2 + &amp;quot; &amp;quot; + verb + &amp;quot; nearby.&amp;quot;&lt;br /&gt;
   print result&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; keyword can be used for a function to &amp;quot;give back&amp;quot; a result.&lt;br /&gt;
&lt;br /&gt;
 def functionName(argumentOne, argumentTwo):&lt;br /&gt;
     functionReturnValue = argumentOne + argumentTwo&lt;br /&gt;
     return functionReturnValue&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Example use of a function===&lt;br /&gt;
 # Calls the function addNumbers with the arguments 1, 2 and 4&lt;br /&gt;
 result = addNumbers(1, 2, 4)&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
For this example, we will create a function to calculate the electric field created by a point charge.&lt;br /&gt;
&lt;br /&gt;
As a reminder, this is the formula we are modeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First, we ask what do we need to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
&lt;br /&gt;
 # Calculates the electric field at a given vector away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9 # 1/(4 * π * e0)&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25871</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25871"/>
		<updated>2016-11-28T01:39:07Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Summary */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Functions always start with def, which is short for define, used to define a word with a piece of code, not data.&lt;br /&gt;
	• Function is like a box of commands&lt;br /&gt;
	• Functions can take inputs&lt;br /&gt;
	• The way we pass those inputs is by putting them in parenthesis&lt;br /&gt;
Functions can return objects (outputs)&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
	• Note that python uses indent to consider what is under a command. If  print &amp;quot;Hello, world&amp;quot; is not indented, then it will not be a part of the function sayHello().&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; keyword can be used for a function to &amp;quot;give back&amp;quot; a result.&lt;br /&gt;
&lt;br /&gt;
 def functionName(argumentOne, argumentTwo):&lt;br /&gt;
     functionReturnValue = argumentOne + argumentTwo&lt;br /&gt;
     return functionReturnValue&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Example use of a function===&lt;br /&gt;
 # Calls the function addNumbers with the arguments 1, 2 and 4&lt;br /&gt;
 result = addNumbers(1, 2, 4)&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
For this example, we will create a function to calculate the electric field created by a point charge.&lt;br /&gt;
&lt;br /&gt;
As a reminder, this is the formula we are modeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First, we ask what do we need to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
&lt;br /&gt;
 # Calculates the electric field at a given vector away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9 # 1/(4 * π * e0)&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25851</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25851"/>
		<updated>2016-11-28T01:34:51Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Summary */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Whenever you type &amp;lt;code&amp;gt;print(5)&amp;lt;/code&amp;gt;, you are calling a function called &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; with an argument of &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt;. This page will describe the basics of functions.&lt;br /&gt;
&lt;br /&gt;
 def sayHello():&lt;br /&gt;
   print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; keyword can be used for a function to &amp;quot;give back&amp;quot; a result.&lt;br /&gt;
&lt;br /&gt;
 def functionName(argumentOne, argumentTwo):&lt;br /&gt;
     functionReturnValue = argumentOne + argumentTwo&lt;br /&gt;
     return functionReturnValue&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Example use of a function===&lt;br /&gt;
 # Calls the function addNumbers with the arguments 1, 2 and 4&lt;br /&gt;
 result = addNumbers(1, 2, 4)&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
For this example, we will create a function to calculate the electric field created by a point charge.&lt;br /&gt;
&lt;br /&gt;
As a reminder, this is the formula we are modeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First, we ask what do we need to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
&lt;br /&gt;
 # Calculates the electric field at a given vector away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9 # 1/(4 * π * e0)&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25849</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25849"/>
		<updated>2016-11-28T01:34:41Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Summary */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Whenever you type &amp;lt;code&amp;gt;print(5)&amp;lt;/code&amp;gt;, you are calling a function called &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; with an argument of &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt;. This page will describe the basics of functions.&lt;br /&gt;
&lt;br /&gt;
def sayHello():&lt;br /&gt;
  print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function that defines the variable &amp;quot;sayHello&amp;quot; to return the phrase &amp;quot;Hello, world&amp;quot; (note that the parenthesis will not be printed. Only the &#039;&#039;Hello, world&#039;&#039; portion will be printed).&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; keyword can be used for a function to &amp;quot;give back&amp;quot; a result.&lt;br /&gt;
&lt;br /&gt;
 def functionName(argumentOne, argumentTwo):&lt;br /&gt;
     functionReturnValue = argumentOne + argumentTwo&lt;br /&gt;
     return functionReturnValue&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Example use of a function===&lt;br /&gt;
 # Calls the function addNumbers with the arguments 1, 2 and 4&lt;br /&gt;
 result = addNumbers(1, 2, 4)&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
For this example, we will create a function to calculate the electric field created by a point charge.&lt;br /&gt;
&lt;br /&gt;
As a reminder, this is the formula we are modeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First, we ask what do we need to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
&lt;br /&gt;
 # Calculates the electric field at a given vector away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9 # 1/(4 * π * e0)&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25835</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25835"/>
		<updated>2016-11-28T01:30:06Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Summary */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Whenever you type &amp;lt;code&amp;gt;print(5)&amp;lt;/code&amp;gt;, you are calling a function called &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; with an argument of &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt;. This page will describe the basics of functions.&lt;br /&gt;
&lt;br /&gt;
  def sayHello():&lt;br /&gt;
    print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function where&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; keyword can be used for a function to &amp;quot;give back&amp;quot; a result.&lt;br /&gt;
&lt;br /&gt;
 def functionName(argumentOne, argumentTwo):&lt;br /&gt;
     functionReturnValue = argumentOne + argumentTwo&lt;br /&gt;
     return functionReturnValue&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Example use of a function===&lt;br /&gt;
 # Calls the function addNumbers with the arguments 1, 2 and 4&lt;br /&gt;
 result = addNumbers(1, 2, 4)&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
For this example, we will create a function to calculate the electric field created by a point charge.&lt;br /&gt;
&lt;br /&gt;
As a reminder, this is the formula we are modeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First, we ask what do we need to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
&lt;br /&gt;
 # Calculates the electric field at a given vector away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9 # 1/(4 * π * e0)&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25827</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25827"/>
		<updated>2016-11-28T01:28:42Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Summary */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Whenever you type &amp;lt;code&amp;gt;print(5)&amp;lt;/code&amp;gt;, you are calling a function called &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; with an argument of &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt;. This page will describe the basics of functions.&lt;br /&gt;
&lt;br /&gt;
def sayHello():&lt;br /&gt;
  print &amp;quot;Hello, world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This is a simple function where&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; keyword can be used for a function to &amp;quot;give back&amp;quot; a result.&lt;br /&gt;
&lt;br /&gt;
 def functionName(argumentOne, argumentTwo):&lt;br /&gt;
     functionReturnValue = argumentOne + argumentTwo&lt;br /&gt;
     return functionReturnValue&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Example use of a function===&lt;br /&gt;
 # Calls the function addNumbers with the arguments 1, 2 and 4&lt;br /&gt;
 result = addNumbers(1, 2, 4)&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
For this example, we will create a function to calculate the electric field created by a point charge.&lt;br /&gt;
&lt;br /&gt;
As a reminder, this is the formula we are modeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First, we ask what do we need to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
&lt;br /&gt;
 # Calculates the electric field at a given vector away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9 # 1/(4 * π * e0)&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25809</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=25809"/>
		<updated>2016-11-28T01:24:12Z</updated>

		<summary type="html">&lt;p&gt;Dkim469: /* Basic Python function syntax */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Kevin Randrup&lt;br /&gt;
&lt;br /&gt;
Claimed Chloe Choi&lt;br /&gt;
&lt;br /&gt;
Edited by Do Young Kim (Fall 2016)&lt;br /&gt;
&lt;br /&gt;
Introduction to functions in Python and applying them to write shorter and more understandable code.&lt;br /&gt;
Assumes that VPython is [http://www.physicsbook.gatech.edu/VPython installed] and you understand the [http://www.physicsbook.gatech.edu/VPython_basics basics] of programming in Python.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also &amp;quot;arguments&amp;quot; or &amp;quot;parameters&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
Whenever you type &amp;lt;code&amp;gt;print(5)&amp;lt;/code&amp;gt;, you are calling a function called &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; with an argument of &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt;. This page will describe the basics of functions and how to &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Basic of Functions==&lt;br /&gt;
&lt;br /&gt;
===Basic Python function syntax===&lt;br /&gt;
Functions can be defined with the keyword &amp;quot;def&amp;quot; followed by the function name and a colon and a parenthesis.&lt;br /&gt;
The variable that should be processed by the function goes Inside the parenthesis.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; keyword can be used for a function to &amp;quot;give back&amp;quot; a result.&lt;br /&gt;
&lt;br /&gt;
 def functionName(argumentOne, argumentTwo):&lt;br /&gt;
     functionReturnValue = argumentOne + argumentTwo&lt;br /&gt;
     return functionReturnValue&lt;br /&gt;
&lt;br /&gt;
===Example function===&lt;br /&gt;
&lt;br /&gt;
 # This function adds three numbers together and gives back the result using the return keyword&lt;br /&gt;
 def addNumbers(a, b, c):&lt;br /&gt;
     return a + b + c&lt;br /&gt;
&lt;br /&gt;
===Example use of a function===&lt;br /&gt;
 # Calls the function addNumbers with the arguments 1, 2 and 4&lt;br /&gt;
 result = addNumbers(1, 2, 4)&lt;br /&gt;
&lt;br /&gt;
==Writing your own Functions==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
For this example, we will create a function to calculate the electric field created by a point charge.&lt;br /&gt;
&lt;br /&gt;
As a reminder, this is the formula we are modeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First, we ask what do we need to calculate the electric field?&lt;br /&gt;
* &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; - the charge of the source&lt;br /&gt;
* &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; - the distance from the source to the electric field&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\overrightarrow{r}&amp;lt;/math&amp;gt; will be the two arguments to our function which will look like this:&lt;br /&gt;
&lt;br /&gt;
 # Calculates the electric field at a given vector away from the charge&lt;br /&gt;
 def electricField(q, r):&lt;br /&gt;
     electricConstant = 9e9 # 1/(4 * π * e0)&lt;br /&gt;
     return electricConstant * q * r.norm() / r.mag2&lt;br /&gt;
&lt;br /&gt;
===Using the function===&lt;br /&gt;
&lt;br /&gt;
We can apply functions to make our code shorter and easier to understand. Here is some example code that we will shorten with functions.&lt;br /&gt;
&lt;br /&gt;
 # Calculate the eletric field in two different places&lt;br /&gt;
 charge = 1.6e-19&lt;br /&gt;
 origin = vector(0,0,0)&lt;br /&gt;
 &lt;br /&gt;
 point1 = vector(-10, 5, 15)&lt;br /&gt;
 point2 = vector(20, -5, 12)&lt;br /&gt;
 &lt;br /&gt;
 field1 = 9e9 * charge * point1.norm() / point1.mag2&lt;br /&gt;
 field2 = 9e9 * charge * point2.norm() / point2.mag2&lt;br /&gt;
&lt;br /&gt;
We can use a function in the last 2 lines to reduce the duplicated code to the following&lt;br /&gt;
&lt;br /&gt;
 field1 = electricField(charge, point1)&lt;br /&gt;
 field2 = electricField(charge, point2)&lt;br /&gt;
&lt;br /&gt;
==Frequently Used Functions==&lt;br /&gt;
&lt;br /&gt;
VPython already has a few predefined functions for your ease. The following functions are available for working with vectors.&lt;br /&gt;
To better illustrate these functions, let&#039;s say we have a vector called exVector.&lt;br /&gt;
&lt;br /&gt;
 exVector = vector(-10, 2 ,5)&lt;br /&gt;
&lt;br /&gt;
===mag()===&lt;br /&gt;
 # Calculates the magnitude of a vector&lt;br /&gt;
 magExVector = mag(exVector)&lt;br /&gt;
 print magExVector    # will print 11.357&lt;br /&gt;
&lt;br /&gt;
===mag2(A)===&lt;br /&gt;
 # Calculates the magnitude squared of a vector&lt;br /&gt;
 mag2ExVector = mag2(exVector)&lt;br /&gt;
 print magExVector     # will print 129&lt;br /&gt;
&lt;br /&gt;
===norm(A)===&lt;br /&gt;
 # Calculates the unit vector of a vector&lt;br /&gt;
 unitExVector = norm(exVector)&lt;br /&gt;
 print unitExVector    # will print &amp;lt;-0.88, 0.17, 0.44&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dot(A, B)===&lt;br /&gt;
 # Calculates the scalar dot product between the two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 dotExVector = dot(exVector, exVector2)&lt;br /&gt;
 print dotExVector     # will print 4&lt;br /&gt;
&lt;br /&gt;
===cross(A, B)===&lt;br /&gt;
 # Calculates the vector cross product between two vectors&lt;br /&gt;
 # exVector2 = vector(4, -2 ,8)&lt;br /&gt;
 crossExVector = cross(exVector, exVector2)&lt;br /&gt;
 print crossExVector   # will print &amp;lt;26, 100, 12&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython VPython]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_basics VPython Basics]&lt;br /&gt;
&lt;br /&gt;
===Further reading===&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Common_Errors_and_Troubleshooting VPython Common Errors and Troubleshooting]&lt;br /&gt;
&lt;br /&gt;
[http://www.physicsbook.gatech.edu/VPython_Lists VPython Lists]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/2/library/functions.html Python standard function library].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: Modeling with VPython]]&lt;/div&gt;</summary>
		<author><name>Dkim469</name></author>
	</entry>
</feed>