Modular Programming with Functions

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

User-Defined Functions Like short programs Can operate on their own data Can receive data from callers and return data to callers.
User Defined Functions
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:
Chapter Five Functions
Introduction to C++ Functions Chapter 6 Sections 6.1 – 6.3 Computer II.
Introduction to Functions Programming. COMP102 Prog Fundamentals I: Introduction to Functions /Slide 2 Introduction to Functions l A complex problem is.
1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Functions. COMP104 Lecture 13 / Slide 2 Review of Array: Bubble Sort for (j=0; j List[j+1]) swap(List[j], List[j+1]); }
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
1 Lecture 14:User-Definded function I Introduction to Computer Science Spring 2006.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
1 10/30/06CS150 Introduction to Computer Science 1 Functions Chapter 3, page 313.
Chapter 05 (Part V) Control Statements: Part II. Nested For-Structures Consider the following codes: for (int i=0; i
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
FUNCTIONS IN C++. DEFINITION OF A FUNCTION A function is a group of statements that together perform a task. Every C++ program has at least one function,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Simple Functions Writing Reuseable Formulas. Problem Using OCD, design and implement a program that computes the area and circumference of an Australian.
Introduction to Functions.  A complex problem is often easier to solve by dividing it into several smaller parts, each of which can be solved by itself.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 8 Functions in Depth. Chapter 8 A programmer-defined function is a block of statements, or a subprogram, that is written to perform a specific.
CSE 1341 Honors Note Set 2 1. Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 05 (Part II) Control Statements: Part II.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Chapter 6: User-Defined Functions I
Dr. Shady Yehia Elmashad
Intro to Programming Week # 6 Repetition Structure Lecture # 10
Chapter 4 Procedural Abstraction and Functions That Return a Value 1
Lesson 5 Functions I A function is a small program which accomplishes a specific task. For example, we invoke (call) the function, sqrt(x), in the library.
CMPT 201 Functions.
CSC113: Computer Programming (Theory = 03, Lab = 01)
CS150 Introduction to Computer Science 1
Writing Reuseable Formulas
Dr. Shady Yehia Elmashad
Functions.
Math Library and IO formatting
User-defined Functions
Dr. Shady Yehia Elmashad
توابع در C++ قسمت اول اصول كامپيوتر 1.
Extra.
CSC1201: Programming Language 2
User Defined Functions
Chapter 5 Function Basics
User-defined Functions
Modular Programming with Functions
Chapter 3: Input/Output
Formatting the Output The C++ standard library supplies many manipulators: endl, setw, fixed, showpoint, setprecesion. If we want to use endl, fixed, or.
Chapter 6: User-Defined Functions I
Functions Divide and Conquer
CS149D Elements of Computer Science
In C Programming Language
Fundamental Programming
CSC1201: Programming Language 2
Output Formatting Bina Ramamurthy 4/16/2019 BR.
Single-Result Functions & Modularity
Introduction to Functions
Functions Divide and Conquer
Chapter 4 Procedural Abstraction and Functions That Return a Value 1
Output Formatting Bina Ramamurthy 9/25/2019 BR.
Introduction to Methods and Interfaces
Presentation transcript:

Modular Programming with Functions B. Ramamurthy Chapter 5 2/21/2019 BR

Introduction Complex problems: Divide and conquer Reusability of code Modular design of software to enhance readability and maintenance. Abstraction Information hiding 2/21/2019 BR

Structure Charts Structure charts are often used to represent the modular design. Here is an example of a structure chart for StickFigure. main drawTriangle drawHollowRectangle drawRectangle 2/21/2019 BR

Libraries of Functions We have used functions from many libraries: cmath : pow, sin, cos fstream : open, close, get, set iomanip: setw, setprecision iostream : << , >> Programmers can define functions for use in their program: programmer-defined functions. 2/21/2019 BR

Functions Definition Function header Function parameters Function body Function return value In fact int main() { … return 0; } is a function. 2/21/2019 BR

Example: StickFigure * 2/21/2019 BR *** ***** ******* ********* * * * * ************************************************************************ 2/21/2019 BR

Part of Stick Figure * *** ***** ******* ********* * * * * Step1 : Draw triangle Step2: Draw rectangle Step3: Draw hollow rectangle 2/21/2019 BR

drawTriangle(..) for (int i3 = 0; i3 < row; i3++) { for (int k = 1; k<= offset; k++) cout<< ' '; for (int j = 1; j <= col; j++ ) cout <<'*'; offset = offset -1; col = col +2; cout<< endl; } 2/21/2019 BR

drawRectangle(..) for (int i4 = 1; i4 <= length; i4++) { for (int k = 1; k<= offset; k++) cout<< ' '; for (int j = 1; j<= width; j++) cout <<'*'; cout<< endl; } 2/21/2019 BR

drawHollowRectangle(..) for (int i4 = 1; i4 <= length; i4++) { for (int k = 1; k<= offset; k++) cout<< ' '; if ((i4 ==1 ) || (i4 == length)) { for (int j = 1; j<= width; j++) cout <<'*'; } else { cout << '*'; for (int k =1; k<= width-2; k++) cout << ‘ '; cout<< endl; 2/21/2019 BR

Function syntax type functioName (parameters) { declare local variables/constants statements } 2/21/2019 BR

Example int factorial (int n) { int prod; for (int i = 1; i <= n; i++) prod = prod *i; return prod; } 2/21/2019 BR