1 Lecture 14:User-Definded function I Introduction to Computer Science Spring 2006.

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout
Advertisements

Chapter 6: User-Defined Functions I
Copyright © 2002 Pearson Education, Inc. Slide 1.
User Defined Functions
Computer Science 1620 Math Library. Remember this program? suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to.
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Functions. COMP104 Functions / Slide 2 Introduction to Functions * A complex problem is often easier to solve by dividing it into several smaller parts,
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.
1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Writing and Testing Programs Drivers and Stubs Supplement to text.
Chapter 6: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
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 Lecture 5: Input/Output (I) Introduction to Computer Science Spring 2006.
Chapter 6: User-Defined Functions I
1 Lecture 17:User-Definded function II Introduction to Computer Science Spring 2006.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Chapter 6: User-Defined Functions
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Functions Why we use functions C library functions Creating our own functions.
1 Simple Functions Writing Reuseable Formulas. In Math Suppose f (x) = 2 x 2 +5Suppose f (x) = 2 x 2 +5 f(5)=?f(5)=? f(5) = 2* =55f(5) = 2*
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
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.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
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 6 USER-DEFINED FUNCTIONS Made By- Kartik Belwal.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
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.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Scis.regis.edu ● CS-361: Control Structures Week 2 Dr. Jesús Borrego Lead Faculty, COS Regis University 1.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
USING & CREATING FUNCTIONS. MODULAR PROGRAMMING  Why Modular Programming?  Improves Readability & Understandability  Improve Maintainability  Allows.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
USER-DEFINED FUNCTIONS I. In this chapter, you will: Learn about standard (predefined) functions and discover how to use them in a program Learn about.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
CHAPTER 6 USER-DEFINED FUNCTIONS I
Chapter 6: User-Defined Functions I
Dr. Shady Yehia Elmashad
CMPT 201 Functions.
Chapter 6: User-Defined Functions I
CSC113: Computer Programming (Theory = 03, Lab = 01)
Writing Reuseable Formulas
Dr. Shady Yehia Elmashad
Math Library and IO formatting
User-Defined Functions
Formatted and Unformatted Input/Output Functions
User Defined Functions
Chapter 5 Function Basics
Using Free Functions Chapter 3 Computing Fundamentals with C++
Functions October 23, 2017.
CS150 Introduction to Computer Science 1
Modular Programming with Functions
Chapter 6: User-Defined Functions I
Functions Divide and Conquer
Output Formatting Bina Ramamurthy 4/16/2019 BR.
Functions Imran Rashid CTO at ManiWeber Technologies.
Introduction to Functions
Output Formatting Bina Ramamurthy 9/25/2019 BR.
Presentation transcript:

1 Lecture 14:User-Definded function I Introduction to Computer Science Spring 2006

2 #include using namespace std; int main() { int i, j; int k; cin>>i>>j; if (i > j) { k = i; } else { k = j; } return 0; } #include using namespace std; /* Function Declarations */ int FindMax(int n1, int n2); int main() { int i, j; int k; cin>>i>>j; k = FindMax(i,j); return 0; } /* Function Definitions */ int FindMax(int n1, int n2) { if (n1 > n2) { return n1; } else { return n2; }

3 Functions Functions are like building blocks They allow complicated programs to be divided into manageable pieces Some advantages of functions: A programmer can focus on just that part of the program and construct it, debug it, and perfect it Different people can work on different functions simultaneously Can be used in more than one place in a program or in different programs

4 Functions (continued) Functions Called modules Like miniature programs Can be put together to form a larger program

5 Predefined Functions In algebra, a function is defined as a rule or correspondence between values, called the function ’ s arguments, and the unique value of the function associated with the arguments If f(x) = 2x + 5, then f(1) = 7, f(2) = 9, and f(3) = 11 1, 2, and 3 are arguments 7, 9, and 11 are the corresponding values

6 Question What should we do if we want to calculate x y ? Answer: use standard(library) function pow(x,y) Pow(x,y)=x y Parameters(arguments) x and y are of type double the value of pow(x,y) is type double: we say function pow is of type double

7 Use of standard function pow #include using namespace std; int main() { double u,v; double result; u = 4.2; v = 3.0; return 0; } #include result=pow(u, v); cout << u << " to the power of " << v << " = " << pow(u, v) << endl; cout << "u = " << u << endl; u = u + pow(3, 3);

8 Predefined Functions (continued) Some of the predefined mathematical functions are: sqrt(x) pow(x,y) floor(x) Predefined functions are organized into separate libraries I/O functions are in iostream header Math functions are in cmath header

9 The Power Function (pow) pow(x,y) calculates x y, pow(2,3) = 8.0 pow returns a value of the type double x and y are called the parameters (or arguments) of the function pow Function pow has two parameters

10 The sqrt and floor Functions The square root function sqrt(x) Calculates the non-negative square root of x, for x >= 0.0 sqrt(2.25) is 1.5 Type double and has only one parameter

11 The sqrt and floor Functions (continued) The floor function floor(x) Calculates largest whole number not greater than x floor(48.79) is 48.0 Type double and has only one parameter

12

13 End of lecture 14 Thank you!