Functions Overview © 2018 Kris Jordan.

Slides:



Advertisements
Similar presentations
Algorithms 10 IST – Topic 6.
Advertisements

Decisions If statements in C.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
ITEC113 Algorithms and Programming Techniques
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Chapter 6: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
CS 201 Functions Debzani Deb.
Chapter 6: User-Defined Functions I
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Flow of Control. 2 Control Structures Control structure: An instruction that determines the order in which other instructions in a program are executed.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Functions.
CPS120: Introduction to Computer Science Lecture 14 Functions.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
1 CS161 Introduction to Computer Science Topic #9.
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.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
1 Overview of Programming Principles of Computers.
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.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
Review Expressions and operators Iteration – while-loop – for-loop.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
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.
Chapter 6: User-Defined Functions I
7 - Programming 7P, Q, R - Testing.
Topic: Functions – Part 2
CSC113: Computer Programming (Theory = 03, Lab = 01)
Java Programming: Guided Learning with Early Objects
JavaScript: Control Statements.
Topics Introduction to Repetition Structures
User-Defined Functions
PL/SQL Scripting in Oracle:
Chapter 10 Programming Fundamentals with JavaScript
Structured Program
Writing Functions.
Computer Science Core Concepts
A LESSON IN LOOPING What is a loop?
Flow of Control.
Topics Introduction to Repetition Structures
The structure of programming
Thinking procedurally
while Loops Looping Control Statement
The main Function, introcs Library, and Prompting
if-then-else Conditional Control Statement
The return Statement © 2018 Kris Jordan.
Parameters and Arguments
The for Loop © 2018 Kris Jordan.
Arrays: Iteration Working through an array algorithmically.
Presentation transcript:

Functions Overview © 2018 Kris Jordan

Function Intuition: How-to Build a House Site Preparation and Grading Foundation Construction Framing Installation of windows and doors Roofing Siding Rough electrical Rough plumbing ... Now you have a house! Pre-build the outside frame in 8- foot sections Stand each 8-foot section of the frame up Insert braces for support Repeat steps 3 and 4 until entire perimeter is complete A Framing "Function" © Kris Jordan

Function Definition Overview Parameters A function definition is a subprogram Parameters are placeholders for inputs the function needs The function body is the algorithm, or sequence of steps, the function will follow when it is used The function body is a block of statements Any statement can be written inside the function body, including if-then-else, while loops, and so on A return statement inside a function body specifies the final value a function results in * Defining a function is like writing down a recipe. The definition has no immediate result. It is not until you call a function or follow a recipe that its steps are actually carried out. Return Statement

Function Call Overview Function Definition Parameters A function call instructs the processor to carry out a function's definition. Arguments are the actual input values. They are assigned to the function definition's parameter placeholders. The processor leaves a bookmark at the function call and jumps into the function definition. When the processor reaches the function's return statement, the returned result is substituted for the function call and the processor jumps back. Function Call (Arguments) Returned Value Return Statement

Example Setup import { print, promptNumber } from "introcs"; export let main = async () => { let a = await promptNumber("a"); let b = await promptNumber("b"); // Function Call let answer = max(a, b); print(answer + " is greatest!"); }; // Function Definition let max = (x: number, y: number): number => { if (x > y) { return x; } else { return y; } main(); Example Setup In VSCode: Start the Development Server View Terminal npm run pull npm start Open the File Explorer Pane Right click on the src folder Select "New folder" Name it: x-functions Right click on the x-functions folder Select "New file" Name it: functions-app.ts In functions-app.ts, write out the code to the right. It has no errors, so review carefully if yours has any. © Kris Jordan

Function Definition Syntax let <name> = (<parameters>): <returnType> => { <function body statements> }; We will define functions outside of the main function, typically following it Like variables, functions can be given a name. Parameters are special variable declarations. They are placeholders for the inputs a function needs. Return type specifies the data type the function will return. Statements in the body block run only when a function is called.

Function Definition Example Return Type Name Parameters let max = (x: number, y: number): number => { if (x > y) { return x; } else { return y; } }; Body Return Statements The max function can be given two number values and will return the larger of the two. © Kris Jordan

Function Call Syntax max(a, b) <name>(<arguments>) Example: <name>(<arguments>) When a function call is encountered the processor drops a bookmark. A function call's data type is its function definition's return type For example: let answer: number = max(a, b); Since the max function's return type is number, a function call to max can be assigned to the number variable answer. When the processor reaches a function call, it follows a set of rules to jump over to the function call with input arguments and return back with a result. We'll explore these rules in depth in upcoming lessons.

What purpose do functions serve? Functions are a fundamental unit of process abstraction Learning to tie your shoe was process abstraction As a child, you struggled to learn the right series of steps Nowadays you can just "tie your shoe" without worrying about each step Defining a function is process abstraction Defining functions takes thoughtful effort to get the right series of steps Once correct, you can reuse your function by "calling" it, without worrying about its steps Functions help you break down and logically organize your programs Functions make it easy to reuse computations or sequences of steps Functions help you avoid repetitive, redundant code © Kris Jordan