Josh Schneider [email protected] Who this session is for… “I’ve never programmed on anything, ever; but I have this class set of TINspires…” These guys might benefit too, but some material will be stuff you already know… “I’ve never programmed on a calculator before, but I’ve done some little scripts here and there on my computer.” “I’ve never programmed on the Nspire before, but I have worked on other TI products.” Who will be really bored… “I’ve not only done a ton of TI-Basic programs, I’ve also written a few Apps!” Why Programming? 1. Programming is a passion of mine. Logic is fun! 2. The amount of work to do always increases, while the amount of people to do it almost never does. I feel that truly effective people find ways to automate repetitive tasks in their every day life. 3. For some students, Programming is a tangible way to teach abstract concepts of logic, causality, critical thinking, and even empathy! “Progra– what?” An Algorithm is a list of instructions to accomplish a goal. Think of an Algorithm as a Recipe Ingredients become Variable Declarations Steps become Code Yield becomes Return The digital expression of your Algorithm is a Program. Scripts vs Programs – An Aside A Programming Language must be translated from the language to machine language (a process called Compiling, using a Compiler) before it can be executed. Changes take time, you have to recompile each time you want to run. A Scripting Language is translated from the language to machine language at runtime (Interpreting) using a piece of software called an Interpreter. Changes can be made and tested “on the fly”. Easier to debug. TI BASIC is technically a scripting language. Prgm vs Func In the world of the TI-Nspire… A Program (Prgm) is an expression of an algorithm. Variables can be local or global. Does not return a response… just output. A Function (Func) is an expression of an algorithm that returns a response. Variables must be local. The Function must return a response. Most of the commands you use in the TI-Nspire are Functions. Some Terminology Debugging is the process of stepping through code, finding, and fixing issues. A Block is “container for code”. An example, the Prgm and EndPrgm statements form a Prgm Block. Everything between those statements are treated as a program. A Conditional statement offers boolean criteria (true or false) and effects based on the response, essentially a choice for the program. A Loop allow you to repeat steps. A loop allow you to repeat steps. A loop allow you to repeat steps… Error Terminology A Syntax Error generally means that you used a function or command incorrectly. For instance: Solve(2x=0) would produce a syntax error, since the Solve() command requires an expression, a comma, and then a variable. A Domain Error generally indicates that a calculation is out of the range of the calculator’s capabilities. Logic Errors The beauty of code: It does exactly what you tell it to do. The frustration of code: It does exactly what you tell it to do. Logic Errors are errors in which the commands and functions are used in their proper syntax, but the code doesn’t produce the expected outcome. This is usually because of human errors in planning. These types of issues are common. At some point, every good programmer ends up crawling lines of code looking for an errant line. It’s a rewarding find. The Program Editor So here’s how to get started programming: On the software: 1. Insert a calculator page. 2. Click on 9:Functions & Programs 3. Choose 1:Program Editor 4. Choose 1:New… On the device: 1. Insert a calculator page. 2. Press Menu 3. Choose 9:Functions & Programs 4. Choose 1:Program Editor 5. Choose 1:New… • Once you’re here, start filling in the name of your program, choosing whether it’s a program or a function, and setting its library access. Library Access? LibPriv means that the function or program will be available from any document, but will not display in the catalog. LibPub means that the function or program will be available from any document, and will display in the catalog. None or leaving out LibPriv/LibPub means that the function or program will only be accessible from the current document. (This is the default) You can write your functions or programs in one document using LibPriv or LibPub, and then access those programs or functions from any document! Program #1 – Hello World The “Hello World” program is the most common example of a new programming language. It is the simplest program that can be written, as it requires no input, and simply outputs the text “Hello World”. Define hello()= Prgm Disp “Hello World!” EndPrgm A “Hello World” program is a good way to learn the most basic structure of a programming language. Program #2 – Random List Generator With this program, we’ll spice things up by adding a loop and input. Since we want to return a value (making the result useful outside the program itself), we’ll do this one as a function. Define randgen(a)= Func Local b,i For the loop, I chose a For..EndFor loop since we know how many times we want to run through. For i,1,a,1 b[i]:=RandInt(0,9) EndFor Return b The idea is, we run the function specifying how many elements we want, and the function outputs a list of that many random integers between 0-9. EndFunc Program #3 – Evens or Odds? With this program, we go one step further: we’re adding conditional statements to the loop we already know. This one is being done as a program, but this time we’ll specify an input parameter. Define evenodd(a)= Prgm For i,1,dim(a),1 If mod(a[i],2)=0 Then Disp a[i],” is even.” Else For a conditional, we use If..Then..Else…Endif. The device will evalute the stuff between If and Then, and if it is true, will execute the statements after Then. If the statement is not true, we go to Else, or exit the conditional nothing else is specified. Disp a[i],” is odd.” EndIf EndFor EndPrgm Next Steps Functions are infinitely useful! You can use your functions to manipulate data in Lists and Spreadsheets, on graphs (using the Calculate tool), and in Calculator. Programs are growing more useful thanks to the TINspire 2.0 addition of the Request and RequestStr commands for taking input. Always remember to Check Syntax and Save before testing your creations! Anticipating the User Users will do interesting things you did not intend. Try to keep your users in mind when writing a new program. Add conditional statements to check incoming data, and return useful error messages to the user or limit your program’s response to the data. Questions?