JavaScript Modularity. Goals By the end of this lecture, you should … Understand why programmers use modularity. Understand how to create a function in.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
Procedural programming in Java
1 Programmer-Defined Functions Functions allow program modularization Variables declared in function are local variables Only known inside function in.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
CS 106 Introduction to Computer Science I 02 / 25 / 2008 Instructor: Michael Eckmann.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Functions Part I.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Introduction to Methods
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
Chapter 6: Functions.
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn.
C++ for Engineers and Scientists Third Edition
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
CNIT 133 Interactive Web Pags – JavaScript and AJAX Advanced topic - variable.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Functions.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Procedural programming in Java Methods, parameters and return values.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
“Education is a Treasure that follows you everywhere.” – Chines Proverb Methods and Functions.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
Computing Higher – SD Unit - Topic 8 – Procedure and Standard Algorithms P Lynch, St Andrew’s High School Unit 2 Software Development Process Topic.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Unit 3 Lesson 5 Strings and the String Class Mr. Dave Clausen (modifications from the textbook)
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
CS 106 Introduction to Computer Science I 10 / 10 / 2007 Instructor: Michael Eckmann.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
JavaScript Variables. Definition A variable is a "container" for information you want to store. A variable's value can change during the script.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 6 Functions The Tic-Tac-Toe Game. Chapter Content In this chapter you will learn to do the following: 0 Write your own functions 0 Accept values.
CS0004: Introduction to Programming
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Information and Computer Sciences University of Hawaii, Manoa
Chapter 9: Value-Returning Functions
Introduction to Programming
CHAPTER 4 CLIENT SIDE SCRIPTING PART 3 OF 3
Programming Fundamentals Lecture #7 Functions
Function There are two types of Function User Defined Function
Functions CIS 40 – Introduction to Programming in Python
Starting Out with Programming Logic & Design
Chapter 4 void Functions
6 Chapter Functions.
Topics Introduction to Functions Defining and Calling a Function
A function is a group of statements that exist within a program for the purpose of performing a specific task. We can use functions to divide and conquer.
Starting Out with Programming Logic & Design
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Javascript Chapter 19 and 20 5/3/2019.
CPS125.
Scope Rules.
Presentation transcript:

JavaScript Modularity

Goals By the end of this lecture, you should … Understand why programmers use modularity. Understand how to create a function in JavaScript. Understand how to pass values to and from functions.

What is Modularity? Modularity means breaking more complex programming problems into smaller, more manageable blocks. Why should we use modularity in our code? ◦ Eliminates duplicate code ◦ Makes code more understandable ◦ Facilitates code reusability In JavaScript, modularity is handled normally through functions …

Functions Allow us to generate new, more complex commands. Programmers give functions names to identify a function’s block of code. Some functions require values, called parameters, in order to work properly. Sometimes, functions also return values.

Function Rules of Thumb You should group functions together with other functions. You should keep function length fairly short – about the length of 1 screen in your editor. It’s also a good idea to break larger functions into smaller ones so that you have one task per function.

Function Rules of Thumb Like a variable’s name, a function’s name should reflect its purpose. Name your functions to show its purpose. Work to minimize “interrelations” between functions (this is called daisy chaining). Instead, use data to connect functions. A good idea is to call all other functions from a main function.

Parts of a Function A function definition consists of two parts: ◦ A function header, which includes the function’s name and defines the function’s parameters (if the function requires them) ◦ The function’s executable block, which defines what the function will do when other code calls it. ◦ If a function returns a value to the code that calls it, the executable block will also contain a return statement.

Variable Scope Scope refers to the region of a program where we define a variable (Flanagan). JavaScript supports two types of scope: ◦ Global Scope ◦ Local Scope

Global Scope Variables We declare global scope variables outside of functions. Global variable values are available to all of the JavaScript. Global variables exist in memory so long as the JavaScript program executes. Don’t use global scope variables in N341 – I want you to use local scope variables …

Local Scope Variables We declare local scope variables inside a function’s executable block. Local scope variables exist in memory ONLY when a function executes. Local scope variables are available ONLY to the code inside a function’s executable block. Other functions cannot use or see a function’s local scope variables. However, we can pass a local scope variable’s value to another function. Use local scope variables for your labs in N341.

Passing Information To pass information to function, we define function parameters. Functions can have no parameters or they can can have any number of parameters. We declare parameters using a comma-separated list after a function’s name. We can re-use parameters each time our program calls a function. Parameters have implicit data types; we don’t need to explicitly type a parameter when we declare it.

Writing a Function - Step 1 Before coding, ask yourself the following: ◦ What will the function do? ◦ Will the function need to get any values from the code that calls it? How many parameters will the function need? ◦ Will the function need to return values to the code that calls it?

Writing a Function - Step 2 Start with the reserved word function ◦ This prepares JavaScript for a function – it says “Get ready, here comes a function.” function

Writing a Function - Step 3 Give the function a meaningful name ◦ The function’s name should somewhat describe what the function will do function SayHi

Writing a Function - Step 4 Establish function parameters (if needed) ◦ Give parameters names that describe what they are going to do. ◦ Separate multiple parameters with commas. function SayHi(fName,lName)

Writing a Function - Step 5 Write the function’s executable block: ◦ Start with a “{” and code any statements that the function will execute. ◦ Use function parameters like variables. function SayHi(fName,lName) { var strMsg = new String(“”); strMsg = “Hello, ”; strMsg += fName + “ ” + lName; strMsg += “!”; Local Variable Parameters

Writing a Function - Step 6 If needed, return a value to the code that called the function. ◦ Use the reserved word return to return a value. function SayHi(fName,lName) { var strMsg = new String(“”); strMsg = “Hello, ”; strMsg += fName + “ ” + lName; strMsg += “!”; return strMsg;

Writing a Function - Step 7 Close the executable block with a “}” ◦ This tells JavaScript that we are done with the function’s definition. function SayHi(fName,lName) { var strMsg = new String(“”); strMsg = “Hello, ”; strMsg += fName + “ ” + lName; strMsg += “!”; return strMsg; }

Calling a Function When code calls a function, control of the program is transferred to that function. When the function finishes, the program will execute the next line in code after the function call. We can call a function in two ways: ◦ If we don’t expect the function to return a value (or if we don’t want to use the returned value) we can call a function by simply using the function’s name. ◦ If we expect the function to return a value, we need to assign that value to a variable. We can do that by assigning the function’s name to the variable.

Calling a Function – Example 1 The function doesn’t return value (or we discard it). The function doesn’t have explicit parameters. SayHi();

Calling a Function – Example 2 The function doesn’t return value (or we discard it). The function has explicit parameters, that we need to send it. SayHi(strFirst, strLast); This code sends the variable values for strFirst & strLast to fName and lName, respectively.

Calling a Function – Example 3 The function returns a value. The function doesn’t have explicit parameters. strMyMsg = SayHi();

Calling a Function – Example 4 The function returns a value. The function has explicit parameters, that we need to send it. strMsg = SayHi(strFirst, strLast);

Questions?

Function for Call Example 1 function SayHi() { var strMsg = new String(“”); strMsg = “Hello World!”; window.alert(strMsg); }

Function for Call Example 2 function SayHi(fName,lName) { var strMsg = new String(“”); strMsg = “Hello, ”; strMsg += fName + “ ” + lName; strMsg += “!”; window.alert(strMsg); } strFirst strLast

Function for Call Example 3 function SayHi() { var strMsg = new String(“”); strMsg = “Hello world!”; return strMsg; }

Function for Call Example 4 function SayHi(fName,lName) { var strMsg = new String(“”); strMsg = “Hello, ”; strMsg += fName + “ ” + lName; strMsg += “!”; return strMsg; } strFirst strLast