Functions! Objectives! 1E3! Topic 9! programming! n This topic should allow students to! n Read chapter 6 of the textbook now.!

Similar documents
Review. Modules. CS 151 Review #6. Sample Program 6.1a:

Functions that Return a Value. Approximate completion time Pre-lab Reading Assignment 20 min. 92

Control Structures: The IF statement!

Array Elements as Function Parameters

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

Topics: Material through example 19 (types, operators, expressions, functions, selection, loops, arrays)

6 Functions. 6.1 Focus on Software Engineering: Modular Programming TOPICS. CONCEPT: A program may be broken up into manageable functions.

Programming in C++ The manager of a company. Lecture Notes 6. Functions (Procedures) 4/24/2018. he he he. Does Does Does

Slide Set 1. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Help File on C++ and Programming at CSUSM by Rika Yoshii

BITG 1113: Function (Part 2) LECTURE 5

Computer Engineering 1 (1E3)

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

LAB: INTRODUCTION TO FUNCTIONS IN C++

Chapter 4: Subprograms Functions for Problem Solving. Mr. Dave Clausen La Cañada High School

Arrays: Higher Dimensional Arrays. CS0007: Introduction to Computer Programming

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Lab 6 Vectors and functions

Over and Over Again GEEN163

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. CS 5301 Spring 2018

Quiz Determine the output of the following program:

Study Guide for Test 2

CIS220 In Class/Lab 1: Due Sunday night at midnight. Submit all files through Canvas (25 pts)

Programming Language. Functions. Eng. Anis Nazer First Semester

Functions, Arrays & Structs

Memory and Pointers written by Cathy Saxton

Standard Version of Starting Out with C++, 4th Edition. Chapter 6 Functions. Copyright 2003 Scott/Jones Publishing

LAB: WHILE LOOPS IN C++

Computer Programming

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Chapter 6: Functions

More loops Ch

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 6: User-Defined Functions I

C++ Final Exam 2017/2018

Arrays in C++ Instructor: Andy Abreu

CISC220 Lab 2: Due Wed, Sep 26 at Midnight (110 pts)

Functions. CS111 Lab Queens College, CUNY Instructor: Kent Chin

Unit 7. 'while' Loops

8. Functions (II) Control Structures: Arguments passed by value and by reference int x=5, y=3, z; z = addition ( x, y );

Understand Execution of a Program

CSI33 Data Structures

Functions. Function Prototypes. Function prototype is needed if the function call comes before the function definition in the program.

C++ Arrays and Vectors

Computer Programming

Unit 6. Thinking with Functions

Unit 14. Passing Arrays & C++ Strings

More on Func*ons Command Line Arguments CS 16: Solving Problems with Computers I Lecture #8

Computer Programming

! Pass by value: when an argument is passed to a. ! It is implemented using variable initialization. ! Changes to the parameter in the function body

Chapter 4: Subprograms Functions for Problem Solving. Mr. Dave Clausen La Cañada High School

LECTURE 06 FUNCTIONS

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

JAVA GUI PROGRAMMING REVISION TOUR III

Computer Programming

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Algorithms for Arrays Vectors Pointers CS 16: Solving Problems with Computers I Lecture #14

Chapter Five: Functions

static CS106L Spring 2009 Handout #21 May 12, 2009 Introduction

Intermediate Programming & Design (C++) Classes in C++

Global & Local Identifiers

C++ Programming Lecture 11 Functions Part I

CSE143 Exam with answers MIDTERM #1, 1/26/2001 Problem numbering may differ from the test as given.

Chapter 7 Functions. As an example, let us write some code to make a virtual cup of coffee.


Excel Template Instructions for the Glo-Brite Payroll Project (Using Excel 2010 or 2013)

Introduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver)

Fundamentals of Programming CS-110. Lecture 2

Lab Session # 6 Functions. ALQUDS University Department of Computer Engineering

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11

Computing and Statistical Data Analysis Lecture 3

COMP 111 PROGRAMMING I MODULARITY USING FUNCTIONS

(I m not printing out these notes! Take your own.)

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter

REPETITION CONTROL STRUCTURE LOGO

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Review. Relational Operators. The if Statement. CS 151 Review #4

Pointers, Dynamic Data, and Reference Types

Function. Mathematical function and C+ + function. Input: arguments. Output: return value

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Introduction to Programming EC-105. Lecture 2

CMSC 341 Lecture 7 Lists

Review of Important Topics in CS1600. Functions Arrays C-strings

CS201- Introduction to Programming Current Quizzes

ECE G205 Fundamentals of Computer Engineering Fall Exercises in Preparation to the Midterm

CSCE 121 ENGR 112 List of Topics for Exam 1

PIC 10A Objects/Classes

Constructor - example

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Problem 1: Get val soon! (15 marks)

Name. CPTR246 Spring '17 (100 total points) Exam 2

ANSWERS. Birkbeck (University of London) Software and Programming 1 In-class Test Feb Student Name Student Number. Answer all questions

Arrays 2 CS 16: Solving Problems with Computers I Lecture #12

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Subject: Computer Science

1) What of the following sets of values for A, B, C, and D would cause the string "one" to be printed?

1) You want to determine whether time has run out. The following code correctly implements this:

Transcription:

Functions 1E3 Topic 9 9 Functions 1 Objectives n This topic should allow students to n Understand the importance of abstraction in programming n Recognise when a function would be useful. n Design appropriate functions. n Understand how functions are used in C++. n Write and use functions in C++. n Read chapter 6 of the textbook now. 9 Functions 2 1

Writing/Using Functions n The 3 & 3 approach to writing and using functions n The function itself 1. Name: come up with a meaningful/descriptive name for the procedure. 2. Parameters: what the function needs in order to carry out its job 3. Return: what kind of data the function gives back (if any). 9 Functions 3 Writing/Using Functions n The 3 & 3 approach to writing and using functions n The function itself 1. Name: come up with a meaningful/descriptive name for the procedure. 2. Parameters: what the function needs in order to carry out its job 3. Return: what kind of data the function gives back (if any). n Appearing in the Programme 1. Declaring the function: function prototype (what it looks like). 2. Calling the function: using it from inside any other function. 3. Implementing the function body: writing its code (what it does). 9 Functions 4 2

n Example 1: a function that prints out a fixed menu: n The 3: 1. Name: displaymenu 2. Parameters: none therefore, leave the brackets empty 3. Returns: nothing therefore, write void. void displaymenu (); 5 n Example 1: a function that prints out a fixed menu: n The 3: 1. Name: displaymenu 2. Parameters: none therefore, leave the brackets empty 3. Returns: nothing therefore, write void. void displaymenu (); n And 3: 1. Declaration: void displaymenu (); // written before the main(). 2. Call: displaymenu(); //written anywhere inside the main(). 3. Implementation, written after the main() void displaymenu () { cout << Option 1: << endl; cout<< Option 2:. <<endl;.. } 6 3

n Example 2: a function that adds two given numbers and returns the result: int add (int x, int y); 7 n Example 2: a function that adds two given numbers and returns the result: int add (int x, int y); int main () { int a=5, b=2; int sum; //now let s call the function sum = add(a, b); //right-hand side is evaluated and result is stored in left-hand side cout << The sum is: << sum; } 8 4

n Example 2: a function that adds two given numbers and returns the result: int add (int x, int y); int main () { int a=5, b=2; int sum; //now let s call the function sum = add(a, b); //right-hand side is evaluated and result is stored in left-hand side cout << The sum is: << sum; } int add(int x, int y) //you can give any names to the variables. { int total; //note that we did not need to re-declare variables x and y. total = x + y; return total; } 9 Parameters & Return n So far we ve seen two examples: n A function that does not take parameters or return a value; it just performs a certain task straightaway (in C it is called a function, although mathematically speaking it s not really a function ). n A function that takes parameters and does some computation then returns the result. 10 5

Parameters & Return n So far we ve seen two examples: n A function that does not take parameters or return a value; it just performs a certain task straightaway (in C it is called a function, although mathematically speaking it s not really a function ). n A function that takes parameters and does some computation then returns the result. n Q: Can there be a function that takes parameters but does not return a value? Or a function that does not take a parameter but returns a value? 11 Parameters & Return n So far we ve seen two examples: n A function that does not take parameters or return a value; it just performs a certain task straightaway (in C it is called a function, although mathematically speaking it s not really a function ). n A function that takes parameters and does some computation then returns the result. n Q: Can there be a function that takes parameters but does not return a value? Or a function that does not take a parameter but returns a value? n Yes. n Think about it and raise your hand if you have an example. 12 6

Boolean Functions" (functions that return a Boolean value) n Boolean functions are useful for abstracting complex conditions. n bool even (int n); n returns true if n is an even number n Use: " " "bool ok = even (n/2); " if (even (number))... n Defn body: { return (n%2 == 0) };" 9 Functions 13 Another Boolean Function n Declaration: n bool is_a_factor_of (int n, int f); n Use: n if (is_a_factor_of (number, i)) composite = true; n Definition: n bool is_a_factor_of (int n, int f) {return ((n % f) == 0) }; n Useful in testing for primes or perfect numbers 9 Functions 14 7

Parameters and arguments double ppsi (double d, double p) {... area = d/2 * d/2 * PI;" " "return (p / area);} n d and p are formal parameters of the function n In the call " x = ppsi (diameter1, price1) n the values of diameter1 and price1 are the arguments passed to the function. 9 Functions 15 Parameter-passing n Suppose diameter1 is 10 and price1 is 15. n 10 is passed as the first argument, and is the value assigned to the formal parameter d. n 15 is passed as the second argument, and is the value assigned to p. n It s as though the call is" ppsi (10, 15) n Inside the body of the function d and p are like variables whose initial values are 10 and 15 respectively. 9 Functions 16 8

Recap 9 Functions 17 Example n Let s write a payroll program (details on web) n Use a top-down design approach. n i.e. write the overall algorithm first n Worry about the details of individual tasks later. n These individual tasks can be handled by functions. 9 Functions 18 9

Algorithm for payroll n For each record in the file n Read the details n Compute the gross pay n Based on hours and hourly rate n Compute the tax due n Based on gross pay and tax_credit n Compute the net pay n Output the computed details 9 Functions 19 When to use a function n When the task is easy to identify but hard to implement or hard to understand the code. n Whenever you are about to write the same or very similar code a second time n E.g. ppsi formula in our better-value pizza program n See pizzafn.cpp 9 Functions 20 10

n ADVANCED EXERCISE n What does this program print? n What does the function f compute? 9 Functions 21 11