Functions Divide and Conquer

Slides:



Advertisements
Similar presentations
User Defined Functions
Advertisements

Chapter Five Functions
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Introduction to Functions Programming. COMP102 Prog Fundamentals I: Introduction to Functions /Slide 2 Introduction to Functions l A complex problem is.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
1 CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
1 9/26/07CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
1 10/30/06CS150 Introduction to Computer Science 1 Functions Chapter 3, page 313.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPS120: Introduction to Computer Science Functions.
CPS120: Introduction to Computer Science Lecture 14 Functions.
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.
111/15/2015CS150 Introduction to Computer Science 1 Summary  Exam: Friday, October 17,  Assignment: Wednesday, October 15, 2003  We have completed.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
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 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Chapter 6: User-Defined Functions I
Dr. Shady Yehia Elmashad
Functions prototypes arguments overloading return values part I.
for Repetition Structures
Chapter 5 Function Basics
CSC113: Computer Programming (Theory = 03, Lab = 01)
Writing Reuseable Formulas
Dr. Shady Yehia Elmashad
CSCI 161: Introduction to Programming Function
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
Dr. Shady Yehia Elmashad
User Defined Functions
Chapter 5 Function Basics
CS150 Introduction to Computer Science 1
Modular Programming with Functions
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
Chapter 6: User-Defined Functions I
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS149D Elements of Computer Science
CS150 Introduction to Computer Science 1
Arithmetic Operations
CS150 Introduction to Computer Science 1
do/while Selection Structure
CS150 Introduction to Computer Science 1
Fundamental Programming
Reading from and Writing to Files
CS150 Introduction to Computer Science 1
Predefined Functions Revisited
CS150 Introduction to Computer Science 1
Functions Imran Rashid CTO at ManiWeber Technologies.
Introduction to Functions
Functions Divide and Conquer
CS150 Introduction to Computer Science 1
Reading from and Writing to Files
Presentation transcript:

Functions Divide and Conquer 10/26/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Last Time We Completed discussing reading from and writing to files Today we will Begin learning about functions and modularity in C++ 10/26/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Functions Functions are a way of building modules in your program Encapsulate some calculation Less repetitive code Example: x = sqrt(y); cout << x << endl; 10/26/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Library functions Code reuse. Why reinvent the wheel? Libraries are collections of often used functions Math library is an example #include <cmath> List of functions given on p. 173 We can call (invoke) the functions as follows: abs( x ) pow( x, y ) sin( x ) 10/26/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 17.1 Problem How would we rewrite the powers of 2 problem using the cmath library? (Output first 10 powers of 2) 10/26/05 CS150 Introduction to Computer Science 1

Programmer Defined Functions We can write our own functions Implement top down design Determine major steps of program Write functions to implement each step Program is more modular--each step is clearly defined Details of steps are hidden 10/26/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Example Write a program that outputs the cube of the first ten integers 10/26/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Example #include <iostream> int cube(int); void main() { int i, cubeOfI; for (i = 1; i<= 10; i++) cubeofi = cube(i); cout << “Cube of “ << i << “ is “ << cubeofi << endl; } int cube (int k) return k*k*k; 10/26/05 CS150 Introduction to Computer Science 1

Working with Functions 1. Function prototypes Function must be declared before it is referenced General form: type fname(datatypes of formal_arguments); Example: int cube(int); 10/26/05 CS150 Introduction to Computer Science 1

Working with Functions 2. Function definitions General form: type fname(formal arguments) { local declarations; statements; } Example: int cube(int k) return k*k*k; 10/26/05 CS150 Introduction to Computer Science 1

Working with Functions 3. Function call General form: fname(actual arguments); Example: cubeofi = cube(i); 10/26/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Things to remember Formal arguments are the arguments in the function definition Actual arguments are the values or variables in the function call Order of the arguments is very important! Make sure types match 10/26/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 17.2 Problem Write a function that sums integers in a particular range Write a program that uses the above function 10/26/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Example #include <iostream> char isEven(int); int getVal(); int main(void) { int num; while ((num = getVal()) != -999) if (isEven(num) == ‘E’) cout << “EVEN” << endl; else cout << “ODD” << endl; } return 0; 10/26/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Example (cont.) char isEven(int number) { const char even = ‘E’; const char odd = ‘O’; char answer; if (number % 2 == 0) answer = even; else answer = odd; return answer; } 10/26/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Example (cont.) int getVal() { int number; cout << “Enter an integer number: “; cin >> number; return number; } 10/26/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Summary In today’s lecture we covered Library functions Programmer defined functions Readings P. 170 - 180 10/26/05 CS150 Introduction to Computer Science 1