Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT.

Similar presentations


Presentation on theme: "CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT."— Presentation transcript:

1 CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT

2 Overview So far we have seen how to define basic mathematical expressions and how to manipulate arrays and matrices Now we will look at script files and functions. They are useful for storing sets of commands and defining them apriori. Also an important step before we get into programming with control and flow operators Today: chapters 4 and 6

3 Scripts Used for writing programs Script variables are global. They can be accessed and modified from anywhere

4 Creating scripts

5 Editor for scripts

6 Running script

7 Output of script

8 Script for mean of three numbers

9 Output of mean script

10 fprintf command A powerful command General syntax fprintf(‘text as string %-5.2f additional text’, variable name) Formatting elements

11 Updated mean script

12 Output of updated mean script

13 Writing to a file fid = fopen(‘file_name’, ‘permission’) File permissions –r = open for reading –w = open for writing (if file exists then content is deleted) –a = open for appending

14 Mean script output to file

15

16 Output file of mean script

17 Function files Function File Input data Output

18 Function syntax function [output arguments] = function_name(input arguments) Example functions: function [A] = RectArea(x, y) function [V, S] = SphereVolArea(r)

19 Local and global variables So far ALL variables we have worked with have been global. This means they can be accessed and modified by any part of the program In functions variables are local by default (and global if they are defined as such)

20 Memory space Global memory space accessible by all scripts and functions Local space for function A Local space for function B

21 Defining and calling a function A function file must be saved before it can be used. The name of the file must be the same as the function name Examples: –FunctionFilename –function [A] = RectArea(x,y)RecArea.m –function[V, S] = SphereVolArea(r) SphereVolArea.m

22 Function for computing area of sphere

23 Area of sphere It won’t run if you click on the run button

24 Area of sphere The function is called from the command line…

25 Area of sphere Or the function can be called from a script.

26 Area of sphere Function output from script

27 Comparison between functions and scripts Both scripts and funtion files are saved with.m extension The first line in a function file is the definition line Function variables are local whereas script ones are global Function files can accept data through input arguments and can return data similarly When a function file is saved, the name of the file should be the same as the function’s.

28 Inline functions name = inline(‘math expression typed as a string’) name = inline(‘math expression’, arg1, …, argn) For example, double = inline(‘2*x’) defines a function that doubles the input. So double(10) = 20

29 Using inline to define area of sphere


Download ppt "CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT."

Similar presentations


Ads by Google