Download presentation
Presentation is loading. Please wait.
1
CS005 Introduction to Programming
Matlab Eamonn Keogh
2
Review: Using Built-In Functions
We can use some of Matlabs built-in functions to do calculations Here sqrt is a function 9 is a parameter argument input argument 3 was the returned value result output Here we say that sqrt was called invoked EDU>> sqrt(9) ans = 3
3
Review: Using Built-In Functions
We sometimes talk about a black box view of a function EDU>> sqrt(9) ans = 3 9 3
4
We can write our own Functions!
The process is very similar to how we wrote scripts However we need to tell matlab what are our Input parameter(s) (can be zero, one, two …) Returned value (Unlike most languages, matlab allows multiple returned values, we will not be doing this in this class) 9 3
5
Designing Functions A name for the function
Let us imagine we want to make a function that takes in three numbers and returns their sum. We need A name for the function This name should be descriptive It should not be currently used The naming rules are the same for variable names Let us use SumOfThreeNums 1 7 2 10
6
Designing Functions A list of input variable names
Let us imagine we want to make a function that takes in three numbers and returns their sum. We need A list of input variable names These name should be descriptive It should not be currently used The naming rules are the same for normal variable names Let us use Number1, Number2, Number3 1 7 2 10
7
Designing Functions A name for the returned value variable
Let us imagine we want to make a function that takes in three numbers and returns their sum. We need A name for the returned value variable These name should be descriptive It should not be currently used The naming rules are the same for normal variable names Let us use ReturnedSum 1 7 2 10
8
We will be able to use the function just like any built in function.
1 7 2 Number1, Number2, Number3 SumOfThreeNums ReturnedSum 10
9
We begin by opening an editable file, just like we did for scripts…
10
The first word must be function, matlab will highlight it in blue.
The second word is our chosen return variable name; ReturnedSum Next comes the assignment operator = Then our function name; SumOfThreeNums Next, we have, in parenthesis, a list of our input arguments; Number1, Number2, Number3 2 10 7 1 ReturnedSum Number1, Number2, Number3 SumOfThreeNums
11
Now we can write the actual code that does the work of the function.
In this case it is a single line, more generally it could be hundreds of lines or even more. It is critical that the last line of our function, contains the return value variable being assigned some value (i.e, on the LHS of a = sign) (This does not have to be true, but it is a very good idea in 99.99% of code, so we will insist on it)
12
We need to save the file before we use it…
We should name the file, the same name as the function (Matlab does not insist on this, but doing otherwise can cause huge problems. We will always use the function name for the file name) As with scripts, the file name always ends with .m
13
We can now use the function just like a built in function…
14
EDU>> SumOfThreeNums(1,2,max(1,2))
ans = 5 EDU>> SumOfThreeNums(1, 2, 2 + 4) 9 EDU>> SumOfThreeNums(1,2,SumOfThreeNums(4,2,0))
15
EDU>> StateTax = 17; EDU>> FedTax = 23;
EDU>> CityTax = 102; EDU>> EamonnsTax = SumOfThreeNums(StateTax,FedTax,CityTax) EamonnsTax = 142 EDU>> (values left blank below, lets fill them in during lecture) StateTax EamonnsTax CityTax FedTax 3 inf 23 44 -2
16
Review: we have seen this slide before
What is Eamonn’s BMI, using imperial units? He is 74 inches tall and 220 lbs in weight It is a little different to the metric version, due to rounding
17
Let write a function to get a users BMI
What would be a good name for this function, how about GetBMI ? What are the input arguments? There are none! What is the returned value, it is a real number. Let us call the returned value variable, UsersBMI ? 21.2
18
List of input arguments is empty
Function name and file name are the same The last line of code assigns the return variable some value
19
Minor point: Matlab prefers that you end your function with the word end It makes no difference, we will see why this is later…
20
EDU>> Fink_Nottles_BMI = GetBMI;
How tall are you in inches : 65 How heavy are you in pounds : 134 EDU>> It would also be legal to say EDU>> Fink_Nottles_BMI = GetBMI();
21
Lets write a function to toss a dice
EDU>> EamonnsRoll = TossDice EamonnsRoll = 2 EDU>> Fink_NottlesRoll = TossDice Fink_NottlesRoll = 3
22
Mia This is a very old and ancient, bluffing dice game, played with two dice and a flat bottomed container with a lid, for any number of players. Play: All players start the game with three lives. The first player rolls the dice without revealing what was rolled. This initial player then has three choices of what to announce to the others: Announce truthfully what has been rolled, Announce (by lying) a greater value than that rolled. Announce (by lying) a lesser value than that rolled. The dice values are ranked by multiplying the higher value of the two by 10 and adding the lower value to produce a two digit number. All possible results are ranked in order as follows: 21, 11, 22, 33, 44, 55, 66, 65, 64, 63, 62,61, 54, 53, 52, 51, 43, 42, 41, 32, 31
23
The dice values are ranked by multiplying the higher value of the two by 10 and adding the lower value to produce a two digit number. EDU>> Dice1 = TossDice; EDU>> Dice2 = TossDice; EDU>> EamonnsMaiScore = max(Dice1,Dice2)* min(Dice1,Dice2) EamonnsMaiScore = 54
24
Or we could write a function…
EDU>> Dice1 = TossDice; EDU>> Dice2 = TossDice; EDU>> EamonnsMaiScore = max(Dice1,Dice2)* min(Dice1,Dice2) EamonnsMaiScore = 54 EDU>> EamonnsMaiScore = MiaScore EamonnsMaiScore = 51 EDU>> Note that we have one function we wrote (MiaScore), calling another function we wrote (TossDice)
25
I have not commented my teaching slides for space reasons, but remember that we should always comment our code
26
The red and blue codes below do the exact same thing
Why write functions? The red and blue codes below do the exact same thing EDU>> Dice1 = TossDice; EDU>> Dice2 = TossDice; EDU>> EamonnsMaiScore = max(Dice1,Dice2)* min(Dice1,Dice2); EDU>> BingosMaiScore = max(Dice1,Dice2)* min(Dice1,Dice2); EDU>> GussiesMaiScore = max(Dice1,Dice2)* min(Dice1,Dice2); EDU>> BarmysMaiScore = max(Dice1,Dice2)* min(Dice1,Dice2); EDU>> EamonnsMaiScore = MiaScore; EDU>> BingosMaiScore = MiaScore; EDU>> GussiesMaiScore = MiaScore; EDU>> BarmysMaiScore = MiaScore; 1) Brevity. 2) Less likely to introduce mistakes. 3) Readability. 4) Ease of debugging
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.