The return Statement © 2018 Kris Jordan.

Slides:



Advertisements
Similar presentations
The if-else Statements
Advertisements

Macro Processor.
CS0004: Introduction to Programming Repetition – Do Loops.
1 Lecture 8:Control Structures I (Selection) (cont.) Introduction to Computer Science Spring 2006.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
1 MATERI PENDUKUNG JUMP Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
IF-ELSE IF-ELSE STATEMENT SWITCH-CASE STATEMENT Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
CPS120: Introduction to Computer Science Decision Making in Programs.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Inequalities and their Graphs Objective: To write and graph simple inequalities with one variable.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create.
Inequalities and their Graphs Objective: To write and graph simple inequalities with one variable.
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
Chapter 6: Loops.
COMP 14 Introduction to Programming
COMP 170 – Introduction to Object Oriented Programming
Repetition Structures Chapter 9
Loops in Java.
Statements (6 of 6) A statement causes an action to be performed by the program. It translates directly into one or more executable computer instructions.
FIGURE 4-10 Function Return Statements
EGR 2261 Unit 4 Control Structures I: Selection
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Boolean Expressions and If
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Scripts & Functions Scripts and functions are contained in .m-files
Control Structures.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Conditionals & Boolean Expressions
While Loops.
Looping and Repetition
FIGURE 4-10 Function Return Statements
Conditionals & Boolean Expressions
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
IF if (condition) { Process… }
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.
Introduction to Object-Oriented Programming with Java--Wu
Iteration Implemented through loop constructs (e.g., in C++)
EVALUATING EXPRESSIONS
Pages:51-59 Section: Control1 : decisions
Three Special Structures – Case, Do While, and Do Until
Evaluating Expressions
FIGURE 4-10 Function Return Statements
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Chapter 4: Control Structures I (Selection)
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.
Program Flow.
Selection Structures: Single-alternative
Pages:51-59 Section: Control1 : decisions
FIGURE 4-10 Function Return Statements
Chapter 3: Selection Structures: Making Decisions
ICS103: Programming in C 4: Selection Structures
while Loops Looping Control Statement
Functions Overview © 2018 Kris Jordan.
if-then-else Conditional Control Statement
Parameters and Arguments
The for Loop © 2018 Kris Jordan.
Flow Control I Branching and Looping.
Presentation transcript:

The return Statement © 2018 Kris Jordan

The return Statement return <expression>; General form: Every function with a return type must have at least one return statement The returned expression's data type must match the return type of its function // Function Definition let max = (x: number, y: number): number => { if (x > y) { return x; } else { return y; } };

The return Statement IMPORTANT: As the processor is evaluating a function call, when it reaches any return statement in the function definition, then the call is complete. The computer evaluates the expression and returns the value immediately to its bookmark. The rest of the function is ignored, skipped over, and not processed. This is ALWAYS, ALWAYS, ALWAYS true!

Return Semantics: Consider the following function Consider an alternate implementation of the max function Is it still correct? What happens when a is greater? let max = (a: number, b: number): number => { if (a > b) { return a; } return b; };

Returning from a function let result: number; result = max(10, 5); 1 let max = (a: number, b: number): number => { if (a > b) { return a; } return b; }; 2 4 3 The max function is called with arguments: 10, 5 The processor jumps to max function. if (a > b) evaluates to true, enters then block return Statement encountered. Expression a evaluates to 10. The function call is complete and this value is returned to step 4. Processor jumps back to bookmark it left at #1 and "max(10, 5)" evaluates to 10. Parameters a 10 b 5

Every function call returns only one value A function definition may have many return statements, however, for any given call only one return statement will be evaluated A function may contain a return statement inside of a loop, however, as soon as return is first encountered it will stop and return immediately Generally: as soon as the computer reaches any return statement within a function, the function call is complete and its job is done.