111/15/2015CS150 Introduction to Computer Science 1 Summary  Exam: Friday, October 17, 2003.  Assignment: Wednesday, October 15, 2003  We have completed.

Slides:



Advertisements
Similar presentations
Introduction to Functions Programming. COMP102 Prog Fundamentals I: Introduction to Functions /Slide 2 Introduction to Functions l A complex problem is.
Advertisements

Chapter 5 Functions.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
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 11/27/06CS150 Introduction to Computer Science 1 Searching Arrays.
CS150 Introduction to Computer Science 1
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
1 CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
The If/Else Statement, Boolean Flags, and Menus Page 180
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.
CPS120: Introduction to Computer Science Functions.
CSE 332: C++ execution control statements Overview of C++ Execution Control Expressions vs. statements Arithmetic operators and expressions * / % + - Relational.
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.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
Review the following : Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops.
Functions & Libraries. Introduction Functions – Modules of code Uses – Abstraction Build complex tasks with simpler ones Don't worry about details at.
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.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
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.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
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.
11/10/2016CS150 Introduction to Computer Science 1 Last Time  We covered “for” loops.
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.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Functions (2)
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
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.
13/15/2016CS150 Introduction to Computer Science 1 Summary  Assignment due on Wednesday, October 29,  Tutor will be in the lab on Tuesday evening,
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
Chapter 6: User-Defined Functions I
Function Topic 4.
Functions prototypes arguments overloading return values part I.
for Repetition Structures
CSCI 161: Introduction to Programming Function
User Defined Functions
Chapter 5 Function Basics
Value returning Functions
CS150 Introduction to Computer Science 1
Counting Loops.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Functions Divide and Conquer
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:

111/15/2015CS150 Introduction to Computer Science 1 Summary  Exam: Friday, October 17,  Assignment: Wednesday, October 15, 2003  We have completed chapters 1, 2, 4 and 5.  We have covered basic C++ expressions, arithmetic expressions, selection structures and repetition structures.  We will now look at functions.

211/15/2015CS150 Introduction to Computer Science 1 Functions  Functions are a way of building modules in your program.  Encapsulate some calculation.  Less repetitive code.  Examples:  x = sqrt(y);  cout << x << endl;

311/15/2015CS150 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  List of functions given on p  Examples:  abs(x)  pow(x,y)  sin(x)

411/15/2015CS150 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)

511/15/2015CS150 Introduction to Computer Science 1 User 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.

611/15/2015CS150 Introduction to Computer Science 1 Problem  Write a program that outputs the cube of the first ten integers.

711/15/2015CS150 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; }

811/15/2015CS150 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);

911/15/2015CS150 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; }

1011/15/2015CS150 Introduction to Computer Science 1 Working with Functions  3. Function call  General form:  fname(actual arguments);  Example:  cubeofi = cube(i);

1111/15/2015CS150 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.

1211/15/2015CS150 Introduction to Computer Science 1 Problem  Write a function that sums integers in a particular range.  Write a program that uses the function.

1311/15/2015CS150 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; }

1411/15/2015CS150 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; }

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