Parameters and Arguments

Slides:



Advertisements
Similar presentations
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Advertisements

C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
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.
Example 2.
Miscellaneous topicsCS-2301 B-term Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The C Programming.
Parameters, Arguments, Local Variables, and Scope CSC 1401: Introduction to Programming with Java Week 8 – Lecture 1 Wanda M. Kunkle.
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:
Call-by-Value vs. Call-by-Reference Call-by-value parameters are used for passing information from the calling function to the called function (input parameters).
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
PROGRAMMING Functions. Objectives Understand the importance of modular programming. Know the role of functions within programming. Use functions within.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
Chapter Function Basics CSC1310 Fall Function function (subroutine, procedure)a set of statements different inputs (parameters) outputs In.
CPS120: Introduction to Computer Science Functions.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Learners Support Publications Functions in C++
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
CSE 1301 Lecture 6 Writing Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Computer Science By: Erica Ligons Compound Statement A compound statement- block A compound statement- is a unit of code consisting of zero or more statement.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Chapter 6 Methods Chapter 6 - Methods.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
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.
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
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.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Functions + Overloading + Scope
User-Written Functions
Functions.
Data Type and Function Prepared for CSB210 Pemrograman Berorientasi Objek By Indriani Noor Hapsari, ST, MT Source: study.
Course A201: Introduction to Programming
Chapter 7: User-Defined Functions II
Functions Jay Summet.
Review: Chapter 5: Syntax directed translation
JavaScript: Functions
Function There are two types of Function User Defined Function
Deitel- C:How to Program (5ed)
Functions.
Lecture 14 Writing Classes part 2 Richard Gesick.
Using local variable without initialization is an error.
Starting Out with Java: From Control Structures through Objects
Functions BIS1523 – Lecture 17.
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.
Chapter 4 void Functions
Passing Parameters by value
Nate Brunelle Today: Functions again, Scope
Introduction to Programming
Functions Jay Summet.
Functions Jay Summet.
Introduction to Computing Lecture 08: Functions (Part I)
CS1201: Programming Language 2
Methods/Functions.
Corresponds with Chapter 5
Functions Jay Summet.
while Loops Looping Control Statement
classes and Objects Syntax
Functions Overview © 2018 Kris Jordan.
if-then-else Conditional Control Statement
The return Statement © 2018 Kris Jordan.
The for Loop © 2018 Kris Jordan.
Presentation transcript:

Parameters and Arguments © 2018 Kris Jordan

Introducing Parameters // Function Definition let <name> = (<parameters>): <returnT> => { <statements> }; General Form Parameters allow functions to require additional pieces of information in order to be called Parameters are specified within the parenthesis of function definition Parameters look a lot like variable declarations... because they are! Parameters are local variables to the function. Their names are scoped inside of the function body's block. // Function Definition let max = (x: number, y: number): number => { if (x > y) { return x; } else { return y; } }; Example

What effect does declaring parameters have? // Function Definition let max = (x: number, y: number): number => { if (x > y) { return x; } else { return y; } }; Function Definition Function Call Usage max(3, 4); Incorrect Function Call Usage max(3); Incorrect Function Call Usage max(3, 4, 50); When a function declares parameters, it is declaring: "you must give me these extra pieces of information in order to call me" The function definition on the left says: "in order to call max, you must give me two number values" In the usage to the right, when we call max, we must give it two number values.

Parameters vs Arguments These are arguments. These are parameters. max(3, 4); // Function Definition let max = (x: number, y: number): number => { if (x > y) { return x; } else { return y; } }; Example Arguments are the values we assign to parameters The type of the arguments must match the types of the parameters We couldn't write max("oh","no");

Parameter Passing: Step-by-Step (1 / 3) max(8, 9); // Function Definition let max = (x: number, y: number): number => { if (x > y) { return x; } else { return y; } }; When a function is called… A "bookmark" is dropped at this place in code. We'll come back! The processor finds the function definition with the same name. Error if no match is found! Notice the argument matches the parameters in type (number) and count (2)!

Parameter Passing: Step-by-Step (2 / 3) max(8, 9); // Function Definition let max = (x: number, y: number): number => { x = 8; y = 9; if (x > y) { return x; } else { return y; } }; Argument values are assigned to parameters This happens invisibly when the code is running. You will never see the lines to the right. However, each time a call happens, the processor assigns each argument value to its parameter. This is called "parameter passing" because we are copying arguments from one point in code into another function's block.

Parameter Passing: Step-by-Step (3 / 3) perim(8, 9); // Function Definition let max = (x: number, y: number): number => { x = 8; y = 9; if (x > y) { return x; } else { return y; } }; Finally, the processor then jumps into the function and continues onto the first line of the function body block