David Stotts Computer Science Department UNC Chapel Hill.

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

David Stotts Computer Science Department UNC Chapel Hill.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
CS0004: Introduction to Programming Repetition – Do Loops.
David Stotts Computer Science Department UNC Chapel Hill.
Fall 2008Programming Development Techniques 1 Topic 2 Scheme and Procedures and Processes September 2008.
Chapter 5 Functions.
Computer Science 1620 Loops.
Sub Programs To Solve a Problem, First Make It Simpler.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
CS 201 Functions Debzani Deb.
Loops We have been using loops since week 2, our void draw(){ } is a loop A few drawbacks of draw() –It is endless –There is only one draw() –It updates.
David Stotts Computer Science Department UNC Chapel Hill.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
Python quick start guide
INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Procedures and Functions Computing Module 1. What is modular programming? Most programs written for companies will have thousands of lines of code. Most.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
David Stotts Computer Science Department UNC Chapel Hill.
Keeping it Neat: Functions and JavaScript Source Files Chapter 7.
David Stotts Computer Science Department UNC Chapel Hill.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
CPS120: Introduction to Computer Science Decision Making in Programs.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
David Stotts Computer Science Department UNC Chapel Hill.
CPS120: Introduction to Computer Science Lecture 14 Functions.
JavaScript III Functions and Abstraction. 2 JavaScript so far statements assignment function calls data types numeric string boolean expressions variables.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Mathematical Expressions, Conditional Statements, Control Structures
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
JavaScript, Fourth Edition
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
1 Looping Dale/Weems/Headington. 2 KA/JS/P Warning l Save your work often! l In the Khan Academy, JavaScript environment, infinite loops will lock up.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
MIT-AITI: Functions Defining and Invoking Functions Functions as Data Function Scope: The call Object Function Arguments: The arguments objects Function.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
David Stotts Computer Science Department UNC Chapel Hill.
Expressions and Data Types Professor Robin Burke.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
APS105 Functions (and Pointers) 1. Modularity –Break a program into manageable parts (modules) –Modules interoperate with each other Benefits of modularity:
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
CSC 107 – Programming For Science. Today’s Goal  Write functions that take & return values  How parameters declared and how we call functions  What.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
CS0004: Introduction to Programming
Introduction to JavaScript MIS 3502, Fall 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 9/29/2016.
Introduction to Programming
Learning to Program D is for Digital.
Functions Review.
Principles of programming languages 4: Parameter passing, Scope rules
CMPT 201 Functions.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Scripts & Functions Scripts and functions are contained in .m-files
User-Defined Functions
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Functions BIS1523 – Lecture 17.
T. Jumana Abu Shmais – AOU - Riyadh
Programming in JavaScript
Selection Statements.
Programming in JavaScript
CPS125.
Top-Down Design with Functions
Presentation transcript:

David Stotts Computer Science Department UNC Chapel Hill

0. data (types, simple information) 1. data storage (variables, assignment) 2. data retrieval (expressions, evaluation) 3. repetition (loops) 4. decision making (conditionals) 5. procedure abstraction (functions) 6. data abstraction (arrays) 7. objects: all-the-above, wrapped up

Named Functions  Sometimes we have a block of statement we need to execute at several different places in our program  We would like to avoid duplicating the code block… no cut and paste Principle: write the code lines once, refer to it as many times as you need

We have already seen this at work, and used it ◦ Math.floor(speed); ◦ Math.sqrt(num); ◦ prompt( “what is the number?” ) ; ◦ alert( “well done !! ” ); Someone else wrote the JavaScript code that computes square roots They wrapped it up in a way that lets you make it execute and work for you when you need it

One common use for functions is the traditional mathematical entity y = f(x) “black box” view, function turns input values into output values The inside of the box is the code you write for the function, the function body argument Return value (domain element) (range element)

You can “wrap up” your own code: you write functions, they are like named mini programs It helps to organize your code into smaller chunks rather than one long huge pile of statements You give a collection of statements a name, and then cause those statements to execute by referring to the name

calling a function is making the function code execute to produce its results You write the function body code once You call it as many times as you need to get results We say a function returns the result it computes A function call is an expression It evaluates to the result the function returns A call can appear anywhere an expression can… assignment, arithmetic, alert, conditions

Arguments are like “program” input for a function Return value is like output var num, x = 47.3; num = sqrt ( x ); Arguments: pass values into a function for use during execution Return value: function passes out a value when it ends a function call, an expression, return value is assigned to num

Functions are usually called using both arguments and return values… however, they are optional Sometimes we have occasion to write/call a function that has no arguments Sometimes we have occasion to write/call a function that has no return value Sometimes we call a function that returns a value but we choose to ignore it, not use it

function myProg ( ) { var x = 5; var xcube; xcube = helper(x); alert(“the result is “ + xcube ); } function helper ( num ) { var result = num ^ 3; return result; } Function definition Function call Makes this execute

function myProg ( ) { var x = 5; var xcube; xcube = helper(x); alert(“the result is “ + xcube ); } myProg(); Function definition Function call Makes this execute

Think of your program as  a collection of function definitions  one lonely call to make a function begin running We will write that “first function” as function myMain ( ) {... } This is just my style for this class, so all our programs have some consistency and similarity There are many ways to structure JavaScript programs

function myMain ( ) { calls helper( ) calls validate( ) } function validate ( ) {... } function helper ( ) { calls isInt( ) } function isInt ( ) {... } myMain( ); the lonely first function call

This call to the “first function” gets the whole snowball rolling downhill function myMain ( ) { calls helper( ) calls validate( ) } function isInt ( ) {... } myMain( ); function helper ( ) { calls isInt( ) } function validate ( ) {... }

function myProg ( ) { var x = 5; var xcube; xcube = helper(x); alert(“the result is “ + xcube ); } function helper ( num ) { var result = num ^ 3; return result; } For this call, we are computing 5 ^ 3 since 5 is passed is as the value for “num” 125 is sent back as the return value, put into “xcube”

function myProg ( ) { var x = 9; var xcube; xcube = helper(x); alert(“the result is “ + xcube ); } function helper ( num ) { var result = num ^ 3; return result; } For this call, we are computing 9 ^ 3 since 9 is passed is as the value for “num” 729 is sent back as the return value, put into “xcube”

Code examples Show no parameters ◦ input prompting Show return values ◦ User input data validation Show parameters passed in Show scope rules

Scope of a name : the part of the program where that name can be seen and used (assigned to, read from, called) JavaScript has global scope and local scope We will use global scope carefully for now

 Local Scope is basically all the names created inside a function  Arguments are variables local to a function  var declarations inside the function are local to that function  Functions can be declared inside a function… they are local

Anything declared local to a function  can be seen and used by code inside that function body  cannot be seen or used by any code outside that function

var gx = 12; var count = 0; function myMain ( ) {... } function helper ( num ) {... } We say that the “top level functions” are declared at the global level Turns out we can declare variables at the global level too Global variables can be seen in all functions return num * gx ;

function myMain( ) { var y = 5; var result; result = helper ( y ) ; } function helper ( num ) { var x = 7; alert( y ); // illegal // y in myMain is not visible return num*x; } Why can the name “helper” be seen and used (called) inside “myMain” ? A mystery… Function “helper” is not declared inside myMain…

For now, don’t use global variables  I want you to get used to passing arguments to functions, and to do it well  Using global variables can create conflicts when developing code modules as a team We will be using the global scope level for top level function names

var aNumber = 100; tweak( ); function tweak( ) { // This prints "undefined", because aNumber is // also defined locally below. alert(aNumber); if (false) { var aNumber = 123; } So don’t so this… it causes confusion Declare variables up top

var aNumber = 100; tweak( ); function tweak( ) { // This prints "undefined", because aNumber is // also defined locally below. var aNumber; alert(aNumber); if (false) { aNumber = 123; } In this form, you can see why it prints undefined

 Declare your variables !  AT THE TOP OF FUNCTIONS! no, srsly … declare your variables at the top of functions