Functions and an Introduction to Recursion.  Write a program for learn C++ subfunction.  Exercise: ◦ Please implement the following functions:  double.

Slides:



Advertisements
Similar presentations
User-Defined Functions Like short programs Can operate on their own data Can receive data from callers and return data to callers.
Advertisements

Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Functions Prototypes, parameter passing, return values, activation frams.
CSE202: Lecture 12The Ohio State University1 Function Calling.
DIFFERENTIATION & INTEGRATION CHAPTER 4.  Differentiation is the process of finding the derivative of a function.  Derivative of INTRODUCTION TO DIFFERENTIATION.
Derivatives - Equation of the Tangent Line Now that we can find the slope of the tangent line of a function at a given point, we need to find the equation.
Functions ROBERT REAVES. Functions  Interface – the formal description of what a subprogram does and how we communicate with it  Encapsulation – Hiding.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
Computer Programming 1 More on functions. Computer Programming 2 Objectives Function overloading Scope rules and namespace Inline Templates Pass by value.
1 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8.
Chapter 4 Summation.
Functions:Passing Parameters by Value Programming.
Functions Pass by Value Pass by Reference IC 210.
CS150 Introduction to Computer Science 1
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
1 11/8/06CS150 Introduction to Computer Science 1 More Functions! page 343 November 8, 2006.
Every slope is a derivative. Velocity = slope of the tangent line to a position vs. time graph Acceleration = slope of the velocity vs. time graph How.
Wicomico High School Mrs. J. A. Austin AP Calculus 1 AB Third Marking Term.
Limits and Derivatives Concept of a Function y is a function of x, and the relation y = x 2 describes a function. We notice that with such a relation,
Linear Approximation It is a way of finding the equation of a line tangent to a curve and using it to approximate a y value of the curve.
An introduction to limits Limits in calculus : This section gives some examples of how to use algebraic techniques to compute limits. these In cludethe.
Today in Calculus Go over homework Derivatives by limit definition Power rule and constant rules for derivatives Homework.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
3.1 Definition of the Derivative & Graphing the Derivative
111/15/2015CS150 Introduction to Computer Science 1 Summary  Exam: Friday, October 17,  Assignment: Wednesday, October 15, 2003  We have completed.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
Example Ex. Find Sol. So. Example Ex. Find (1) (2) (3) Sol. (1) (2) (3)
Antiderivatives. Indefinite Integral The family of antiderivatives of a function f indicated by The symbol is a stylized S to indicate summation 2.
MAT 212 Brief Calculus Section 5.4 The Definite Integral.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
FUNCTIONS - What Is A Function? - Advantages Function Declaration
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
CSED101 INTRODUCTION TO COMPUTING FUNCTION ( 함수 ) 유환조 Hwanjo Yu.
Chapter 17.2 The Derivative. How do we use the derivative?? When graphing the derivative, you are graphing the slope of the original function.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
4.1 Antiderivatives 1 Definition: The antiderivative of a function f is a function F such that F’=f. Note: Antiderivative is not unique! Example: Show.
5.3 Definite Integrals. Example: Find the area under the curve from x = 1 to x = 2. The best we can do as of now is approximate with rectangles.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
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.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
ㅎㅎ Fifth step for Learning C++ Programming Homework 1 Homework 2
Numerical Methods Some example applications in C++
Mean Value Theorem.
Chapter 16A.
A Lecture for the c++ Course
Differentiating Polynomials & Equations of Tangents & Normals
CSCI 161: Introduction to Programming Function
FOR LOOPS.
Part (a) Keep in mind that dy/dx is the SLOPE! We simply need to substitute x and y into the differential equation and represent each answer as a slope.
CS150 Introduction to Computer Science 1
Lab 1 Introduction to C++.
CS150 Introduction to Computer Science 1
Section 5.2 Definite Integrals.
Riemann Sums and Integrals
CS150 Introduction to Computer Science 1
Functions Divide and Conquer
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Fundamental Programming
§3.10 Linear Approximations and Differentials
CS150 Introduction to Computer Science 1
3.10 Linear Approximations and Differentials
Presentation transcript:

Functions and an Introduction to Recursion

 Write a program for learn C++ subfunction.  Exercise: ◦ Please implement the following functions:  double derivative(double a, double n, double x0);  receives three parameters and returns the slope of ax n at x 0.  double integral(double a, double n, double x1, double x2);  receives four parameters and returns the area enclosed by ax n, y=0, x=x 1 and x=x 2.

 To compute the slope S between (x 1, y 1 ) and (x 2, y 2 ): x y x2x2 x1x1 (x 1, y 1 ) (x 2, y 2 ) y 2 - y 1 x 2 - x 1

 To compute the slope of the tangent line at x 0 : x0x0 x y x 0 +dx f(x)f(x)

 When dx approaches 0, then the slope of the tangent line at x 0 is called the derivative of f(x) at x 0. ◦ We would use a very small dx to approximate the derivative.

 Program framework prototype implementation

 You can use the #define directive to give a meaningful name to a constant in your program. ◦ Example:  DX will be replaced by #include #define DX int main () { cout << DX << endl; return 0; } #include #define DX int main () { cout << DX << endl; return 0; }

 Remember the only way for a sub-function to communication with outside is through its parameters and return value! Copy value Output x Parameter int value Return Value S argument int x3 int value = x*x + 2*x + 5; 20

 What the sub-function can only access are its parameters and variables. ◦ Note: do not declare a variable of the same variable name with parameters.

 How do we compute the area enclosed by ax n, y=0, x=x 1 and x=x 2 ? x y x1x1 x2x2

 Use rectangles of the same width to cover the enclosed field and then sum up their area x y x1x1 x2x2 dx f(x 1 +dx) f(x1)f(x1)f(x 1 +2dx) f(x 1 +3dx) f(x 1 +4dx) f(x 1 +5dx)

 The measure of area is ◦ The accuracy of area measure depends on how small dx is. x y x1x1 x2x2

 When dx approaches 0, we can approximate the area below f(x) between x 1 and x 2. ◦ We denote as the integral of f(x) from x 1 to x 2.

 Program framework

 Use a very small value to represent dx.  Compute the area of rectangle by multiplying f(x) and dx.  Accumulate the area of rectangles. for (double i = x1; i <= x2; i += DX) { …… }