COMP 111 PROGRAMMING I MODULARITY USING FUNCTIONS

Similar documents
Outline. Functions. Functions. Predefined Functions. Example. Example. Predefined functions User-defined functions Actual parameters Formal parameters

Fundamentals of Programming Session 25

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

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

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

CS115 Principles of Computer Science

Lecture 04 FUNCTIONS AND ARRAYS

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Function Example. Function Definition. C Programming. Syntax. A small program(subroutine) that performs a particular task. Modular programming design

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

Week 4 EECS 183 MAXIM ALEKSA. maximal.io

EECS 183, Week 5. General. Variables I/O. 0. At which location do you have to take the exam? 1. Source code vs. object code? 2. What s a library?

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.7. User Defined Functions II

2/3/2018 CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II. Lecture Contents. C# basics. Methods Arrays. Dr. Amal Khalifa, Spr17

Functions. Lecture 6 COP 3014 Spring February 11, 2018

Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake

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

4. C++ functions. 1. Library Function 2. User-defined Function

Lab Instructor : Jean Lai

BITG 1113: Function (Part 2) LECTURE 5

Programming in C. Pointers and Arrays

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

CS313D: ADVANCED PROGRAMMING LANGUAGE

Computer Science 1 Honors

QUIZ Lesson 4. Exercise 4: Write an if statement that assigns the value of x to the variable y if x is in between 1 and 20, otherwise y is unchanged.

Fundamentals of Programming Session 23

Functions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single

LAB 11: METHODS. CPCS The Lab Note Lab 11 Page 1. Statement Purpose:

CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II

Chapter 6 Methods. Dr. Hikmat Jaber

CSCI 131, Midterm Exam 1 Review Questions This sheet is intended to help you prepare for the first exam in this course. The following topics have

Benefits of Methods. Chapter 5 Methods

Introduction to Programming

ECE 122. Engineering Problem Solving with Java

Lecture 5: Methods CS2301

Introduction to Programming in Turing. Input, Output, and Variables

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

Java Methods. Lecture 8 COP 3252 Summer May 23, 2017

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

COMP 110/L Lecture 13. Kyle Dewey

Chapter 3 Function Overloading

Introduction to Computer Science Midterm 3 Fall, Points

C++ Programming Lecture 11 Functions Part I

WARM UP LESSONS BARE BASICS

Lecture #6-7 Methods

Chapter 5 Methods. Modifier returnvaluetype methodname(list of parameters) { // method body; }

LAB 13: ARRAYS (ONE DIMINSION)

Reviewing all Topics this term

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

Question 2. [5 points] Given the following symbolic constant definition

Computer Science & Engineering 150A Problem Solving Using Computers

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

LOOPS. 1- Write a program that prompts user to enter an integer N and determines and prints the sum of cubes from 5 to N (i.e. sum of 5 3 to N 3 ).

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 9: OCT. 4TH INSTRUCTOR: JIAYIN WANG

Lecture 8 Tao Wang 1

Variables and Operators 2/20/01 Lecture #

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions

Introduction to Computer Science Unit 2. Notes

COMP2521. Generic ADTs in C

COP3502 Programming Fundamentals for CIS Majors 1. Instructor: Parisa Rashidi

Chapter 6 : Modularity Using Functions (pp )

Programming Language. Functions. Eng. Anis Nazer First Semester

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

C Programming for Engineers Functions

Object Oriented Methods : Deeper Look Lecture Three

Chapter 7 - Notes User-Defined Functions II

Computer Department. Question (1): State whether each of the following is true or false. Question (2): Select the correct answer from the following:

Lecture 9 - C Functions

User Defined Functions

Midterm Exam 2 Solutions C Programming Dr. Beeson, Spring 2009

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

What is an algorithm?

2. Numbers In, Numbers Out

ASSIGNMENT CLASS-11 COMPUTER SCIENCE [C++]

Recommended Group Brainstorm (NO computers during this time)

CSCI-1200 Data Structures Spring 2015 Lecture 2 STL Strings & Vectors

EECS402 Lecture 02. Functions. Function Prototype

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

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Methods CSC 121 Spring 2017 Howard Rosenthal

Introduction to Computers II Lecture 4. Dr Ali Ziya Alkar Dr Mehmet Demirer

Chapter 5 C Functions

1. Introduction. 2. Deliverables

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Computer Programming, I. Laboratory Manual. Experiment #7. Methods

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 14, SPRING 2013

Q1: Multiple choice / 20 Q2: Arrays / 40 Q3: Functions / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information.

Makefiles Makefiles should begin with a comment section of the following form and with the following information filled in:

GRADE 7 MATH LEARNING GUIDE

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

Getting started with C++ (Part 2)

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

or 5.00 or 5.000, and so on You can expand the decimal places of a number that already has digits to the right of the decimal point.

CpSc 1011 Lab 3 Integer Variables, Mathematical Operations, & Redirection

1 class Lecture5 { 2 3 "Methods" / References 8 [1] Ch. 5 in YDL 9 [1] Ch. 20 in YDL 0 / Zheng-Liang Lu Java Programming 176 / 199

Transcription:

COMP 111 PROGRAMMING I MODULARITY USING FUNCTIONS Instructor: Dr Dionysiou

ADMINISTRATIVE This week s lecture [BRON06] Chapter 6 (6.1) What is a function? Function declaration (prototype) Function definition (implementation) Function parameters 2

LECTURE OUTLINE Introduction to functions Modular programming Functions with no parameters Function Basics Function declaration (prototype) Function definition (implementation) Function call Functions with parameters 3

WHAT IS A FUNCTION? We ve seen functions before! Math functions Returned value We want to calculate the square root of number 123 1. find math function that performs the calculation double sqrt(double); 2. call the function and store the returned value result = sqrt(123); 4

WHAT IS A FUNCTION? Our programs so far used only one driver function (main) All input, calculations, output operations are performed in this one module Our programs from now on will be modular Self-contained modules (functions) will be responsible for particular tasks Input data Perform calculation Output results Modules are identified by a name are executed when they are called main() is a function that your program will always have! Program execution starts there 5

WHAT IS A FUNCTION? 6

WHAT IS A FUNCTION? Execution starts here 7

WHAT IS A FUNCTION? Calling function Called function 8

WHAT IS A FUNCTION? 9

WHAT IS A FUNCTION? Function declaration (function prototype) A function cannot be called unless it is defined!!! A function is not executed unless it is called!!! Function call Function definition (function implementation) 10

CREATE THESE PATTERNS 11

FUNCTIONS CALLING FUNCTIONS main() house() triangle() rectangle() Calling function? Called function? 12

CALCULATOR EXERCISE PART 1 Write a void function called displaymenu that displays the following menu options. Call this function in your main program. Welcome to my calculator! 1 : Add numbers 2 : Subtract numbers 3 : Multiply numbers 4 : Divide numbers 5 : Exit Enter choice: 13

CALCULATOR EXERCISE PART 1 14

LECTURE OUTLINE Introduction to functions Modular programming Functions with no parameters Function Basics Function declaration (prototype) Function definition (implementation) Function call Functions with parameters 15

FUNCTION BASICS There are 3 steps you need to follow: Declare your function Function prototype Define your function Function implementation Call your function Function call 16

FUNCTION PROTOTYPE Before a function is called, it must be declared Function prototype (or signature) return data type (if any, otherwise void) order and data types of parameters (if any, otherwise void) returndatatype functionname(list of parameter data types) void square(void); void findmax(double, double); char grade(double); double pi(void); double swap(int, char, int); 17

FUNCTION DEFINITION Actual implementation of the function Only defined once in a program Can be used by any other function that calls it Consists of two parts Header line Return data type of the function (could be void) Function name Data type of formal parameters in order (could be void) Function body C++ statements Function prototype and header line data types MUST match!! 18

FUNCTION DEFINITION findmax prototype returns void accepts 2 double arguments findmax definition header returns void accepts 2 double arguments x and y 19

FUNCTION CALL Use the name of the function and actual parameters (if any) Parameters must be listed in the same order and type as declared in the function prototype After the values are passed, control is transferred to the called function After the function terminates, control is returned back to the calling function 20

FUNCTION DEFINITION findmax call passes actual parameters 3.5 and 5.5 to the function 21

FORMAL AND ACTUAL PARAMETERS Actual parameters Formal parameters x has value 5 in the function show y has value 7 in the function show ch has value a in the function show 22

FORMAL AND ACTUAL PARAMETERS Actual parameters Formal parameters How to change the function call so that: x has value 15 in the function show y has value -7 in the function show ch has value d in the function show 23

FUNCTION POWER! Power of a function is its ability to work with parameters! Same function can be used on different arguments of the same type! Functions with parameters! 24

LECTURE OUTLINE Introduction to functions Modular programming Functions with no parameters Function Basics Function declaration (prototype) Function definition (implementation) Function call Functions with parameters 25

FUNCTIONS WITH PARAMETERS What is the flow of execution in this program? Rewrite this program to do the following: 1. Accept grade 2. Call printgrade to output the corresponding letter grade 1. Do a better validation on score 3. Repeat as long as the grade is between 0 and 100 26

FUNCTIONS WITH PARAMETERS 27

IN-CLASS EXERCISES Write a void function to display a line of $, where the number of $ is passed as an argument to this function. Test your program from main. Write a void function to display a square of $, where the side of the square is passed as an argument to this function. Test your program from main. Write a void function to display a line of a character, where the character is passed as an argument to this function. Test your program from main. Write a void function to display a square of a character, where the character and the square side are passed as arguments to this function. Test your program from main. 28

FUNCTIONS WITH PARAMETERS VS. WITHOUT PARAMETERS Write a void function called displaynumbers that will prompt the user to enter 2 integer numbers m and n, and will display all the numbers from m to n. i.e. if m=4 and n=9, the function will display 4 5 6 7 8 9 Test your function by calling it from your main program. Write a void function called displaynumbers that has two integer arguments m and n, and will display all the numbers from m to n. 29

IN-CLASS EXERCISE - ANSWER 30

IN-CLASS EXERCISE - ANSWER 31