<?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=Andrewhennessy</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=Andrewhennessy"/>
	<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/Special:Contributions/Andrewhennessy"/>
	<updated>2026-04-11T22:11:00Z</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=29339</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29339"/>
		<updated>2017-11-23T21:24:14Z</updated>

		<summary type="html">&lt;p&gt;Andrewhennessy: /* Summary */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Claimed By Andrew Hennessy (Fall 2017)&lt;br /&gt;
&lt;br /&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;
===Automating problems with VPython===&lt;br /&gt;
&lt;br /&gt;
 1.When you are trying to simulate multi-particle systems that require hundreds of calculations&lt;br /&gt;
 2.Sometimes when you are having trouble understanding homework problems solving it with python can help you understand hard to grasp concepts.&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;
===When to write a function===&lt;br /&gt;
 1. You have a multi particle system&lt;br /&gt;
 2. You are calculating multiple forces on multiple particles over each time step&lt;br /&gt;
 3. You try not to use functions but keep running into errors or you see behaviors that don&#039;t look right. &lt;br /&gt;
&lt;br /&gt;
===When NOT to write a function===&lt;br /&gt;
 1. You have a simple system consisting of one particle and less than 3 or so forces&lt;br /&gt;
 2. You only need to find 4 or 5 values pertaining to the kinematics of the object&lt;br /&gt;
 3. You would be creating more trouble if you were writing function. &lt;br /&gt;
&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;
==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;
==Examples (Continued)==&lt;br /&gt;
&lt;br /&gt;
With Vpython it is possible to make graphs with ease by simply referencing a x axis and providing values that correspond with the x axis and have a value that can be plotted on the y axis. &lt;br /&gt;
&lt;br /&gt;
To start you must import the necessary library:&lt;br /&gt;
&lt;br /&gt;
 from visual.graph import *&lt;br /&gt;
&lt;br /&gt;
Then make a variable and declare what your x axis will be as well as color properties:&lt;br /&gt;
 &lt;br /&gt;
 f1 = gcurve(color=color.cyan)&lt;br /&gt;
&lt;br /&gt;
Every time you iterate through a loop use your index (most likely time elapsed) and a output value to add a point to your plot:&lt;br /&gt;
&lt;br /&gt;
  f1.plot(pos=(x,outputValue)) &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
 &lt;br /&gt;
[[File:Graph1Ahennessy.png|500px]]&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;
&lt;br /&gt;
&lt;br /&gt;
==Connectedness(Taking Vpython core functions and creating a simulation)==&lt;br /&gt;
Many Vpython assignments require the creation of functions that can calculate Momentum,impulse,velocity Position&lt;br /&gt;
Below are some frequently used equations that can be adapted to certain problem sets:&lt;br /&gt;
&lt;br /&gt;
===Momentum===&lt;br /&gt;
&lt;br /&gt;
 def returnMomentum(vparticle,mparticle):&lt;br /&gt;
    momentum = vector(0,0,0)&lt;br /&gt;
    return vparticle*mparticle + momentum&lt;br /&gt;
 momentum = returnMomentum(vparticle,mparticle)&lt;br /&gt;
&lt;br /&gt;
===impulse===&lt;br /&gt;
&lt;br /&gt;
 # deltat = timestep in simulation. &lt;br /&gt;
 def returnImpulse(momentum,deltat):&lt;br /&gt;
    impulse = vector(0,0,0)&lt;br /&gt;
    return momentum*deltat&lt;br /&gt;
 impulse = returnImpulse(momentum,deltat)&lt;br /&gt;
&lt;br /&gt;
===velocity===&lt;br /&gt;
&lt;br /&gt;
 def returnVelocity(impulse,mparticle,velocity):&lt;br /&gt;
    velocity = vector(0,0,0)&lt;br /&gt;
    velocity = (impulse/mparticle)+velocity&lt;br /&gt;
    return velocity&lt;br /&gt;
 velocity = returnVelocity(impulse,mparticle,velocity)&lt;br /&gt;
&lt;br /&gt;
===position===&lt;br /&gt;
&lt;br /&gt;
 def returnPosition(position, velocity, deltat):&lt;br /&gt;
    position = vector(0,0,0)&lt;br /&gt;
    position = position + velocity*deltat&lt;br /&gt;
    return position&lt;br /&gt;
 position = returnPosition(position, velocity , deltat)&lt;br /&gt;
&lt;br /&gt;
===Drag force===&lt;br /&gt;
 def returnDragforce(dragcoef,velocity,density,area):&lt;br /&gt;
    dragForce = dragcoef*((density*(velocity**2))/2)*area&lt;br /&gt;
    dragForce = dragForce * -1 * norm(velocity)&lt;br /&gt;
    return dragForce&lt;br /&gt;
 dragForce = returnDragforce(dragcoef,velocity,density,area)&lt;br /&gt;
&lt;br /&gt;
===Bringing Everything Together===&lt;br /&gt;
&lt;br /&gt;
[[File:Ezgif-5-93f2b472a8.gif]]&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>Andrewhennessy</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29338</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29338"/>
		<updated>2017-11-23T20:33:17Z</updated>

		<summary type="html">&lt;p&gt;Andrewhennessy: /* Bringing Everything Together */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Claimed By Andrew Hennessy (Fall 2017)&lt;br /&gt;
&lt;br /&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;
===When to write a function===&lt;br /&gt;
 1. You have a multi particle system&lt;br /&gt;
 2. You are calculating multiple forces on multiple particles over each time step&lt;br /&gt;
 3. You try not to use functions but keep running into errors or you see behaviors that don&#039;t look right. &lt;br /&gt;
&lt;br /&gt;
===When NOT to write a function===&lt;br /&gt;
 1. You have a simple system consisting of one particle and less than 3 or so forces&lt;br /&gt;
 2. You only need to find 4 or 5 values pertaining to the kinematics of the object&lt;br /&gt;
 3. You would be creating more trouble if you were writing function. &lt;br /&gt;
&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;
==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;
==Examples (Continued)==&lt;br /&gt;
&lt;br /&gt;
With Vpython it is possible to make graphs with ease by simply referencing a x axis and providing values that correspond with the x axis and have a value that can be plotted on the y axis. &lt;br /&gt;
&lt;br /&gt;
To start you must import the necessary library:&lt;br /&gt;
&lt;br /&gt;
 from visual.graph import *&lt;br /&gt;
&lt;br /&gt;
Then make a variable and declare what your x axis will be as well as color properties:&lt;br /&gt;
 &lt;br /&gt;
 f1 = gcurve(color=color.cyan)&lt;br /&gt;
&lt;br /&gt;
Every time you iterate through a loop use your index (most likely time elapsed) and a output value to add a point to your plot:&lt;br /&gt;
&lt;br /&gt;
  f1.plot(pos=(x,outputValue)) &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
 &lt;br /&gt;
[[File:Graph1Ahennessy.png|500px]]&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;
&lt;br /&gt;
&lt;br /&gt;
==Connectedness(Taking Vpython core functions and creating a simulation)==&lt;br /&gt;
Many Vpython assignments require the creation of functions that can calculate Momentum,impulse,velocity Position&lt;br /&gt;
Below are some frequently used equations that can be adapted to certain problem sets:&lt;br /&gt;
&lt;br /&gt;
===Momentum===&lt;br /&gt;
&lt;br /&gt;
 def returnMomentum(vparticle,mparticle):&lt;br /&gt;
    momentum = vector(0,0,0)&lt;br /&gt;
    return vparticle*mparticle + momentum&lt;br /&gt;
 momentum = returnMomentum(vparticle,mparticle)&lt;br /&gt;
&lt;br /&gt;
===impulse===&lt;br /&gt;
&lt;br /&gt;
 # deltat = timestep in simulation. &lt;br /&gt;
 def returnImpulse(momentum,deltat):&lt;br /&gt;
    impulse = vector(0,0,0)&lt;br /&gt;
    return momentum*deltat&lt;br /&gt;
 impulse = returnImpulse(momentum,deltat)&lt;br /&gt;
&lt;br /&gt;
===velocity===&lt;br /&gt;
&lt;br /&gt;
 def returnVelocity(impulse,mparticle,velocity):&lt;br /&gt;
    velocity = vector(0,0,0)&lt;br /&gt;
    velocity = (impulse/mparticle)+velocity&lt;br /&gt;
    return velocity&lt;br /&gt;
 velocity = returnVelocity(impulse,mparticle,velocity)&lt;br /&gt;
&lt;br /&gt;
===position===&lt;br /&gt;
&lt;br /&gt;
 def returnPosition(position, velocity, deltat):&lt;br /&gt;
    position = vector(0,0,0)&lt;br /&gt;
    position = position + velocity*deltat&lt;br /&gt;
    return position&lt;br /&gt;
 position = returnPosition(position, velocity , deltat)&lt;br /&gt;
&lt;br /&gt;
===Drag force===&lt;br /&gt;
 def returnDragforce(dragcoef,velocity,density,area):&lt;br /&gt;
    dragForce = dragcoef*((density*(velocity**2))/2)*area&lt;br /&gt;
    dragForce = dragForce * -1 * norm(velocity)&lt;br /&gt;
    return dragForce&lt;br /&gt;
 dragForce = returnDragforce(dragcoef,velocity,density,area)&lt;br /&gt;
&lt;br /&gt;
===Bringing Everything Together===&lt;br /&gt;
&lt;br /&gt;
[[File:Ezgif-5-93f2b472a8.gif]]&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>Andrewhennessy</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=File:Ezgif-5-93f2b472a8.gif&amp;diff=29337</id>
		<title>File:Ezgif-5-93f2b472a8.gif</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=File:Ezgif-5-93f2b472a8.gif&amp;diff=29337"/>
		<updated>2017-11-23T20:32:46Z</updated>

		<summary type="html">&lt;p&gt;Andrewhennessy: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Andrewhennessy</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29336</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29336"/>
		<updated>2017-11-23T20:25:51Z</updated>

		<summary type="html">&lt;p&gt;Andrewhennessy: /* Connectedness(Taking Vpython core functions and creating a simulation) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Claimed By Andrew Hennessy (Fall 2017)&lt;br /&gt;
&lt;br /&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;
===When to write a function===&lt;br /&gt;
 1. You have a multi particle system&lt;br /&gt;
 2. You are calculating multiple forces on multiple particles over each time step&lt;br /&gt;
 3. You try not to use functions but keep running into errors or you see behaviors that don&#039;t look right. &lt;br /&gt;
&lt;br /&gt;
===When NOT to write a function===&lt;br /&gt;
 1. You have a simple system consisting of one particle and less than 3 or so forces&lt;br /&gt;
 2. You only need to find 4 or 5 values pertaining to the kinematics of the object&lt;br /&gt;
 3. You would be creating more trouble if you were writing function. &lt;br /&gt;
&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;
==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;
==Examples (Continued)==&lt;br /&gt;
&lt;br /&gt;
With Vpython it is possible to make graphs with ease by simply referencing a x axis and providing values that correspond with the x axis and have a value that can be plotted on the y axis. &lt;br /&gt;
&lt;br /&gt;
To start you must import the necessary library:&lt;br /&gt;
&lt;br /&gt;
 from visual.graph import *&lt;br /&gt;
&lt;br /&gt;
Then make a variable and declare what your x axis will be as well as color properties:&lt;br /&gt;
 &lt;br /&gt;
 f1 = gcurve(color=color.cyan)&lt;br /&gt;
&lt;br /&gt;
Every time you iterate through a loop use your index (most likely time elapsed) and a output value to add a point to your plot:&lt;br /&gt;
&lt;br /&gt;
  f1.plot(pos=(x,outputValue)) &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
 &lt;br /&gt;
[[File:Graph1Ahennessy.png|500px]]&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;
&lt;br /&gt;
&lt;br /&gt;
==Connectedness(Taking Vpython core functions and creating a simulation)==&lt;br /&gt;
Many Vpython assignments require the creation of functions that can calculate Momentum,impulse,velocity Position&lt;br /&gt;
Below are some frequently used equations that can be adapted to certain problem sets:&lt;br /&gt;
&lt;br /&gt;
===Momentum===&lt;br /&gt;
&lt;br /&gt;
 def returnMomentum(vparticle,mparticle):&lt;br /&gt;
    momentum = vector(0,0,0)&lt;br /&gt;
    return vparticle*mparticle + momentum&lt;br /&gt;
 momentum = returnMomentum(vparticle,mparticle)&lt;br /&gt;
&lt;br /&gt;
===impulse===&lt;br /&gt;
&lt;br /&gt;
 # deltat = timestep in simulation. &lt;br /&gt;
 def returnImpulse(momentum,deltat):&lt;br /&gt;
    impulse = vector(0,0,0)&lt;br /&gt;
    return momentum*deltat&lt;br /&gt;
 impulse = returnImpulse(momentum,deltat)&lt;br /&gt;
&lt;br /&gt;
===velocity===&lt;br /&gt;
&lt;br /&gt;
 def returnVelocity(impulse,mparticle,velocity):&lt;br /&gt;
    velocity = vector(0,0,0)&lt;br /&gt;
    velocity = (impulse/mparticle)+velocity&lt;br /&gt;
    return velocity&lt;br /&gt;
 velocity = returnVelocity(impulse,mparticle,velocity)&lt;br /&gt;
&lt;br /&gt;
===position===&lt;br /&gt;
&lt;br /&gt;
 def returnPosition(position, velocity, deltat):&lt;br /&gt;
    position = vector(0,0,0)&lt;br /&gt;
    position = position + velocity*deltat&lt;br /&gt;
    return position&lt;br /&gt;
 position = returnPosition(position, velocity , deltat)&lt;br /&gt;
&lt;br /&gt;
===Drag force===&lt;br /&gt;
 def returnDragforce(dragcoef,velocity,density,area):&lt;br /&gt;
    dragForce = dragcoef*((density*(velocity**2))/2)*area&lt;br /&gt;
    dragForce = dragForce * -1 * norm(velocity)&lt;br /&gt;
    return dragForce&lt;br /&gt;
 dragForce = returnDragforce(dragcoef,velocity,density,area)&lt;br /&gt;
&lt;br /&gt;
===Bringing Everything Together===&lt;br /&gt;
&amp;lt;iframe src=&amp;quot;https://trinket.io/embed/glowscript/69831da0af&amp;quot; width=&amp;quot;100%&amp;quot; height=&amp;quot;356&amp;quot; frameborder=&amp;quot;0&amp;quot; marginwidth=&amp;quot;0&amp;quot; marginheight=&amp;quot;0&amp;quot; allowfullscreen&amp;gt;&amp;lt;/iframe&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;
[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>Andrewhennessy</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29335</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29335"/>
		<updated>2017-11-23T20:17:40Z</updated>

		<summary type="html">&lt;p&gt;Andrewhennessy: /* Examples (Continued) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Claimed By Andrew Hennessy (Fall 2017)&lt;br /&gt;
&lt;br /&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;
===When to write a function===&lt;br /&gt;
 1. You have a multi particle system&lt;br /&gt;
 2. You are calculating multiple forces on multiple particles over each time step&lt;br /&gt;
 3. You try not to use functions but keep running into errors or you see behaviors that don&#039;t look right. &lt;br /&gt;
&lt;br /&gt;
===When NOT to write a function===&lt;br /&gt;
 1. You have a simple system consisting of one particle and less than 3 or so forces&lt;br /&gt;
 2. You only need to find 4 or 5 values pertaining to the kinematics of the object&lt;br /&gt;
 3. You would be creating more trouble if you were writing function. &lt;br /&gt;
&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;
==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;
==Examples (Continued)==&lt;br /&gt;
&lt;br /&gt;
With Vpython it is possible to make graphs with ease by simply referencing a x axis and providing values that correspond with the x axis and have a value that can be plotted on the y axis. &lt;br /&gt;
&lt;br /&gt;
To start you must import the necessary library:&lt;br /&gt;
&lt;br /&gt;
 from visual.graph import *&lt;br /&gt;
&lt;br /&gt;
Then make a variable and declare what your x axis will be as well as color properties:&lt;br /&gt;
 &lt;br /&gt;
 f1 = gcurve(color=color.cyan)&lt;br /&gt;
&lt;br /&gt;
Every time you iterate through a loop use your index (most likely time elapsed) and a output value to add a point to your plot:&lt;br /&gt;
&lt;br /&gt;
  f1.plot(pos=(x,outputValue)) &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
 &lt;br /&gt;
[[File:Graph1Ahennessy.png|500px]]&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;
&lt;br /&gt;
&lt;br /&gt;
==Connectedness(Taking Vpython core functions and creating a simulation)==&lt;br /&gt;
Many Vpython assignments require the creation of functions that can calculate Momentum,impulse,velocity Position&lt;br /&gt;
Below are some frequently used equations that can be adapted to certain problem sets:&lt;br /&gt;
&lt;br /&gt;
===Momentum===&lt;br /&gt;
&lt;br /&gt;
 def returnMomentum(vparticle,mparticle):&lt;br /&gt;
    momentum = vector(0,0,0)&lt;br /&gt;
    return vparticle*mparticle + momentum&lt;br /&gt;
 momentum = returnMomentum(vparticle,mparticle)&lt;br /&gt;
&lt;br /&gt;
===impulse===&lt;br /&gt;
&lt;br /&gt;
 # deltat = timestep in simulation. &lt;br /&gt;
 def returnImpulse(momentum,deltat):&lt;br /&gt;
    impulse = vector(0,0,0)&lt;br /&gt;
    return momentum*deltat&lt;br /&gt;
 impulse = returnImpulse(momentum,deltat)&lt;br /&gt;
&lt;br /&gt;
===velocity===&lt;br /&gt;
&lt;br /&gt;
 def returnVelocity(impulse,mparticle,velocity):&lt;br /&gt;
    velocity = vector(0,0,0)&lt;br /&gt;
    velocity = (impulse/mparticle)+velocity&lt;br /&gt;
    return velocity&lt;br /&gt;
 velocity = returnVelocity(impulse,mparticle,velocity)&lt;br /&gt;
&lt;br /&gt;
===position===&lt;br /&gt;
&lt;br /&gt;
 def returnPosition(position, velocity, deltat):&lt;br /&gt;
    position = vector(0,0,0)&lt;br /&gt;
    position = position + velocity*deltat&lt;br /&gt;
    return position&lt;br /&gt;
 position = returnPosition(position, velocity , deltat)&lt;br /&gt;
&lt;br /&gt;
===Drag force===&lt;br /&gt;
 def returnDragforce(dragcoef,velocity,density,area):&lt;br /&gt;
    dragForce = dragcoef*((density*(velocity**2))/2)*area&lt;br /&gt;
    dragForce = dragForce * -1 * norm(velocity)&lt;br /&gt;
    return dragForce&lt;br /&gt;
 dragForce = returnDragforce(dragcoef,velocity,density,area)&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>Andrewhennessy</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29334</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29334"/>
		<updated>2017-11-23T20:17:14Z</updated>

		<summary type="html">&lt;p&gt;Andrewhennessy: /* Examples (Continued) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Claimed By Andrew Hennessy (Fall 2017)&lt;br /&gt;
&lt;br /&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;
===When to write a function===&lt;br /&gt;
 1. You have a multi particle system&lt;br /&gt;
 2. You are calculating multiple forces on multiple particles over each time step&lt;br /&gt;
 3. You try not to use functions but keep running into errors or you see behaviors that don&#039;t look right. &lt;br /&gt;
&lt;br /&gt;
===When NOT to write a function===&lt;br /&gt;
 1. You have a simple system consisting of one particle and less than 3 or so forces&lt;br /&gt;
 2. You only need to find 4 or 5 values pertaining to the kinematics of the object&lt;br /&gt;
 3. You would be creating more trouble if you were writing function. &lt;br /&gt;
&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;
==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;
==Examples (Continued)==&lt;br /&gt;
&lt;br /&gt;
With Vpython it is possible to make graphs with ease by simply referencing a x axis and providing values that correspond with the x axis and have a value that can be plotted on the y axis. &lt;br /&gt;
&lt;br /&gt;
To start you must import the necessary library:&lt;br /&gt;
&lt;br /&gt;
 from visual.graph import *&lt;br /&gt;
&lt;br /&gt;
Then make a variable and declare what your x axis will be as well as color properties:&lt;br /&gt;
 &lt;br /&gt;
 f1 = gcurve(color=color.cyan)&lt;br /&gt;
&lt;br /&gt;
Every time you iterate through a loop use your index (most likely time elapsed) and a output value to add a point to your plot:&lt;br /&gt;
&lt;br /&gt;
  f1.plot(pos=(x,outputValue)) &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
 &lt;br /&gt;
[[File:Graph1Ahennessy.png|500px]]|&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;
&lt;br /&gt;
&lt;br /&gt;
==Connectedness(Taking Vpython core functions and creating a simulation)==&lt;br /&gt;
Many Vpython assignments require the creation of functions that can calculate Momentum,impulse,velocity Position&lt;br /&gt;
Below are some frequently used equations that can be adapted to certain problem sets:&lt;br /&gt;
&lt;br /&gt;
===Momentum===&lt;br /&gt;
&lt;br /&gt;
 def returnMomentum(vparticle,mparticle):&lt;br /&gt;
    momentum = vector(0,0,0)&lt;br /&gt;
    return vparticle*mparticle + momentum&lt;br /&gt;
 momentum = returnMomentum(vparticle,mparticle)&lt;br /&gt;
&lt;br /&gt;
===impulse===&lt;br /&gt;
&lt;br /&gt;
 # deltat = timestep in simulation. &lt;br /&gt;
 def returnImpulse(momentum,deltat):&lt;br /&gt;
    impulse = vector(0,0,0)&lt;br /&gt;
    return momentum*deltat&lt;br /&gt;
 impulse = returnImpulse(momentum,deltat)&lt;br /&gt;
&lt;br /&gt;
===velocity===&lt;br /&gt;
&lt;br /&gt;
 def returnVelocity(impulse,mparticle,velocity):&lt;br /&gt;
    velocity = vector(0,0,0)&lt;br /&gt;
    velocity = (impulse/mparticle)+velocity&lt;br /&gt;
    return velocity&lt;br /&gt;
 velocity = returnVelocity(impulse,mparticle,velocity)&lt;br /&gt;
&lt;br /&gt;
===position===&lt;br /&gt;
&lt;br /&gt;
 def returnPosition(position, velocity, deltat):&lt;br /&gt;
    position = vector(0,0,0)&lt;br /&gt;
    position = position + velocity*deltat&lt;br /&gt;
    return position&lt;br /&gt;
 position = returnPosition(position, velocity , deltat)&lt;br /&gt;
&lt;br /&gt;
===Drag force===&lt;br /&gt;
 def returnDragforce(dragcoef,velocity,density,area):&lt;br /&gt;
    dragForce = dragcoef*((density*(velocity**2))/2)*area&lt;br /&gt;
    dragForce = dragForce * -1 * norm(velocity)&lt;br /&gt;
    return dragForce&lt;br /&gt;
 dragForce = returnDragforce(dragcoef,velocity,density,area)&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>Andrewhennessy</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29333</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29333"/>
		<updated>2017-11-23T20:16:14Z</updated>

		<summary type="html">&lt;p&gt;Andrewhennessy: /* Examples (Continued) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Claimed By Andrew Hennessy (Fall 2017)&lt;br /&gt;
&lt;br /&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;
===When to write a function===&lt;br /&gt;
 1. You have a multi particle system&lt;br /&gt;
 2. You are calculating multiple forces on multiple particles over each time step&lt;br /&gt;
 3. You try not to use functions but keep running into errors or you see behaviors that don&#039;t look right. &lt;br /&gt;
&lt;br /&gt;
===When NOT to write a function===&lt;br /&gt;
 1. You have a simple system consisting of one particle and less than 3 or so forces&lt;br /&gt;
 2. You only need to find 4 or 5 values pertaining to the kinematics of the object&lt;br /&gt;
 3. You would be creating more trouble if you were writing function. &lt;br /&gt;
&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;
==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;
==Examples (Continued)==&lt;br /&gt;
&lt;br /&gt;
With Vpython it is possible to make graphs with ease by simply referencing a x axis and providing values that correspond with the x axis and have a value that can be plotted on the y axis. &lt;br /&gt;
&lt;br /&gt;
To start you must import the necessary library:&lt;br /&gt;
&lt;br /&gt;
 from visual.graph import *&lt;br /&gt;
&lt;br /&gt;
Then make a variable and declare what your x axis will be as well as color properties:&lt;br /&gt;
 &lt;br /&gt;
 f1 = gcurve(color=color.cyan)&lt;br /&gt;
&lt;br /&gt;
Every time you iterate through a loop use your index (most likely time elapsed) and a output value to add a point to your plot:&lt;br /&gt;
&lt;br /&gt;
  f1.plot(pos=(x,outputValue)) &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
 &lt;br /&gt;
[[File:Graph1Ahennessy.png]]|500px|&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;
&lt;br /&gt;
&lt;br /&gt;
==Connectedness(Taking Vpython core functions and creating a simulation)==&lt;br /&gt;
Many Vpython assignments require the creation of functions that can calculate Momentum,impulse,velocity Position&lt;br /&gt;
Below are some frequently used equations that can be adapted to certain problem sets:&lt;br /&gt;
&lt;br /&gt;
===Momentum===&lt;br /&gt;
&lt;br /&gt;
 def returnMomentum(vparticle,mparticle):&lt;br /&gt;
    momentum = vector(0,0,0)&lt;br /&gt;
    return vparticle*mparticle + momentum&lt;br /&gt;
 momentum = returnMomentum(vparticle,mparticle)&lt;br /&gt;
&lt;br /&gt;
===impulse===&lt;br /&gt;
&lt;br /&gt;
 # deltat = timestep in simulation. &lt;br /&gt;
 def returnImpulse(momentum,deltat):&lt;br /&gt;
    impulse = vector(0,0,0)&lt;br /&gt;
    return momentum*deltat&lt;br /&gt;
 impulse = returnImpulse(momentum,deltat)&lt;br /&gt;
&lt;br /&gt;
===velocity===&lt;br /&gt;
&lt;br /&gt;
 def returnVelocity(impulse,mparticle,velocity):&lt;br /&gt;
    velocity = vector(0,0,0)&lt;br /&gt;
    velocity = (impulse/mparticle)+velocity&lt;br /&gt;
    return velocity&lt;br /&gt;
 velocity = returnVelocity(impulse,mparticle,velocity)&lt;br /&gt;
&lt;br /&gt;
===position===&lt;br /&gt;
&lt;br /&gt;
 def returnPosition(position, velocity, deltat):&lt;br /&gt;
    position = vector(0,0,0)&lt;br /&gt;
    position = position + velocity*deltat&lt;br /&gt;
    return position&lt;br /&gt;
 position = returnPosition(position, velocity , deltat)&lt;br /&gt;
&lt;br /&gt;
===Drag force===&lt;br /&gt;
 def returnDragforce(dragcoef,velocity,density,area):&lt;br /&gt;
    dragForce = dragcoef*((density*(velocity**2))/2)*area&lt;br /&gt;
    dragForce = dragForce * -1 * norm(velocity)&lt;br /&gt;
    return dragForce&lt;br /&gt;
 dragForce = returnDragforce(dragcoef,velocity,density,area)&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>Andrewhennessy</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29332</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29332"/>
		<updated>2017-11-23T20:15:59Z</updated>

		<summary type="html">&lt;p&gt;Andrewhennessy: /* Examples (Continued) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Claimed By Andrew Hennessy (Fall 2017)&lt;br /&gt;
&lt;br /&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;
===When to write a function===&lt;br /&gt;
 1. You have a multi particle system&lt;br /&gt;
 2. You are calculating multiple forces on multiple particles over each time step&lt;br /&gt;
 3. You try not to use functions but keep running into errors or you see behaviors that don&#039;t look right. &lt;br /&gt;
&lt;br /&gt;
===When NOT to write a function===&lt;br /&gt;
 1. You have a simple system consisting of one particle and less than 3 or so forces&lt;br /&gt;
 2. You only need to find 4 or 5 values pertaining to the kinematics of the object&lt;br /&gt;
 3. You would be creating more trouble if you were writing function. &lt;br /&gt;
&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;
==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;
==Examples (Continued)==&lt;br /&gt;
&lt;br /&gt;
With Vpython it is possible to make graphs with ease by simply referencing a x axis and providing values that correspond with the x axis and have a value that can be plotted on the y axis. &lt;br /&gt;
&lt;br /&gt;
To start you must import the necessary library:&lt;br /&gt;
&lt;br /&gt;
 from visual.graph import *&lt;br /&gt;
&lt;br /&gt;
Then make a variable and declare what your x axis will be as well as color properties:&lt;br /&gt;
 &lt;br /&gt;
 f1 = gcurve(color=color.cyan)&lt;br /&gt;
&lt;br /&gt;
Every time you iterate through a loop use your index (most likely time elapsed) and a output value to add a point to your plot:&lt;br /&gt;
&lt;br /&gt;
  f1.plot(pos=(x,outputValue)) &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
 &lt;br /&gt;
[[File:Graph1Ahennessy.png]]|500px&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;
&lt;br /&gt;
&lt;br /&gt;
==Connectedness(Taking Vpython core functions and creating a simulation)==&lt;br /&gt;
Many Vpython assignments require the creation of functions that can calculate Momentum,impulse,velocity Position&lt;br /&gt;
Below are some frequently used equations that can be adapted to certain problem sets:&lt;br /&gt;
&lt;br /&gt;
===Momentum===&lt;br /&gt;
&lt;br /&gt;
 def returnMomentum(vparticle,mparticle):&lt;br /&gt;
    momentum = vector(0,0,0)&lt;br /&gt;
    return vparticle*mparticle + momentum&lt;br /&gt;
 momentum = returnMomentum(vparticle,mparticle)&lt;br /&gt;
&lt;br /&gt;
===impulse===&lt;br /&gt;
&lt;br /&gt;
 # deltat = timestep in simulation. &lt;br /&gt;
 def returnImpulse(momentum,deltat):&lt;br /&gt;
    impulse = vector(0,0,0)&lt;br /&gt;
    return momentum*deltat&lt;br /&gt;
 impulse = returnImpulse(momentum,deltat)&lt;br /&gt;
&lt;br /&gt;
===velocity===&lt;br /&gt;
&lt;br /&gt;
 def returnVelocity(impulse,mparticle,velocity):&lt;br /&gt;
    velocity = vector(0,0,0)&lt;br /&gt;
    velocity = (impulse/mparticle)+velocity&lt;br /&gt;
    return velocity&lt;br /&gt;
 velocity = returnVelocity(impulse,mparticle,velocity)&lt;br /&gt;
&lt;br /&gt;
===position===&lt;br /&gt;
&lt;br /&gt;
 def returnPosition(position, velocity, deltat):&lt;br /&gt;
    position = vector(0,0,0)&lt;br /&gt;
    position = position + velocity*deltat&lt;br /&gt;
    return position&lt;br /&gt;
 position = returnPosition(position, velocity , deltat)&lt;br /&gt;
&lt;br /&gt;
===Drag force===&lt;br /&gt;
 def returnDragforce(dragcoef,velocity,density,area):&lt;br /&gt;
    dragForce = dragcoef*((density*(velocity**2))/2)*area&lt;br /&gt;
    dragForce = dragForce * -1 * norm(velocity)&lt;br /&gt;
    return dragForce&lt;br /&gt;
 dragForce = returnDragforce(dragcoef,velocity,density,area)&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>Andrewhennessy</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29331</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29331"/>
		<updated>2017-11-23T20:14:53Z</updated>

		<summary type="html">&lt;p&gt;Andrewhennessy: /* Examples (Continued) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Claimed By Andrew Hennessy (Fall 2017)&lt;br /&gt;
&lt;br /&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;
===When to write a function===&lt;br /&gt;
 1. You have a multi particle system&lt;br /&gt;
 2. You are calculating multiple forces on multiple particles over each time step&lt;br /&gt;
 3. You try not to use functions but keep running into errors or you see behaviors that don&#039;t look right. &lt;br /&gt;
&lt;br /&gt;
===When NOT to write a function===&lt;br /&gt;
 1. You have a simple system consisting of one particle and less than 3 or so forces&lt;br /&gt;
 2. You only need to find 4 or 5 values pertaining to the kinematics of the object&lt;br /&gt;
 3. You would be creating more trouble if you were writing function. &lt;br /&gt;
&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;
==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;
==Examples (Continued)==&lt;br /&gt;
&lt;br /&gt;
With Vpython it is possible to make graphs with ease by simply referencing a x axis and providing values that correspond with the x axis and have a value that can be plotted on the y axis. &lt;br /&gt;
&lt;br /&gt;
To start you must import the necessary library:&lt;br /&gt;
&lt;br /&gt;
 from visual.graph import *&lt;br /&gt;
&lt;br /&gt;
Then make a variable and declare what your x axis will be as well as color properties:&lt;br /&gt;
 &lt;br /&gt;
 f1 = gcurve(color=color.cyan)&lt;br /&gt;
&lt;br /&gt;
Every time you iterate through a loop use your index (most likely time elapsed) and a output value to add a point to your plot:&lt;br /&gt;
&lt;br /&gt;
  f1.plot(pos=(x,outputValue)) &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
 &lt;br /&gt;
[[File:Graph1Ahennessy.png]]&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;
&lt;br /&gt;
&lt;br /&gt;
==Connectedness(Taking Vpython core functions and creating a simulation)==&lt;br /&gt;
Many Vpython assignments require the creation of functions that can calculate Momentum,impulse,velocity Position&lt;br /&gt;
Below are some frequently used equations that can be adapted to certain problem sets:&lt;br /&gt;
&lt;br /&gt;
===Momentum===&lt;br /&gt;
&lt;br /&gt;
 def returnMomentum(vparticle,mparticle):&lt;br /&gt;
    momentum = vector(0,0,0)&lt;br /&gt;
    return vparticle*mparticle + momentum&lt;br /&gt;
 momentum = returnMomentum(vparticle,mparticle)&lt;br /&gt;
&lt;br /&gt;
===impulse===&lt;br /&gt;
&lt;br /&gt;
 # deltat = timestep in simulation. &lt;br /&gt;
 def returnImpulse(momentum,deltat):&lt;br /&gt;
    impulse = vector(0,0,0)&lt;br /&gt;
    return momentum*deltat&lt;br /&gt;
 impulse = returnImpulse(momentum,deltat)&lt;br /&gt;
&lt;br /&gt;
===velocity===&lt;br /&gt;
&lt;br /&gt;
 def returnVelocity(impulse,mparticle,velocity):&lt;br /&gt;
    velocity = vector(0,0,0)&lt;br /&gt;
    velocity = (impulse/mparticle)+velocity&lt;br /&gt;
    return velocity&lt;br /&gt;
 velocity = returnVelocity(impulse,mparticle,velocity)&lt;br /&gt;
&lt;br /&gt;
===position===&lt;br /&gt;
&lt;br /&gt;
 def returnPosition(position, velocity, deltat):&lt;br /&gt;
    position = vector(0,0,0)&lt;br /&gt;
    position = position + velocity*deltat&lt;br /&gt;
    return position&lt;br /&gt;
 position = returnPosition(position, velocity , deltat)&lt;br /&gt;
&lt;br /&gt;
===Drag force===&lt;br /&gt;
 def returnDragforce(dragcoef,velocity,density,area):&lt;br /&gt;
    dragForce = dragcoef*((density*(velocity**2))/2)*area&lt;br /&gt;
    dragForce = dragForce * -1 * norm(velocity)&lt;br /&gt;
    return dragForce&lt;br /&gt;
 dragForce = returnDragforce(dragcoef,velocity,density,area)&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>Andrewhennessy</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=File:Graph1Ahennessy.png&amp;diff=29330</id>
		<title>File:Graph1Ahennessy.png</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=File:Graph1Ahennessy.png&amp;diff=29330"/>
		<updated>2017-11-23T20:14:01Z</updated>

		<summary type="html">&lt;p&gt;Andrewhennessy: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Andrewhennessy</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29329</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29329"/>
		<updated>2017-11-23T20:12:40Z</updated>

		<summary type="html">&lt;p&gt;Andrewhennessy: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Claimed By Andrew Hennessy (Fall 2017)&lt;br /&gt;
&lt;br /&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;
===When to write a function===&lt;br /&gt;
 1. You have a multi particle system&lt;br /&gt;
 2. You are calculating multiple forces on multiple particles over each time step&lt;br /&gt;
 3. You try not to use functions but keep running into errors or you see behaviors that don&#039;t look right. &lt;br /&gt;
&lt;br /&gt;
===When NOT to write a function===&lt;br /&gt;
 1. You have a simple system consisting of one particle and less than 3 or so forces&lt;br /&gt;
 2. You only need to find 4 or 5 values pertaining to the kinematics of the object&lt;br /&gt;
 3. You would be creating more trouble if you were writing function. &lt;br /&gt;
&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;
==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;
==Examples (Continued)==&lt;br /&gt;
&lt;br /&gt;
With Vpython it is possible to make graphs with ease by simply referencing a x axis and providing values that correspond with the x axis and have a value that can be plotted on the y axis. &lt;br /&gt;
&lt;br /&gt;
To start you must import the necessary library:&lt;br /&gt;
&lt;br /&gt;
 from visual.graph import *&lt;br /&gt;
&lt;br /&gt;
Then make a variable and declare what your x axis will be as well as color properties:&lt;br /&gt;
 &lt;br /&gt;
 f1 = gcurve(color=color.cyan)&lt;br /&gt;
&lt;br /&gt;
Every time you iterate through a loop use your index (most likely time elapsed) and a output value to add a point to your plot:&lt;br /&gt;
&lt;br /&gt;
  f1.plot(pos=(x,outputValue)) &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
 &lt;br /&gt;
[[File:Example.jpg]]&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;
&lt;br /&gt;
&lt;br /&gt;
==Connectedness(Taking Vpython core functions and creating a simulation)==&lt;br /&gt;
Many Vpython assignments require the creation of functions that can calculate Momentum,impulse,velocity Position&lt;br /&gt;
Below are some frequently used equations that can be adapted to certain problem sets:&lt;br /&gt;
&lt;br /&gt;
===Momentum===&lt;br /&gt;
&lt;br /&gt;
 def returnMomentum(vparticle,mparticle):&lt;br /&gt;
    momentum = vector(0,0,0)&lt;br /&gt;
    return vparticle*mparticle + momentum&lt;br /&gt;
 momentum = returnMomentum(vparticle,mparticle)&lt;br /&gt;
&lt;br /&gt;
===impulse===&lt;br /&gt;
&lt;br /&gt;
 # deltat = timestep in simulation. &lt;br /&gt;
 def returnImpulse(momentum,deltat):&lt;br /&gt;
    impulse = vector(0,0,0)&lt;br /&gt;
    return momentum*deltat&lt;br /&gt;
 impulse = returnImpulse(momentum,deltat)&lt;br /&gt;
&lt;br /&gt;
===velocity===&lt;br /&gt;
&lt;br /&gt;
 def returnVelocity(impulse,mparticle,velocity):&lt;br /&gt;
    velocity = vector(0,0,0)&lt;br /&gt;
    velocity = (impulse/mparticle)+velocity&lt;br /&gt;
    return velocity&lt;br /&gt;
 velocity = returnVelocity(impulse,mparticle,velocity)&lt;br /&gt;
&lt;br /&gt;
===position===&lt;br /&gt;
&lt;br /&gt;
 def returnPosition(position, velocity, deltat):&lt;br /&gt;
    position = vector(0,0,0)&lt;br /&gt;
    position = position + velocity*deltat&lt;br /&gt;
    return position&lt;br /&gt;
 position = returnPosition(position, velocity , deltat)&lt;br /&gt;
&lt;br /&gt;
===Drag force===&lt;br /&gt;
 def returnDragforce(dragcoef,velocity,density,area):&lt;br /&gt;
    dragForce = dragcoef*((density*(velocity**2))/2)*area&lt;br /&gt;
    dragForce = dragForce * -1 * norm(velocity)&lt;br /&gt;
    return dragForce&lt;br /&gt;
 dragForce = returnDragforce(dragcoef,velocity,density,area)&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>Andrewhennessy</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29328</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29328"/>
		<updated>2017-11-23T19:55:18Z</updated>

		<summary type="html">&lt;p&gt;Andrewhennessy: /* Commonly Used Functions (Mechanics) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Claimed By Andrew Hennessy (Fall 2017)&lt;br /&gt;
&lt;br /&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;
===When to write a function===&lt;br /&gt;
 1. You have a multi particle system&lt;br /&gt;
 2. You are calculating multiple forces on multiple particles over each time step&lt;br /&gt;
 3. You try not to use functions but keep running into errors or you see behaviors that don&#039;t look right. &lt;br /&gt;
&lt;br /&gt;
===When NOT to write a function===&lt;br /&gt;
 1. You have a simple system consisting of one particle and less than 3 or so forces&lt;br /&gt;
 2. You only need to find 4 or 5 values pertaining to the kinematics of the object&lt;br /&gt;
 3. You would be creating more trouble if you were writing function. &lt;br /&gt;
&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;
==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;
&lt;br /&gt;
&lt;br /&gt;
==Connectedness(Taking Vpython core functions and creating a simulation)==&lt;br /&gt;
Many Vpython assignments require the creation of functions that can calculate Momentum,impulse,velocity Position&lt;br /&gt;
Below are some frequently used equations that can be adapted to certain problem sets:&lt;br /&gt;
&lt;br /&gt;
===Momentum===&lt;br /&gt;
&lt;br /&gt;
 def returnMomentum(vparticle,mparticle):&lt;br /&gt;
    momentum = vector(0,0,0)&lt;br /&gt;
    return vparticle*mparticle + momentum&lt;br /&gt;
 momentum = returnMomentum(vparticle,mparticle)&lt;br /&gt;
&lt;br /&gt;
===impulse===&lt;br /&gt;
&lt;br /&gt;
 # deltat = timestep in simulation. &lt;br /&gt;
 def returnImpulse(momentum,deltat):&lt;br /&gt;
    impulse = vector(0,0,0)&lt;br /&gt;
    return momentum*deltat&lt;br /&gt;
 impulse = returnImpulse(momentum,deltat)&lt;br /&gt;
&lt;br /&gt;
===velocity===&lt;br /&gt;
&lt;br /&gt;
 def returnVelocity(impulse,mparticle,velocity):&lt;br /&gt;
    velocity = vector(0,0,0)&lt;br /&gt;
    velocity = (impulse/mparticle)+velocity&lt;br /&gt;
    return velocity&lt;br /&gt;
 velocity = returnVelocity(impulse,mparticle,velocity)&lt;br /&gt;
&lt;br /&gt;
===position===&lt;br /&gt;
&lt;br /&gt;
 def returnPosition(position, velocity, deltat):&lt;br /&gt;
    position = vector(0,0,0)&lt;br /&gt;
    position = position + velocity*deltat&lt;br /&gt;
    return position&lt;br /&gt;
 position = returnPosition(position, velocity , deltat)&lt;br /&gt;
&lt;br /&gt;
===Drag force===&lt;br /&gt;
 def returnDragforce(dragcoef,velocity,density,area):&lt;br /&gt;
    dragForce = dragcoef*((density*(velocity**2))/2)*area&lt;br /&gt;
    dragForce = dragForce * -1 * norm(velocity)&lt;br /&gt;
    return dragForce&lt;br /&gt;
 dragForce = returnDragforce(dragcoef,velocity,density,area)&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>Andrewhennessy</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29327</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29327"/>
		<updated>2017-11-23T19:48:44Z</updated>

		<summary type="html">&lt;p&gt;Andrewhennessy: /* Writing your own Functions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Claimed By Andrew Hennessy (Fall 2017)&lt;br /&gt;
&lt;br /&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;
===When to write a function===&lt;br /&gt;
 1. You have a multi particle system&lt;br /&gt;
 2. You are calculating multiple forces on multiple particles over each time step&lt;br /&gt;
 3. You try not to use functions but keep running into errors or you see behaviors that don&#039;t look right. &lt;br /&gt;
&lt;br /&gt;
===When NOT to write a function===&lt;br /&gt;
 1. You have a simple system consisting of one particle and less than 3 or so forces&lt;br /&gt;
 2. You only need to find 4 or 5 values pertaining to the kinematics of the object&lt;br /&gt;
 3. You would be creating more trouble if you were writing function. &lt;br /&gt;
&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;
==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;
&lt;br /&gt;
&lt;br /&gt;
==Commonly Used Functions (Mechanics) ==&lt;br /&gt;
Many Vpython assignments require the creation of functions that can calculate Momentum,impulse,velocity Position&lt;br /&gt;
Below are some frequently used equations that can be adapted to certain problem sets:&lt;br /&gt;
&lt;br /&gt;
===Momentum===&lt;br /&gt;
&lt;br /&gt;
 def returnMomentum(vparticle,mparticle):&lt;br /&gt;
    momentum = vector(0,0,0)&lt;br /&gt;
    return vparticle*mparticle + momentum&lt;br /&gt;
 momentum = returnMomentum(vparticle,mparticle)&lt;br /&gt;
&lt;br /&gt;
===impulse===&lt;br /&gt;
&lt;br /&gt;
 # deltat = timestep in simulation. &lt;br /&gt;
 def returnImpulse(momentum,deltat):&lt;br /&gt;
    impulse = vector(0,0,0)&lt;br /&gt;
    return momentum*deltat&lt;br /&gt;
 impulse = returnImpulse(momentum,deltat)&lt;br /&gt;
&lt;br /&gt;
===velocity===&lt;br /&gt;
&lt;br /&gt;
 def returnVelocity(impulse,mparticle,velocity):&lt;br /&gt;
    velocity = vector(0,0,0)&lt;br /&gt;
    velocity = (impulse/mparticle)+velocity&lt;br /&gt;
    return velocity&lt;br /&gt;
 velocity = returnVelocity(impulse,mparticle,velocity)&lt;br /&gt;
&lt;br /&gt;
===position===&lt;br /&gt;
&lt;br /&gt;
 def returnPosition(position, velocity, deltat):&lt;br /&gt;
    position = vector(0,0,0)&lt;br /&gt;
    position = position + velocity*deltat&lt;br /&gt;
    return position&lt;br /&gt;
 position = returnPosition(position, velocity , deltat)&lt;br /&gt;
&lt;br /&gt;
===Drag force===&lt;br /&gt;
 def returnDragforce(dragcoef,velocity,density,area):&lt;br /&gt;
    dragForce = dragcoef*((density*(velocity**2))/2)*area&lt;br /&gt;
    dragForce = dragForce * -1 * norm(velocity)&lt;br /&gt;
    return dragForce&lt;br /&gt;
 dragForce = returnDragforce(dragcoef,velocity,density,area)&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>Andrewhennessy</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29326</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29326"/>
		<updated>2017-11-23T19:42:00Z</updated>

		<summary type="html">&lt;p&gt;Andrewhennessy: /* Commonly Used Functions (Mechanics) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Claimed By Andrew Hennessy (Fall 2017)&lt;br /&gt;
&lt;br /&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;
==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;
&lt;br /&gt;
&lt;br /&gt;
==Commonly Used Functions (Mechanics) ==&lt;br /&gt;
Many Vpython assignments require the creation of functions that can calculate Momentum,impulse,velocity Position&lt;br /&gt;
Below are some frequently used equations that can be adapted to certain problem sets:&lt;br /&gt;
&lt;br /&gt;
===Momentum===&lt;br /&gt;
&lt;br /&gt;
 def returnMomentum(vparticle,mparticle):&lt;br /&gt;
    momentum = vector(0,0,0)&lt;br /&gt;
    return vparticle*mparticle + momentum&lt;br /&gt;
 momentum = returnMomentum(vparticle,mparticle)&lt;br /&gt;
&lt;br /&gt;
===impulse===&lt;br /&gt;
&lt;br /&gt;
 # deltat = timestep in simulation. &lt;br /&gt;
 def returnImpulse(momentum,deltat):&lt;br /&gt;
    impulse = vector(0,0,0)&lt;br /&gt;
    return momentum*deltat&lt;br /&gt;
 impulse = returnImpulse(momentum,deltat)&lt;br /&gt;
&lt;br /&gt;
===velocity===&lt;br /&gt;
&lt;br /&gt;
 def returnVelocity(impulse,mparticle,velocity):&lt;br /&gt;
    velocity = vector(0,0,0)&lt;br /&gt;
    velocity = (impulse/mparticle)+velocity&lt;br /&gt;
    return velocity&lt;br /&gt;
 velocity = returnVelocity(impulse,mparticle,velocity)&lt;br /&gt;
&lt;br /&gt;
===position===&lt;br /&gt;
&lt;br /&gt;
 def returnPosition(position, velocity, deltat):&lt;br /&gt;
    position = vector(0,0,0)&lt;br /&gt;
    position = position + velocity*deltat&lt;br /&gt;
    return position&lt;br /&gt;
 position = returnPosition(position, velocity , deltat)&lt;br /&gt;
&lt;br /&gt;
===Drag force===&lt;br /&gt;
 def returnDragforce(dragcoef,velocity,density,area):&lt;br /&gt;
    dragForce = dragcoef*((density*(velocity**2))/2)*area&lt;br /&gt;
    dragForce = dragForce * -1 * norm(velocity)&lt;br /&gt;
    return dragForce&lt;br /&gt;
 dragForce = returnDragforce(dragcoef,velocity,density,area)&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>Andrewhennessy</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29325</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29325"/>
		<updated>2017-11-23T19:30:47Z</updated>

		<summary type="html">&lt;p&gt;Andrewhennessy: /* Commonly Used Functions (Mechanics) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Claimed By Andrew Hennessy (Fall 2017)&lt;br /&gt;
&lt;br /&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;
==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;
&lt;br /&gt;
&lt;br /&gt;
==Commonly Used Functions (Mechanics) ==&lt;br /&gt;
Many Vpython assignments require the creation of functions that can calculate Momentum,impulse,velocity Position&lt;br /&gt;
Below are some frequently used equations that can be adapted to certain problem sets:&lt;br /&gt;
&lt;br /&gt;
===Momentum===&lt;br /&gt;
&lt;br /&gt;
 def returnMomentum(vparticle,mparticle):&lt;br /&gt;
    momentum = vector(0,0,0)&lt;br /&gt;
    return vparticle*mparticle + momentum&lt;br /&gt;
 momentum = returnMomentum(vparticle,mparticle)&lt;br /&gt;
&lt;br /&gt;
===impulse===&lt;br /&gt;
&lt;br /&gt;
 # deltat = timestep in simulation. &lt;br /&gt;
 def returnImpulse(momentum,deltat):&lt;br /&gt;
    impulse = vector(0,0,0)&lt;br /&gt;
    return momentum*deltat&lt;br /&gt;
 impulse = returnImpulse(momentum,deltat)&lt;br /&gt;
&lt;br /&gt;
===velocity===&lt;br /&gt;
&lt;br /&gt;
 def returnVelocity(impulse,mparticle,velocity):&lt;br /&gt;
    velocity = vector(0,0,0)&lt;br /&gt;
    velocity = (impulse/mparticle)+velocity&lt;br /&gt;
    return velocity&lt;br /&gt;
 velocity = returnVelocity(impulse,mparticle,velocity)&lt;br /&gt;
&lt;br /&gt;
===position===&lt;br /&gt;
&lt;br /&gt;
 def returnPosition(position, velocity, deltat):&lt;br /&gt;
    position = vector(0,0,0)&lt;br /&gt;
    position = position + velocity*deltat&lt;br /&gt;
    return position&lt;br /&gt;
 position = returnPosition(position, velocity , deltat)&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>Andrewhennessy</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29324</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29324"/>
		<updated>2017-11-23T19:30:03Z</updated>

		<summary type="html">&lt;p&gt;Andrewhennessy: /* impulse */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Claimed By Andrew Hennessy (Fall 2017)&lt;br /&gt;
&lt;br /&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;
==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;
&lt;br /&gt;
&lt;br /&gt;
==Commonly Used Functions (Mechanics) ==&lt;br /&gt;
Many Vpython assignments require the creation of functions that can calculate Momentum,impulse,velocity Position&lt;br /&gt;
Below are some frequently used equations that can be adapted to certain problem sets:&lt;br /&gt;
&lt;br /&gt;
===Momentum===&lt;br /&gt;
&lt;br /&gt;
 def returnMomentum(vparticle,mparticle):&lt;br /&gt;
    momentum = vector(0,0,0)&lt;br /&gt;
    return vparticle*mparticle + momentum&lt;br /&gt;
&lt;br /&gt;
 momentum = returnMomentum(vparticle,mparticle)&lt;br /&gt;
&lt;br /&gt;
===impulse===&lt;br /&gt;
&lt;br /&gt;
 # deltat = timestep in simulation. &lt;br /&gt;
&lt;br /&gt;
 def returnImpulse(momentum,deltat):&lt;br /&gt;
    impulse = vector(0,0,0)&lt;br /&gt;
    return momentum*deltat&lt;br /&gt;
 impulse = returnImpulse(momentum,deltat)&lt;br /&gt;
&lt;br /&gt;
===velocity===&lt;br /&gt;
&lt;br /&gt;
 def returnVelocity(impulse,mparticle,velocity):&lt;br /&gt;
    velocity = vector(0,0,0)&lt;br /&gt;
    velocity = (impulse/mparticle)+velocity&lt;br /&gt;
    return velocity&lt;br /&gt;
&lt;br /&gt;
 velocity = returnVelocity(impulse,mparticle,velocity)&lt;br /&gt;
&lt;br /&gt;
===position===&lt;br /&gt;
&lt;br /&gt;
 def returnPosition(position, velocity, deltat):&lt;br /&gt;
    position = vector(0,0,0)&lt;br /&gt;
    position = position + velocity*deltat&lt;br /&gt;
    return position&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>Andrewhennessy</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29323</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29323"/>
		<updated>2017-11-23T19:29:49Z</updated>

		<summary type="html">&lt;p&gt;Andrewhennessy: /* Commonly Used Functions (Mechanics) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Claimed By Andrew Hennessy (Fall 2017)&lt;br /&gt;
&lt;br /&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;
==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;
&lt;br /&gt;
&lt;br /&gt;
==Commonly Used Functions (Mechanics) ==&lt;br /&gt;
Many Vpython assignments require the creation of functions that can calculate Momentum,impulse,velocity Position&lt;br /&gt;
Below are some frequently used equations that can be adapted to certain problem sets:&lt;br /&gt;
&lt;br /&gt;
===Momentum===&lt;br /&gt;
&lt;br /&gt;
 def returnMomentum(vparticle,mparticle):&lt;br /&gt;
    momentum = vector(0,0,0)&lt;br /&gt;
    return vparticle*mparticle + momentum&lt;br /&gt;
&lt;br /&gt;
 momentum = returnMomentum(vparticle,mparticle)&lt;br /&gt;
&lt;br /&gt;
===impulse===&lt;br /&gt;
&lt;br /&gt;
 # deltat = timestep in simulation. &lt;br /&gt;
&lt;br /&gt;
 def returnImpulse(momentum,deltat):&lt;br /&gt;
    impulse = vector(0,0,0)&lt;br /&gt;
    return momentum*deltat&lt;br /&gt;
&lt;br /&gt;
 impulse = returnImpulse(momentum,deltat)&lt;br /&gt;
&lt;br /&gt;
===velocity===&lt;br /&gt;
&lt;br /&gt;
 def returnVelocity(impulse,mparticle,velocity):&lt;br /&gt;
    velocity = vector(0,0,0)&lt;br /&gt;
    velocity = (impulse/mparticle)+velocity&lt;br /&gt;
    return velocity&lt;br /&gt;
&lt;br /&gt;
 velocity = returnVelocity(impulse,mparticle,velocity)&lt;br /&gt;
&lt;br /&gt;
===position===&lt;br /&gt;
&lt;br /&gt;
 def returnPosition(position, velocity, deltat):&lt;br /&gt;
    position = vector(0,0,0)&lt;br /&gt;
    position = position + velocity*deltat&lt;br /&gt;
    return position&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>Andrewhennessy</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29322</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29322"/>
		<updated>2017-11-23T19:28:28Z</updated>

		<summary type="html">&lt;p&gt;Andrewhennessy: /* Commonly Used Functions (Mechanics) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Claimed By Andrew Hennessy (Fall 2017)&lt;br /&gt;
&lt;br /&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;
==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;
&lt;br /&gt;
&lt;br /&gt;
==Commonly Used Functions (Mechanics) ==&lt;br /&gt;
Many Vpython assignments require the creation of functions that can calculate Momentum,impulse,velocity Position&lt;br /&gt;
Below are some frequently used equations that can be adapted to certain problem sets.&lt;br /&gt;
&lt;br /&gt;
===Momentum===&lt;br /&gt;
&lt;br /&gt;
def returnMomentum(vparticle,mparticle):&lt;br /&gt;
   momentum = vector(0,0,0)&lt;br /&gt;
   return vparticle*mparticle + momentum&lt;br /&gt;
&lt;br /&gt;
momentum = returnMomentum(vparticle,mparticle)&lt;br /&gt;
&lt;br /&gt;
===impulse===&lt;br /&gt;
&lt;br /&gt;
# deltat = timestep in simulation. &lt;br /&gt;
&lt;br /&gt;
def returnImpulse(momentum,deltat):&lt;br /&gt;
   impulse = vector(0,0,0)&lt;br /&gt;
   return momentum*deltat&lt;br /&gt;
&lt;br /&gt;
impulse = returnImpulse(momentum,deltat)&lt;br /&gt;
&lt;br /&gt;
===velocity===&lt;br /&gt;
&lt;br /&gt;
def returnVelocity(impulse,mparticle,velocity):&lt;br /&gt;
   velocity = vector(0,0,0)&lt;br /&gt;
   velocity = (impulse/mparticle)+velocity&lt;br /&gt;
   return velocity&lt;br /&gt;
&lt;br /&gt;
velocity = returnVelocity(impulse,mparticle,velocity)&lt;br /&gt;
&lt;br /&gt;
===position===&lt;br /&gt;
&lt;br /&gt;
def returnPosition(position, velocity, deltat):&lt;br /&gt;
   position = vector(0,0,0)&lt;br /&gt;
   position = position + velocity*deltat&lt;br /&gt;
   return position&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>Andrewhennessy</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29318</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29318"/>
		<updated>2017-11-23T18:56:50Z</updated>

		<summary type="html">&lt;p&gt;Andrewhennessy: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Claimed By Andrew Hennessy (Fall 2017)&lt;br /&gt;
&lt;br /&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;
==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;
&lt;br /&gt;
&lt;br /&gt;
==Commonly Used Functions (Mechanics) ==&lt;br /&gt;
Many Vpython assignments require the creation of functions that can calculate Momentum, Position, Net Force, Energies (Potential &amp;amp; Kinetic), Friction, and drag.&lt;br /&gt;
Below are some frequently used equations that can be adapted to certain problem sets:&lt;br /&gt;
&lt;br /&gt;
def code:&lt;br /&gt;
   hello&lt;br /&gt;
&lt;br /&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;
[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>Andrewhennessy</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29317</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29317"/>
		<updated>2017-11-23T18:51:49Z</updated>

		<summary type="html">&lt;p&gt;Andrewhennessy: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Claimed By Andrew Hennessy (Fall 2017)&lt;br /&gt;
&lt;br /&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;
==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;
==Commonly Used Functions (Mechanics) ==&lt;br /&gt;
&lt;br /&gt;
Insert Here&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>Andrewhennessy</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29316</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29316"/>
		<updated>2017-11-23T18:49:33Z</updated>

		<summary type="html">&lt;p&gt;Andrewhennessy: /* cross(A, B) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Claimed By Andrew Hennessy (Fall 2017)&lt;br /&gt;
&lt;br /&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;
==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>Andrewhennessy</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29315</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29315"/>
		<updated>2017-11-23T18:48:49Z</updated>

		<summary type="html">&lt;p&gt;Andrewhennessy: /* cross(A, B) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Claimed By Andrew Hennessy (Fall 2017)&lt;br /&gt;
&lt;br /&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 = &amp;lt;exVector_y * exVector2_z - exVector_z * exVector2_y,exVector_z * exVector2_x - exVector_x * exVector2_z,exVector_x * exVector2_y - exVector_y * exVector2_x&amp;gt;&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;
==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>Andrewhennessy</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29224</id>
		<title>VPython Functions</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_Functions&amp;diff=29224"/>
		<updated>2017-11-08T22:33:00Z</updated>

		<summary type="html">&lt;p&gt;Andrewhennessy: Claimed&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Claimed By Andrew Hennessy (Fall 2017)&lt;br /&gt;
&lt;br /&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>Andrewhennessy</name></author>
	</entry>
</feed>