Functions and Libraries. The idea of a function Functions in programming A function is a block of code that has been given a name. To invoke that code.

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

Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Computer Science 1620 Functions. Given a number n, the factorial of n, written n!, is computed as follows: note: 0! = 1 examples: n! = n x (n-1) x (n-2)
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Chapter 6: User-Defined Functions I
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Programmer-defined functions Development of simple functions using value parameters.
Chapter 6: User-Defined Functions I
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Functions in C++ Eric Roberts CS 106B January 9, 2013.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Recursion Chapter Nature of Recursion t Problems that lend themselves to a recursive solution have the following characteristics: –One or more.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
Lecture 10 Recursion CSE225: Data Structures. 2 A Look Back at Functions #include double distance(double x1, double y1, double x2, double y2) { double.
Lecture6 Recursion function © by Pearson Education, Inc. All Rights Reserved. 1.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 6: User-Defined Functions
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
CPS120: Introduction to Computer Science Decision Making in Programs.
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.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
CPS120: Introduction to Computer Science Functions.
Chapter 6: Programmer- defined Functions Development of simple functions using value and reference parameters JPC and JWD © 2002 McGraw-Hill, Inc. Modified.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
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.
Section 4 - Functions. All of the programs that we have studied so far have consisted of a single function, main(). However, having more than one function.
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.
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.
GE 211 Dr. Ahmed Telba. // compound assignment operators #include using namespace std; int main () { a =5 int a, b=3; a = b; a+=2; // equivalent to a=a+2.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
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.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
FUNCTIONS - What Is A Function? - Advantages Function Declaration
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Concepts of Algorithms CSC-244 Unit 5 and 6 Recursion Shahid Iqbal Lone Computer College Qassim University K.S.A.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Objectives: How to define and call functions. Function declarations and how they differ from function definitions. How arguments are passed to functions.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
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.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Mechanics of Functions
Chapter 6: User-Defined Functions I
Functions in C++ Eric Roberts CS 106B January 7, 2015.
Functions and an Introduction to Recursion
User-Defined Functions
User-defined Functions
Mechanics of Functions
Chapter 5 Function Basics
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
User-defined Functions
Recursion.
Chapter 6: User-Defined Functions I
Functions and an Introduction to Recursion
Functions Imran Rashid CTO at ManiWeber Technologies.
CS1201: Programming Language 2
Introduction to Functions
Presentation transcript:

Functions and Libraries

The idea of a function

Functions in programming A function is a block of code that has been given a name. To invoke that code is known as calling that function. Arguments are a list of expression enclosed in parentheses (in function call). Parameters are a list of variable enclosed in parentheses (in function definition).

The advantage of using functions Write once, use many times. Make it possible to divide a large program into smaller (called decomposition). Some functions may be complicated, it is appropriate to decompose them into smaller ones. This process is called top-down design or stepwise refinement.

Functions and algorithms Functions provide a basis for the implementation of algorithms. Euclid’s algorithm 1. Divide x by y and compute the remainder; call that remainder r. 2. If r is zero, the algorithm is complete, and the answer is y. 3. If r is not zero, set x to the old value of y, set y equal to r, and repeat the process. int gcd(int x, int y) { int r = x % y; while (r != 0) { x = y; y = r; r = x % y; } return y; }

Libraries Most of the code the computer executes is not the code you have written yourself, but the library code that you have loaded along with the application. If you want to become an effective C++ programmer, you need to spend much time learning about the standard libraries. The library exports the service, e.g. exports cin and cout streams.

Libraries An implementer is a programmer who implements a library. A client is a programmer who calls functions provided by a library. Example functions in abs(x)exp(x)cos(theta) sqrt(x)log(x)sin(theta) floor(x)log10(x)tan(theta) ceil(x)pow(x,y)atan(x)

Defining functions in C++ type name(parameters) {... body... } ตัวอย่าง predicate function bool isEven(int n) { return n % 2 == 0; } ตัวอย่างการเรียกใช้ predicate function If (isEven(i))...

Overloading Using the same name for more than one version of a function is called overloading. Signature is a pattern of arguments taken by a function (i.e. number and types of argument). The primary advantage is makes it easier for a programmer to keep track of different function names.

Overloading int abs(int x) { return (x < 0) ? -x : x; } double abs(double x) { return (x < 0) ? -x : x; }

Default parameters Default parameters are parameters should have if they do not appear in the call. ตัวอย่างการประกาศใน function prototype void formatInColumns(int nColumns = 2); ตัวอย่างการเรียกใช้ formatInColumns();

Default parameters rules The default value appears only in the function prototype and not in the function definition. Any default parameters must appear at the end of the parameter list.

Bad using default parameter void setInitialLocation(double x, double y); void setInitialLocation() { setInitialLocation(0, 0); } ตัวอย่างการใช้งานที่ไม่ดีเพราะสามารถเรียก ฟังก์ชันด้วยอาร์กิวเม้นท์ตัวเดียว void setInitialLocation(double x = 0, double y = 0);

The steps in calling a function The calling function computes values for each argument. The system create new space for all local variable of the new function which is called stack frame. The value of each argument is copied into the corresponding parameter variable. A type conversion is performed. The statements in the function body are executed until reaching a return statement. The value of the return expression is evaluated and returned as a value of a function. A type conversion is performed. The stack frame is discarded. The calling program continues.

The combinations function

/* * File: Combinations.cpp * * This program computes the mathematical function C(n, k) from * its mathematical definition in terms of factorials. */ #include using namespace std; /* Function prototypes */ int combinations(int n, int k); int fact(int n);

/* Main program */ int main() { int n, k; cout << "Enter the number of objects (n): "; cin >> n; cout << "Enter the number to be chosen (k): "; cin >> k; cout << "C(n, k) = " << combinations(n, k) << endl; return 0; }

/* * Function: combinations(n, k) * Usage: int nWays = combinations(n, k); * * Returns the mathematical combinations function C(n, k), which is * the number of ways one can choose k elements from a set of size n. */ int combinations(int n, int k) { return fact(n) / (fact(k) * fact(n - k)); }

/* * Function: fact(n) * Usage: int result = fact(n); * * Returns the factorial of n, which is the product of all the * integers between 1 and n, inclusive. */ int fact(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; }