29
CIS 101: Computer Programming and Problem Solving Lecture 5 Usman Roshan Department of Computer Science NJIT

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

  • View
    215

  • Download
    1

Embed Size (px)

Citation preview

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

CIS 101: Computer Programming and Problem Solving

Lecture 5Usman Roshan

Department of Computer Science

NJIT

Page 2: CIS 101: Computer Programming and Problem Solving Lecture 5 Usman Roshan Department of Computer Science NJIT

Function files

Function FileInput data Output

Page 3: CIS 101: Computer Programming and Problem Solving Lecture 5 Usman Roshan Department of Computer Science NJIT

Function syntax

function [output arguments] = function_name(input arguments)

Example functions:

function [A] = RectArea(x, y)

function [V, S] = SphereVolArea(r)

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

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)

Page 5: CIS 101: Computer Programming and Problem Solving Lecture 5 Usman Roshan Department of Computer Science NJIT

Memory space

Global memoryspace accessibleby all scripts andfunctions

Local spacefor function A

Local spacefor function B

Page 6: CIS 101: Computer Programming and Problem Solving Lecture 5 Usman Roshan Department of Computer Science NJIT

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:– Function Filename– function [A] = RectArea(x,y) RecArea.m– function[V, S] = SphereVolArea(r) SphereVolArea.m

Page 7: CIS 101: Computer Programming and Problem Solving Lecture 5 Usman Roshan Department of Computer Science NJIT

Function for computing area of sphere

Page 8: CIS 101: Computer Programming and Problem Solving Lecture 5 Usman Roshan Department of Computer Science NJIT

Area of sphere

It won’t run if you click on the run button

Page 9: CIS 101: Computer Programming and Problem Solving Lecture 5 Usman Roshan Department of Computer Science NJIT

Area of sphere

The function is called from the command line…

Page 10: CIS 101: Computer Programming and Problem Solving Lecture 5 Usman Roshan Department of Computer Science NJIT

Area of sphere

Or the function can be called from a script.

Page 11: CIS 101: Computer Programming and Problem Solving Lecture 5 Usman Roshan Department of Computer Science NJIT

Area of sphere

Function output from script

Page 12: CIS 101: Computer Programming and Problem Solving Lecture 5 Usman Roshan Department of Computer Science NJIT

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.

Page 13: CIS 101: Computer Programming and Problem Solving Lecture 5 Usman Roshan Department of Computer Science NJIT

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

Page 14: CIS 101: Computer Programming and Problem Solving Lecture 5 Usman Roshan Department of Computer Science NJIT

Using inline to define area of sphere

Page 15: CIS 101: Computer Programming and Problem Solving Lecture 5 Usman Roshan Department of Computer Science NJIT

Logical operators

• AND: if both are true result is true (1)

• OR: if either is true result is true (1)

• NOT: produces opposite of operand

Page 16: CIS 101: Computer Programming and Problem Solving Lecture 5 Usman Roshan Department of Computer Science NJIT

Conditional statements

if <boolean expression>

<commands>

else

<commands>

end

Page 17: CIS 101: Computer Programming and Problem Solving Lecture 5 Usman Roshan Department of Computer Science NJIT

Max of two numbers

Page 18: CIS 101: Computer Programming and Problem Solving Lecture 5 Usman Roshan Department of Computer Science NJIT

Max of two numbers

Page 19: CIS 101: Computer Programming and Problem Solving Lecture 5 Usman Roshan Department of Computer Science NJIT

Loops

for k = f:s:t

<group of commands>

end

Page 20: CIS 101: Computer Programming and Problem Solving Lecture 5 Usman Roshan Department of Computer Science NJIT

Loops

Page 21: CIS 101: Computer Programming and Problem Solving Lecture 5 Usman Roshan Department of Computer Science NJIT

Loops

Page 22: CIS 101: Computer Programming and Problem Solving Lecture 5 Usman Roshan Department of Computer Science NJIT

Loops

while <conditional expression>

<group of commands>

end

Page 23: CIS 101: Computer Programming and Problem Solving Lecture 5 Usman Roshan Department of Computer Science NJIT

Loops

Page 24: CIS 101: Computer Programming and Problem Solving Lecture 5 Usman Roshan Department of Computer Science NJIT

Loops

Page 25: CIS 101: Computer Programming and Problem Solving Lecture 5 Usman Roshan Department of Computer Science NJIT

Nested loops

for k = 1:n

for h = 1:m

<group of matlab commands>

end

end

Page 26: CIS 101: Computer Programming and Problem Solving Lecture 5 Usman Roshan Department of Computer Science NJIT

Loops

Page 27: CIS 101: Computer Programming and Problem Solving Lecture 5 Usman Roshan Department of Computer Science NJIT

Loops

Page 28: CIS 101: Computer Programming and Problem Solving Lecture 5 Usman Roshan Department of Computer Science NJIT

Loops

• break: terminates execution of loop• continue: goes to next iteration of loopfor k = 1:2:200

<commands>…continue<commands>…

end

stops execution and goes to next iteration

Page 29: CIS 101: Computer Programming and Problem Solving Lecture 5 Usman Roshan Department of Computer Science NJIT

Loops

• break: terminates execution of loop• continue: goes to next iteration of loopfor k = 1:2:200

<commands>…break<commands>…

end

stops execution and exits loop