Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS005 Introduction to Programming

Similar presentations


Presentation on theme: "CS005 Introduction to Programming"— Presentation transcript:

1 CS005 Introduction to Programming
Matlab Eamonn Keogh

2 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..

3 In the black box view of max it is always expecting two input arguments, and to return one number.
So we need to write a function that is expects three input arguments, and to return one number. Actually, MyMax is not a good name, it is not descriptive enough. Let is use MaxOfThreeNumbers 1 7 max 10 1 7 2 MyMax 7

4 The name of the function is MaxOfThreeNumbers
We need names for the three input arguments Let us use Number1, Number2, Number3 We need a name for the returned variable, let us call it ReturnedNum 1 7 2 MaxOfThreeNumbers 7

5 7 1 2 MaxOfThreeNumbers ReturnedNum 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 Number1 ‘7’ gets bound to Number2 ‘2’ gets bound to Number3 Some calculations happen … And the answer (in this case 7) gets assigned to ReturnedNum 1 7 2 MaxOfThreeNumbers Number1, Number2, Number3 ReturnedNum 7

6 MaxOfThreeNumbers 2 HisAge 1 ReturnedNum 19 Here:
‘1’ gets bound to Number1 HisAge gets bound to Number2 ‘3’ gets bound to Number3 Some calculations happen … And the answer (in this case 19) gets assigned to ReturnedNum MaxOfThreeNumbers 2 19 HisAge 1 Number1, Number2, Number3 ReturnedNum

7 We begin by opening an editable file, just like we did for scripts…

8 MaxOfThreeNumbers Number1, Number2, Number3 ReturnedNum See how the first line of our function maps onto our black box view of a function.

9 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.

10 The real code I Here is one solution

11 The real code II Here is another solution

12

13 Testing the Code This version of the code has a bug! Can you see it?

14 The buggy code passes this test
And it passes this test. But it fails this test. We must test our code!

15 Matlab did try to warn us that the code was buggy
However, there will always be bugs that Matlab cannot catch.

16 HomeWork 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.

17 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}?

18 Programming Requires Cleverness
Let us add the three numbers = 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..

19 Programming Requires Cleverness
Let us add the three numbers = 10 Now subtract the max number, which was – 7 = 3 Now subtract the min number, which was = 2 And 2 was the middle number..

20 Programming Requires Cleverness


Download ppt "CS005 Introduction to Programming"

Similar presentations


Ads by Google