Functions. COMP104 Functions / Slide 2 Introduction to Functions * A complex problem is often easier to solve by dividing it into several smaller parts,

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Methods Java 5.1 A quick overview of methods
Modular Programming With Functions
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.
More on Functions Programming. COMP104 Lecture 19 / Slide 2 Passing Parameters by Reference l To have a function with multiple outputs, we have to use.
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 14:User-Definded function I Introduction to Computer Science Spring 2006.
12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept.
1 TAC2000/ Protocol Engineering and Application Research Laboratory (PEARL) MATH Functions in C Language.
CSE1222: Lecture 4The Ohio State University1. Mathematical Functions (1)  The math library file cmath Yes, this is a file with definitions for common.
CSE202: Lecture 4The Ohio State University1 Mathematical Functions.
Chapter 3 Expressions and Interactivity Department of Computer Science Missouri State Univeristy.
Functions Why we use functions C library functions Creating our own functions.
Functions—Part I. Slide 2 Where are we now? Simple types of variables 3 program structures cin(>>)/cout(
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
Functions. Let’s look at the 2 programs of evaluating: y=2^3+2^5+2^6; #include using namespace std; int main() { int y=0; int partResult=1; for (int i=1;
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
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.
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
Department of Electrical and Computer Engineering Introduction to C++: Primitive Data Types, Libraries and Operations By Hector M Lugo-Cordero August 27,
CS221 Random Numbers. Random numbers are often very important in programming Suppose you are writing a program to play the game of roulette The numbers.
Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
1 MODULAR DESIGN AND ABSTRACTION. 2 SPECIFYING THE DETAILS OF A PROBLEM INTO A RELATED SET OF SMALLER PROBLEMS.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3 : Top Down Design with Functions By Suraya Alias.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Introduction to C++.  Computers: CPU, Memory & Input / Output (IO)  Program: Sequence of instructions for the computer.  Operating system: Program.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
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.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
CSE 110: Programming Language I Afroza Sultana UB 1230.
Chapter 6: User-Defined Functions I
Introduction to Programming
Functions in C ++ Subject: Programming Languages ​​for III year Topic: functions, member class.
Mathematical Functions
Library Functions Goals of software engineering reliable code
Function Topic 4.
Chapter 4 Procedural Abstraction and Functions That Return a Value 1
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
Lesson 5 Functions I A function is a small program which accomplishes a specific task. For example, we invoke (call) the function, sqrt(x), in the library.
Chapter 3: Expressions and Interactivity.
CMPT 201 Functions.
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Writing Reuseable Formulas
User-defined Functions
Using Free Functions Chapter 3 Computing Fundamentals with C++
Functions October 23, 2017.
Functions.
Functions.
User-defined Functions
Starting Out with C++: From Control Structures through Objects
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
Chapter 6: User-Defined Functions I
Functions Imran Rashid CTO at ManiWeber Technologies.
Introduction to Functions
Chapter 4 Procedural Abstraction and Functions That Return a Value 1
Presentation transcript:

Functions

COMP104 Functions / Slide 2 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. * This is called top-down programming. * These parts are called functions in C++ (also sometimes called subprograms).  main() then executes these functions so that the original problem is solved.

COMP104 Functions / Slide 3 Advantages of Functions * Functions separate the concept (what is done) from the implementation (how it is done). * Functions make programs easier to understand. * Functions make programs easier to modify. * Functions can be called several times in the same program, allowing the code to be reused.

COMP104 Functions / Slide 4 Function Input and Output Function Result Parameters

COMP104 Functions / Slide 5 C++ Functions * C++ allows the use of both internal (user-defined) and external functions.  External functions (e.g., cin, cout, rand, etc.) are usually grouped into specialized libraries (e.g., iostream, cstdlib, cmath, etc.)

COMP104 Functions / Slide 6 Mathematical Functions * #include * double log(double x) natural logarithm * double log10(double x) base 10 logarithm * double exp(double x) e to the power x * double pow(double x, double y) x to the power y * double sqrt(double x) positive square root of x * double ceil(double x) smallest integer not less than x * double floor(double x) largest integer not greater than x * double sin(double x), cos(double x), tan(double x), etc...

COMP104 Functions / Slide 7 Distance Between Two Points // Compute distance between two points #include #include // contains sqrt() using namespace std; int main(){ double x1, y1, x2, y2; // coordinates for point 1 & 2 double dist; // distance between points cout << "Enter x & y coordinates of the 1st point: "; cin >> x1 >> y1; cout << "Enter x & y coordinates of the 2nd point: "; cin >> x2 >> y2; // Compute the distance between points 1 & 2 dist = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)); cout << "The distance is " << dist << endl; return 0; }

COMP104 Functions / Slide 8 Functions in a Program * C++ programs usually have the following form: // include statements // function prototypes // main() function // user-defined functions

COMP104 Functions / Slide 9 Function Prototype * The function prototype declares the interface, or input and output parameters of the function, leaving the implementation for the function definition. * The function prototype has the following syntax: ( ); * Example: A function that prints the card (J) given the card number (11) as input: void printcard(int); n (This is a void function - a function that does not return a value)

COMP104 Functions / Slide 10 Function Definition * The function definition can be placed anywhere in the program after the function prototypes.  You can place a function definition in front of main(). In this case there is no need to provide a function prototype for the function, since the function is already defined before its use. * A function definition has following syntax: ( ){ }

COMP104 Functions / Slide 11 Function Call * A function call has the following syntax: ( ) n There is a one-to-one correspondence between the parameters in a function call and the parameters in the function definition.

COMP104 Functions / Slide 12 Printing Cards  The main() program which calls printcard() #include using namespace std; void printcard(int);// function prototype int main(){ int c1, c2, c3, c4, c5; // pick cards... // print cards printcard(c1); printcard(c2); printcard(c3); printcard(c4); printcard(c5); // find score // print score }

COMP104 Functions / Slide 13 Printing Cards * A function that prints the card (J) given the card number (11) as input: void printcard(int cardnum){ if(cardnum==1) cout << "A"; else if(cardnum>=2 && cardnum<=10) cout << cardnum; else if(cardnum==11) cout << "J"; else if(cardnum==12) cout << "Q"; else if(cardnum==13) cout << "K"; }

COMP104 Functions / Slide 14 Absolute Value #include using namespace std; int absolute(int);// function prototype for absolute() int main(){ int x, y, diff; cout << "Enter two integers (separated by a blank): "; cin >> x >> y; diff = absolute( x - y); cout << "The absolute difference between " << x << " and " << y << " is: " << diff << endl; return 0; } // Define a function to take absolute value of an integer int absolute(int x){ if (x >= 0)return x; else return -x; }

COMP104 Functions / Slide 15 Absolute Value (alternative) * Note that it is possible to omit the function prototype if the function is placed before it is called. #include using namespace std; int absolute(int x){ if (x >= 0)return x; else return -x; } int main(){ int x, y, diff; cout << "Enter two integers (separated by a blank): "; cin >> x >> y; diff = absolute( x - y); cout << "The absolute difference between " << x << " and " << y << " is: " << diff << endl; return 0; }

COMP104 Functions / Slide 16 Adding Numbers  Consider the following function: int add(int a, int b){ int result = a+b; return result; } * We might call the function using the syntax: int main(){ int sum; sum = add(5, 3); return 0; } * This would result in variable sum being assigned the value 8.

COMP104 Functions / Slide 17 Three-Point Distance #include using namespace std; double dist(double, double, double, double); int main(){ double x1, y1, // coordinates for point 1 x2, y2, // coordinates for point 2 x3, y3; // coordinates for point 3 cout << "Enter x & y coordinates of the 1st point: "; cin >> x1 >> y1; cout << "Enter x & y coordinates of the 2nd point: "; cin >> x2 >> y2; cout << "Enter x & y coordinates of the 3rd point: "; cin >> x3 >> y3;

COMP104 Functions / Slide 18 Three-Point Distance cout <<"The distance from point 1 to 2 is: " << dist(x1,y1,x2,y2) << endl; cout <<"The distance from point 2 to 3 is: " << dist(x2,y2,x3,y3) << endl; cout <<"The distance from point 1 to 3 is: " << dist(x1,y1,x3,y3) << endl; return 0; } // Function for computing the distance between 2 pts double dist(double x1, double y1, double x2, double y2) { double dist; dist = sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) ); return dist; }