Functions, Part 2 of 2 Topics Functions That Return a Value

Slides:



Advertisements
Similar presentations
Subroutines – parameter passing passing data to/from a subroutine can be done through the parameters and through the return value of a function subroutine.
Advertisements

1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Lab 8 User Defined Function.
Chapter 5 ( ) of Programming Languages by Ravi Sethi
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging.
Chapter 6: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
Chapter 6: User-Defined Functions I
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Local Variables A local variable is a variable that is declared within a method declaration. Local variables are accessible only from the method in which.
Topic 3 – The General Form of a C Program. CISC 105 – Topic 3 The General Form of a C Program Now, all of the basic building blocks of a C program are.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
CMSC 1041 Functions II Functions that return a value.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
4.3 Functions. Functions Last class we talked about the idea and organization of a function. Today we talk about how to program them.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Functions + Overloading + Scope
UMBC CMSC 104 – Section 01, Fall 2016
Chapter 6: User-Defined Functions I
Functions, Part 2 of 2 Topics Functions That Return a Value
Chapter 10: Void Functions
FIGURE 4-10 Function Return Statements
Functions Comp 205 Fall ‘17.
Programming the Web using XHTML and JavaScript
Chapter 5 Functions DDC 2133 Programming II.
User-Defined Functions
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Using local variable without initialization is an error.
FIGURE 4-10 Function Return Statements
Heterogeneous aggregate datatypes
4-2 Functions in C In C, the idea of top–down design is done using functions. A C program is made of one or more functions, one and only one of which.
Javascript: variables and parameters
CS150 Introduction to Computer Science 1
Functions, Part 2 of 3 Topics Functions That Return a Value
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Subroutines – parameter passing
Introduction to C Topics Compilation Using the gcc Compiler
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Pascal Subprogram Procedure Function Build in Function (e.g. Sin(x))
FUNCTION CSC128.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
FIGURE 4-10 Function Return Statements
Arrays Topics Definition of a Data Structure Definition of an Array
Chapter 6: User-Defined Functions I
Chapter 9: Value-Returning Functions
Introduction to C Topics Compilation Using the gcc Compiler
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Brent M. Dingle Texas A&M University Chapter 5 – Section 2-3
FIGURE 4-10 Function Return Statements
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Chapter 10: Void Functions
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays Topics Definition of a Data Structure Definition of an Array
Functions, Part 2 of 3 Topics Functions That Return a Value
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Functions, Part 2 of 42 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Functions that return a value
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Functions, Part 1 of 2 Topics Using Predefined Functions
Presentation transcript:

Functions, Part 2 of 2 Topics Functions That Return a Value Parameter Passing Local Variables

Functions Can Return Values /***************************************************************** ** AverageTwo - calculates and returns the average of two numbers ** Inputs: num1 - a number ** num2 - a number ** Outputs: the average of num1 and num2 *****************************************************************/ function AverageTwo (num1, num2) { var average; /* average of the two numbers */ average = (num1 + num2) / 2; return average; }

Using AverageTwo <head> <title>AverageTwo Example</title> <script type="text/javascript"> <!-- function AverageTwo(num1, num2) { var average; average = (num1 + num2) / 2; return average; } //--> </script> </head> <body> var ave, value1 = 5, value2 = 8; ave = AverageTwo(value1, value2); alert("The average is " + ave); </body> 13

Parameter Passing Actual parameters are the parameters that appear in the function call. average = AverageTwo (value1, value2) ; Formal parameters are the parameters that appear in the function header. function AverageTwo (num1, num2) Actual and formal parameters are matched by position. Each formal parameter receives the value of its corresponding actual parameter.

Parameter Passing Corresponding actual and formal parameters do not have to have the same name, but they may.

Local Variables Functions only “see” (have access to) their own local variables. Formal parameters are declarations of local variables. The values passed are assigned to those variables. Other local variables can be declared within the function body.

Parameter Passing and Local Variables <head> <title>AverageTwo Example</title> <script type="text/javascript"> <!-- function AverageTwo(num1, num2) { var average; average = (num1 + num2) / 2; return average; } //--> </script> </head> <body> <script type="text/javascript"> <!-- var ave, value1 = 5, value2 = 8; ave = AverageTwo(value1, value2); alert("The average is " + ave); //--> </script> </body> 5 8 num1 num2 average value1 value2 ave

Same Name, Still Different Memory Locations <head> <title>AverageTwo Example</title> <script type="text/javascript"> <!-- function AverageTwo(num1, num2) { var average; average = (num1 + num2) / 2; return average; } //--> </script> </head> <body> <script type="text/javascript"> <!-- var average, num1 = 5, num2 = 8; average = AverageTwo(num1, num2); alert("The average is " + average); //--> </script> </body> 5 8 num1 num2 average num1 num2 average

Changes to Local Variables Do NOT Change Other Variables with the Same Name <head> <title>AddOne Example</title> <script type="text/javascript"> <!-- function AddOne(num1) { num1 = num1 + 1; alert("In AddOne: num1 = " + num1); } //--> </script> </head> <body> <script type="text/javascript"> <!-- var num1 = 5; AddOne(num1); alert("In the body: num1 = " + num1); //--> </script> </body> num1 num1