Functions.

Slides:



Advertisements
Similar presentations
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)
Advertisements

Functions. COMP104 Functions / Slide 2 Introduction to Functions * A complex problem is often easier to solve by dividing it into several smaller parts,
Introduction to Functions Programming. COMP102 Prog Fundamentals I: Introduction to Functions /Slide 2 Introduction to Functions l A complex problem is.
Computer Science 1620 Loops.
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
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:Passing Parameters by Value Programming.
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]); }
CS 201 Functions Debzani Deb.
Top-Down Design with Functions 4 What do programmer’s (not programs!) use as input to the design of a program? –Documentation Problem definition Requirements.
Chapter 6: User-Defined Functions I
Functions. COMP104 Lecture 13 / Slide 2 Function Prototype * The function prototype declares the interface, or input and output parameters of the function,
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Function Part II: Some ‘advanced’ concepts on functions.
Topic 2A – Library Functions and Casting. CISC 105 – Topic 2A Functions A function is a piece of code which performs a specific task. When a function.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Functions—Part I. Slide 2 Where are we now? Simple types of variables 3 program structures cin(>>)/cout(
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
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 6 September 17, 2009.
1 FUNCTIONS - I Chapter 5. 2 What are functions ? Large programs can be modularized into sub programs which are smaller, accomplish a specific task and.
CPS120: Introduction to Computer Science Functions.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
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.
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
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.
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.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
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.
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.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
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.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 6: User-Defined Functions I
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Function Topic 4.
Functions Chapter 5 CS12 - Computer Programming 1 Chapter 5.
CO1401 Programming Design and Implementation
CSC113: Computer Programming (Theory = 03, Lab = 01)
CSCI 161: Introduction to Programming Function
User-Defined Functions
User-defined Functions
Using Free Functions Chapter 3 Computing Fundamentals with C++
Loops (iterations, repetitions)
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.
Functions.
User-defined Functions
More ‘concepts’ on Function
Pass by Reference.
Chapter 6: User-Defined Functions I
Lesson #6 Modular Programming and Functions.
Predefined Functions Revisited
Functions Imran Rashid CTO at ManiWeber Technologies.
CS 144 Advanced C++ Programming January 31 Class Meeting
Introduction to Functions
More ‘concepts’ on Function
Functions in C Math Library Functions Functions Function Definitions
Presentation transcript:

Functions

Where are we now? Midterm: now Simple types of variables Array structured programming now Simple types of variables 3 program structures cin(>>)/cout(<<) Array Functions (subprograms) (File I/O)

Review: Problem Solving Process Define and analyze the problem What is the input & output? What constraints must be satisfied? What information is essential? Design an algorithm What steps must be done? Wirte down the solution steps in detail Implement a program - programming or coding. Compile, test, and debug the program - testing. Document and maintain the program. Problem solving phase Implementation phase

Top-down analysis A complex problem is often easier to solve by dividing it into several smaller parts (subproblems), each of which can be solved by itself. This is called top-down programming. Let’s take the example of Diamand drawing!

Example: Diamond Pattern Input: nothing Output: print out a diamond pattern * * * * * * * * * * * * * * * * * * * * * * * * *

Example: top-down analysis Output: print out a diamond pattern print out the upper half  upper triangular form print out the lower half  lower triangular form

Example: upper triangular pattern row 1: print 4 spaces, 1 star; row 2: print 3 spaces, 3 stars; row 3: print 2 spaces, 5 stars; row 4: print 1 space, 7 stars; row 5: print 0 spaces, 9 stars; * * * * * * * * * * * * * * * * * * * * * * * * *

Example: top-down refinement row 1: print (5-row) spaces, (2*row - 1) stars; row 2: print (5-row) spaces, (2*row - 1) stars; row 3: print (5-row) spaces, (2*row - 1) stars; row 4: print (5-row) spaces, (2*row - 1) stars; row 5: print (5-row) spaces, (2*row - 1) stars; For each ‘row’ (more exactly from row 1 to 5), always do Print 5-row spaces Print 2*row-1 stars It’s a loop: pseudo-code * * * * * * * * * * * * * * * * * * * * * * * * * int row; row=1; while(row<=5){ print 5-row spaces; print 2*row-1 stars; row=row+1; }

Example: algorithm int row, space, star; row=1; int row; while(row<=5){ space=1; while(space<=5-row) { cout << " "; space=space+1; } star=1; while(star<=2*row-1) { cout << "*"; star=star+1; cout << endl ; row=row+1; int row; row=1; while(row<=5){ 5-row spaces; 2*row-1 stars; row=row+1; }

Introduction to Functions The subproblems are solved by subprograms, called functions in C++. main() is the first function, then executes these functions so that the original problem is solved.

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

Function Input and Output results Input parameters

C++ Functions C++ allows the use of both pre-defined and user-defined functions. Predefined functions (e.g., cin, cout, rand, etc.) are usually grouped into specialized libraries (e.g., iostream, cstdlib, cmath, etc.)

Mathematical Functions #include <cmath> 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...

How to use a pre-defined function: Distance Between Two Points Output:distance Input: two points: (x1,y1), and (x2,y2) Distance = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)) x1,y1, x2, y2 are input data, so we are done!

How to use a pre-defined function: Distance Between Two Points #include <iostream> #include <cmath> // 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; dist = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)); cout << "The distance is " << dist << endl; return 0; } They are also function calls!

How to define a function? A function definition has following syntax: <type> <function name>(<parameter list>){ <local declarations> <sequence of statements> }

A function returns a single result (that’s the type of the function) One of the statements in the function body should have the form: return <expression>; (it’s good to use ONLY one at last!) The value passed back by return should have the same type as the function.

Input absoluteValue Output int main(){ int value; int absvalue; cout << "Enter integer: "; cin >> value; if (value < 0) absvalue = -value; else absvalue = value; cout << "The absolute value is " << absvalue << endl; return 0; } Input if (value < 0) absvalue = -value; else absvalue = value; absoluteValue Output

Example: Absolute Value (1st version) #include <iostream> using namespace std; int absolute(int fx){ int abs; if (fx >= 0) abs=fx else abs = -fx; return abs; } int main(){ int x, y, diff,adiff; cout << "Enter two integers (separated by a blank): "; cin >> x >> y; diff = x – y; adiff=absolute(diff); cout << "The absolute difference between " << x << " and " << y << " is: " << adiff << endl; return 0;

Function Prototype The function prototype declares the interface, or input and output parameters of the function, leaving the implementation for the function definition. (It therefore separates the concept from the implementation!) The function prototype has the following syntax: <type> <function name>(<type list>);

You can place a function definition in front of main() 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. The function definition can be placed anywhere in the program after the function prototypes. C++ programs usually have the following form: // include statements // function prototypes // main() function // user-defined functions

Absolute Value (2nd version) #include <iostream> 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; } int absolute(int fx){ int abs; if (fx >= 0) abs=fx else abs=-fx; return abs;

How to use or call a function? A function call has the following syntax: <function name>(<parameter list>) There is a one-to-one correspondence between the parameters in a function call and the parameters in the function definition.

Summary with Absolute Value Example #include <iostream> using namespace std; int absolute (int); // function prototype for absolute() // int absolute (int fx); int main(){ int x, y, diff; cout << "Enter two integers (separated by a blank): "; cin >> x >> y; diff = x-y; diff = absolute(diff); 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 fx) { int abs; if (fx >= 0) abs=fx; else abs= -fx; return (abs); function call, argument formal parameter final result of the function

‘Void’ type functions A function returns a single result, but a void type function does not return anything (but does something, a sub-program)! (‘main’ is just a function that has no formal parameters for the moment )

Example (void): Printing Cards The main() program which calls printcard() #include <iostream> 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 }

(This is a void function - a function that does not return a value) 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"; } (This is a void function - a function that does not return a value)

Example: Total length of a triangle Output: total-length Input: three points: A=(x1,y1), B=(x2,y2), and C=(x3,y3) Total-length = length(AB) + length(AC) + length(AB) Length(AB) = dist(A,B); Length(AC) = dist(A,C); Length(BC) = dist(B,C);

Example: Total length of a triangle #include <iostream> #include <cmath> 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 l12,l23,l13,length; 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;

Two different variables How to communicate? Pass by value! l12 = dist(x1,y1,x2,y2); l23 = dist(x2,y2,x3,y3); l13 = dist(x1,y1,x3,y3); length = l12 + l23 + l13; return 0; } // Function for computing the distance between 2 pts double dist(double fx1, double fy1, double fx2, double fy2) { double dist; dist = sqrt( (fx2-fx1)*(fx2-fx1) + (fy2-fy1)*(fy2-fy1) ); return dist; Two different variables How to communicate? Pass by value! exactly the same! // 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; }

Pass by value

Passing parameters by Value This is by default and desirable behavior! An important fact to remember about parameter passing by value is that changes to the parameters inside the function body have no effect outside of the function. You may think that calling parameters and formal parameters are just different variables.

Example 0 For example, consider the following code: int sum(int a, int b){ a = a + b; return a; } void main(){ int x, y, z; x = 3; y = 5; z = sum(x,y); What is the value of x, y, and z at the end of the main() program?

The answer: 3, 5, and 8. Even though the value of parameter a is changed, the corresponding value in variable x does not change. This is why this is called pass by value. The value of the original variable is copied to the parameter, but changes to the value of the parameter do not affect the original variable. In fact, all information in (local) variables declared within the function will be lost when the function terminates. The only information saved from a pass by value function is in the return statement.

Example 1 An example to show how the function does not affect a variable which is used as a parameter: // Test the effect of a function // on its parameter #include <iostream> using namespace std; void Increment(int Number) { Number = Number + 1; cout << "The parameter Number is: " << Number << endl; }

void main() { int I = 10; //parameter is a variable Increment(I); cout << "The variable I is: " << I << endl; //parameter is a constant Increment(35); //parameter is an expression Increment(I+26); }

Example 2 // Print the sum and average of two numbers // Input: two numbers x & y // Output: sum - the sum of x & y // average - the average of x & y #include <iostream> using namespace std; void PrintSumAve ( double, double ); int main ( ) { double x, y; cout << "Enter two numbers: "; cin >> x >> y; PrintSumAve ( x , y ); }

void PrintSumAve (double no1, double no2) { double sum, average; sum = no1 + no2; average = sum / 2; cout << "The sum is " << sum << endl; cout << "The average is " << average << endl; }

Data areas after call to PrintSumAve() :

Example 3 //Compute new balance at a certain interest rate //Inputs: A positive balance and // a positive interest rate //Output: The new balance after interest was posted #include <iostream> using namespace std; double new_balance(double balance, double rate); /* Returns the balance in a bank account after adding interest. For example, if rate is 5.0, then the interest rate is 5% and so new_balance(100, 5.0) returns 105.00. */

int main(){ double interest, balance; cout << "Enter balance (positive number): "; cin >> balance; if (balance <= 0.0) cout <<"Balance not positive; stopping" << endl; else { cout <<"Enter interest rate (positive number): "; cin >> interest; if (interest <= 0.0) cout << "Interest not positive; stopping" << endl; else cout <<"New balance = $" << new_balance(balance, interest)<< endl; } return 0;

// New balance is computed as balance + balance * %rate double new_balance(double balance, double rate) { double interest_fraction, interest; interest_fraction = rate / 100; interest = interest_fraction * balance; return (balance + interest); }

Summary Function parameters (like variables) Function type (type of the function name) Return statement (the last statement) Finishes the function Return the value to the caller Understanding the ‘flow control’ between ‘main’ and a function Communication: pass by value Empty ‘void’ type

Input: formal parameters Similar to (local) variables

Output: return statement The final result (that should be returned to the calling expression), the end of the ‘sub-programme’ Recommended to use only one ‘return’ as the last statement of the function (for good structure of the programmes) It can in fact appear any places (close friends of ‘break’ and ‘continue’  Example: int max(int a, int b) {if (a>b) return a; else return b;} or int max(int a, int b) {int m; if (a>b) m=a; else m=b; return m;} The type should match that of the function

Function call: pass by value Effective parameters (variables) and formal parameters (variables) are completely different We just copy to or initialize the formal parameters from the effective parameters ‘effective parameters’ are ‘originals’, and ‘formal parameters’ are ‘photocopies’! They are different.