From the course: Mathematica 11 Essential Training

Define a function - Mathematica Tutorial

From the course: Mathematica 11 Essential Training

Start my 1-month free trial

Define a function

- [Instructor] Mathematica has an impressively huge colllection of functions that you can use to analyse your data. In this movie I will show you how to create a custom function so that you can have a simply way to perform calculations you need without stringing together combinations of other functions and commands inside of the program. I have created a blank Mathematica notebook so I can go ahead and start creating my function. First I will type in the name of the function. It needs to be all one word, and I'll call it timestwo. I've started the word timestwo with a lower case t and that's because all the built in key words or symbols in Mathematica start with capital letters. The is was I can be reasonably certain I am not duplicating any built in key words. Next I need to provide the arguments that Mathematica should expect for this function so I'll type a left square bracket and then x followed by an underscore. A letter followed by an underscore indicates that it is an argument that will be used in the calculation. Then I'll type a right square bracket then a colon and an equal sign. Now I define the actual operation that is used to generate the function's output. And in this case as the name implies, we're just multiplying values by two. So I'll type x which is my input asterisk two, and shift + enter. When I press shift + enter I see that I have constructed a function. Now I can test the output, so let's say that I want to see the output of seven times two, that would be times two, and you can see it comes up auto complete. Press tab, left square bracket and then the number seven, right square bracket and shift + enter and I get my output of 14. You can also define functions that expect two inputs. So let's say that I want to do exponentiation where I want to raise one number to the power of another number. For that I can type in the name of my function, I'll just call it raise, r-a-i-s-e then a left square bracket. Now I need to indicate the two inputs or arguments that will be used, so I'll have a followed by an underscore, comma b again followed by an underscore and a right square bracket, Now to indicate it's a function I'll type colon and equal. Everything looks good. And I'll define the exponential or exponentiation operation as a carat b. And when I press shift + enter I see that I have created my function. If I want to see what raise does I can just type raise, followed by a left square bracket and then the two arguments. Let's say we'll raise four to the power of seven So I'll type in four comma seven followed by a right square bracket and shift + enter and I get the value of 16,384. If you want to get rid of a function you can use the clear command. So let's say I've decided I don't need to use the raise function because I can simply type in a number raised to the power of another number, so I'll type clear, followed by a left square bracket, raise r-a-i-s-e, right square bracket and shift + enter And now if I try typing in the same function I had before, raise left square bracket, then four comma seven followed by the right square bracket shift + enter you'll see that I just get the text, raise and then four comma seven inside of the brackets. You can also use the clear command to remove values from the list variable. I can be a little confusing that the name of the variable or the function appears in the auto complete list, but don't let that throw you, you really have gotten rid of it.

Contents