CS 005 Introduction to Programming Matlab Eamonn Keogh
CS 005 Introduction to Programming Matlab Eamonn Keogh eamonn@cs. ucr. edu
More on Designing Functions Let us get more practice using and writing functions We have seen max is a built-in function that can find the largest of two numbers. Does it work for three numbers? No. Let us write our own function that can. .
In the black box view of max it is always expecting two input arguments, and to return one number. 1 7 max 10 So we need to write a function that is expects three input arguments, and to return one number. Actually, My. Max is not a good name, it is not descriptive enough. Let is use Max. Of. Three. Numbers 1 7 My. Max 7 2
The name of the function is Max. Of. Three. Numbers We need names for the three input arguments Let us use Number 1, Number 2, Number 3 We need a name for the returned variable, let us call it Returned. Num 1 7 2 Max. Of. Three. Numbers 7
Above is how we want to be able to use the function (be we have not written it yet) Here: ‘ 1’ gets bound to Number 1 ‘ 7’ gets bound to Number 2 ‘ 2’ gets bound to Number 3 Some calculations happen … 1 7 2 Number 1, Number 2, Number 3 Max. Of. Three. Numbers And the answer (in this case 7) gets assigned to Returned. Num 7
Here: ‘ 1’ gets bound to Number 1 His. Age gets bound to Number 2 ‘ 3’ gets bound to Number 3 Some calculations happen … And the answer (in this case 19) gets assigned to Returned. Num 1 His. Age 2 Number 1, Number 2, Number 3 Max. Of. Three. Numbers Returned. Num 19
We begin by opening an editable file, just like we did for scripts…
See how the first line of our function maps onto our black box view of a function. Number 1, Number 2, Number 3 Max. Of. Three. Numbers Returned. Num
Stub Testing Here is a clever idea. Before we write the real code, we put in a temporary “stub” that just returns say 999. We can now test the function.
The real code I Here is one solution
The real code II Here is another solution
Testing the Code This version of the code has a bug! Can you see it?
The buggy code passes this test And it passes this test. But it fails this test. We must test our code!
Matlab did try to warn us that the code was buggy However, there will always be bugs that Matlab cannot catch.
Home. Work • This will not be collected or graded, but you must do it, before Friday’s lab. • Write a function that finds the min of three numbers. • Write a function that finds the max of four numbers.
Programming Requires Cleverness We have seen how to get the maximum of three numbers, i. e. the 7 from {1, 7, 2} We have seen how to get the minimum of three numbers, i. e. the 1 from {1, 7, 2} How can we get the middle value, i. e. the 2 from {1, 7, 2}?
Programming Requires Cleverness Let us add the three numbers 1 + 7 + 2 = 10 Now subtract the max number, which was 7 10 – 7 = 3 Now subtract the min number, which was 1 3 - 1 = 2 And 2 was the middle number. .
Programming Requires Cleverness Let us add the three numbers 1 + 7 + 2 = 10 Now subtract the max number, which was 7 10 – 7 = 3 Now subtract the min number, which was 1 And 2 was the middle number. . 3 - 1 = 2
Programming Requires Cleverness
- Slides: 20