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

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
Programming In C++ Spring Semester 2013 Lecture 4 Programming In C++, Lecture 3 By Umer Rana.
Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 1Winter Quarter MATLAB: Script and.
True or false A variable of type char can hold the value 301. ( F )
CIS 101: Computer Programming and Problem Solving Lecture 8 Usman Roshan Department of Computer Science NJIT.
CIS 101: Computer Programming and Problem Solving Lecture 7 Usman Roshan Department of Computer Science NJIT.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
CIS 101: Computer Programming and Problem Solving Lecture 9 Usman Roshan Department of Computer Science NJIT.
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
General Computer Science for Engineers CISC 106 Lecture 08 Dr. John Cavazos Computer and Information Sciences 2/27/2009.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
C++ for Engineers and Scientists Third Edition
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Control Structures, Operators, and Functions.
CECS 121 EXAM 1. /* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
1 Session 3: Flow Control & Functions iNET Academy Open Source Web Programming.
Introduction to MATLAB Session 3 Simopekka Vänskä, THL Department of Mathematics and Statistics University of Helsinki 2011.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Chapter 05 (Part III) Control Statements: Part II.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
JavaScript, Fourth Edition
Artificial Intelligence Lecture No. 26 Dr. Asad Ali Safi ​ Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Control Structures RepetitionorIterationorLooping Part I.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Digital Image Processing Introduction to M-function Programming.
Working with Loops, Conditional Statements, and Arrays.
General Computer Science for Engineers CISC 106 Lecture 15 Dr. John Cavazos Computer and Information Sciences 03/16/2009.
The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
ENG College of Engineering Engineering Education Innovation Center 1 Functions 2 in MATLAB Topics Covered: 1.Functions in Script Files Inline Functions.
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
Unit – 3 Control structures. Condition Statements 1.If.…..else :- Has someone ever told you, "if you work hard, then you will succeed"? And what happens.
Learning Javascript From Mr Saem
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Lecture 4 CS140 Dick Steflik. Reading Keyboard Input Import java.util.Scanner – A simple text scanner which can parse primitive types and strings using.
G. Pullaiah College of Engineering and Technology
EEE 161 Applied Electromagnetics
Chapter 7 User-Defined Methods.
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
Making Choices with if Statements
Loops in Java.
Python: Control Structures
Introduction to Programming for Mechanical Engineers
Scripts & Functions Scripts and functions are contained in .m-files
Expressions and Control Flow in JavaScript
JavaScript: Control Statements.
Agenda Control Flow Statements Purpose test statement
User-Defined Functions
Introduction to MATLAB
CHAPTER 8 (skip , ) CHAPTER 10
The Linux Command Line Chapter 29
An Introduction to Java – Part I, language basics
The University of Texas – Pan American
Let’s all Repeat Together
CSCI N207 Data Analysis Using Spreadsheet
Programming II Vocabulary Review Loops & functions
Announcements Exercise Sets 2 and 3 Due Thursday.
Presentation transcript:

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

Function files Function File Input data Output

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

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)

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

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

Function for computing area of sphere

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

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

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

Area of sphere Function output from script

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.

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

Using inline to define area of sphere

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

Conditional statements if else end

Max of two numbers

Loops for k = f:s:t … end

Loops

while … end

Loops

Nested loops for k = 1:n for h = 1:m … end

Loops

break: terminates execution of loop continue: goes to next iteration of loop for k = 1:2:200 … continue … end stops execution and goes to next iteration

Loops break: terminates execution of loop continue: goes to next iteration of loop for k = 1:2:200 … break … end stops execution and exits loop