1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.

Slides:



Advertisements
Similar presentations
User Defined Functions
Advertisements

Chapter Five Functions
Modular Programming With Functions
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
1 Programming in C++ Lecture Notes 9 Functions (Returning Values) Andreas Savva.
Introduction to Functions Programming. COMP102 Prog Fundamentals I: Introduction to Functions /Slide 2 Introduction to Functions l A complex problem is.
Chapter 5 Functions.
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.
CS150 Introduction to Computer Science 1
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.
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.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
1 CS161 Introduction to Computer Science Topic #10.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
CPS120: Introduction to Computer Science Lecture 14 Functions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
111/15/2015CS150 Introduction to Computer Science 1 Summary  Exam: Friday, October 17,  Assignment: Wednesday, October 15, 2003  We have completed.
Functions & Libraries. Introduction Functions – Modules of code Uses – Abstraction Build complex tasks with simpler ones Don't worry about details at.
1 CS161 Introduction to Computer Science Topic #9.
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.
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.
CHAPTER 6 USER-DEFINED FUNCTIONS I. In this chapter, you will: Learn about standard (predefined) functions and discover how to use them in a program Learn.
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.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Function prototype A function must be declared before it can be referenced. One way to declare a function is to insert a function prototype before the.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
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 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Dr. Shady Yehia Elmashad
Functions prototypes arguments overloading return values part I.
for Repetition Structures
CSC113: Computer Programming (Theory = 03, Lab = 01)
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
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
Functions Divide and Conquer
CS149D Elements of Computer Science
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
do/while Selection Structure
CS150 Introduction to Computer Science 1
Fundamental Programming
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
Presentation transcript:

1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer

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

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

4 10/18/04CS150 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 o #include  List of functions given on p. 173  We can call (invoke) the functions as follows: o abs( x ) o pow( x, y ) o sin( x )

5 10/18/04CS150 Introduction to Computer Science 1 Problem  How would we rewrite the powers of 2 problem using the cmath library? (Output first 10 powers of 2)

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

7 10/18/04CS150 Introduction to Computer Science 1 Problem  Write a program that outputs the cube of the first ten integers

8 10/18/04CS150 Introduction to Computer Science 1 Example #include 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; }

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

10 10/18/04CS150 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; }

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

12 10/18/04CS150 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

13 10/18/04CS150 Introduction to Computer Science 1 Problem  Write a function that sums integers in a particular range  Write a program that uses the above function

14 10/18/04CS150 Introduction to Computer Science 1 Example #include 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; }

15 10/18/04CS150 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; }

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

17 10/18/04CS150 Introduction to Computer Science 1 Summary  In today’s lecture we covered o Library functions o Programmer defined functions  Readings o P