Functions. COMP104 Lecture 13 / Slide 2 Function Prototype * The function prototype declares the interface, or input and output parameters of the function,

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

1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Programming Functions: Passing Parameters by Reference.
Local and Global Variables. COMP104 Local and Global / Slide 2 Scope The scope of a declaration is the block of code where the identifier is valid for.
Introduction to Functions Programming. COMP102 Prog Fundamentals I: Introduction to Functions /Slide 2 Introduction to Functions l A complex problem is.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
Computer Science 1620 Loops.
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.
Programming Scope of Identifiers. COMP102 Prog. Fundamentals I: Scope of Identifiers/ Slide 2 Scope l A sequence of statements within { … } is considered.
Passing Arrays to Functions. COMP104 Lecture 16 / Slide 2 Array Element Pass by Value * Individual array elements can be passed by value or by reference.
Functions:Passing Parameters by Value Programming.
Functions. COMP104 Lecture 13 / Slide 2 Review of Array: Bubble Sort for (j=0; j List[j+1]) swap(List[j], List[j+1]); }
Local, Global Variables, and Scope. COMP104 Slide 2 Functions are ‘global’, variables are ‘local’ int main() { int x,y,z; … } int one(int x, …) { double.
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
Pass by Reference. COMP104 Pass by Reference / Slide 2 Passing Parameters by Reference * To have a function with multiple outputs, we have to use pass.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Chapter 6: Functions.
Programming Functions: Passing Parameters by Reference.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Functions g g Data Flow g Scope local global part 4 part 4.
COMP103 - C++ Review1 Variables and Special Chars (Ch. 2, 3)
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Functions—Part I. Slide 2 Where are we now? Simple types of variables 3 program structures cin(>>)/cout(
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
CSE1222: Lecture 3The Ohio State University1. Assignment Operations  The C++ assignment operator is: =  Examples: x = 3 * 5; y = x – 7; y = y + 4; Do.
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.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
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.
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
Functions Overview Functions are sequence of statements with its own local variables supports modularity, reduces code duplication Data transfer between.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
L what are predefined functions? l what is? n function name n argument(s) n return value n function call n function invocation n nested function call l.
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Function Definition: Return_DT F_name (
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
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.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
C++ Programming Lecture 12 Functions – Part IV
User-Defined Functions (cont’d) - Reference Parameters.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
 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.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
-Neelima Singh PGT(CS) KV Sec-3 Rohini
What Actions Do We Have Part 1
Functions Chapter 5 CS12 - Computer Programming 1 Chapter 5.
Predefined Functions Revisited
Intro to Programming Week # 6 Repetition Structure Lecture # 10
A Lecture for the c++ Course
CS 1430: Programming in C++.
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Return_DT F_name ( list of formal parameters)
Functions.
Functions.
Pass by Reference.
The Function Prototype
Predefined Functions Revisited
Functions Imran Rashid CTO at ManiWeber Technologies.
Introduction to Functions
Presentation transcript:

Functions

COMP104 Lecture 13 / Slide 2 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 Lecture 13 / Slide 3 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 Lecture 13 / Slide 4 Function Call * A function call has the following syntax: ( ); e.g. int square_root = sqrt(2); n There is a one-to-one correspondence between the parameters in a function call and the parameters in the function definition.

COMP104 Lecture 13 / Slide 5 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 Lecture 13 / Slide 6 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 Lecture 13 / Slide 7 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 Lecture 13 / Slide 8 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 Lecture 13 / Slide 9 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; }

COMP104 Lecture 13 / Slide 10 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function) * One of the statements in the function body should have the form: return ; * The value passed back by return should have the same type as the function.

COMP104 Lecture 13 / Slide 11 Pass by Value * 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.

COMP104 Lecture 13 / Slide 12 Pass by Value: Example 0 * For example, consider the following code: int sum(int x, int y){ x = x + y; return x; } 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?

COMP104 Lecture 13 / Slide 13 void main(){ int x, y, z; x = 3; y = 5; z = sum(x,y); } x y 35 int sum(int x, int y){ x = x + y; return x; } x y 35 x y 85 Pass by Value: Example 0

COMP104 Lecture 13 / Slide 14 Pass by Value: Example 0 * The answer: 3, 5, and 8.  Even though the value of function parameter x is changed, the corresponding value in variable x in the main function 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.

COMP104 Lecture 13 / Slide 15 Pass by Value: 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 using namespace std; void Increment(int Number) { Number = Number + 1; cout << "The parameter Number is: " << Number << endl; }

COMP104 Lecture 13 / Slide 16 Pass by Value: Example 1 void main() { int I = 10; //parameter is a variable Increment(I); cout << "The variable I is: " << I << endl; //parameter is a constant Increment(35); cout << "The variable I is: " << I << endl; //parameter is an expression Increment(I+26); cout << "The variable I is: " << I << endl; }

COMP104 Lecture 13 / Slide 17 Pass by Value: Example 1

COMP104 Lecture 13 / Slide 18 Pass by Value: 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 using namespace std; void PrintSumAve ( double, double ); void main ( ) { double x, y; cout << "Enter two numbers: "; cin >> x >> y; PrintSumAve ( x, y ); }

COMP104 Lecture 13 / Slide 19 Pass by Value: Example 2 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; }

COMP104 Lecture 13 / Slide 20 Pass by Value: Example 2  Data areas after call to PrintSumAve() :

COMP104 Lecture 13 / Slide 21 Pass by Value: 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 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 */

COMP104 Lecture 13 / Slide 22 Pass by Value: Example 3 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; }

COMP104 Lecture 13 / Slide 23 Pass by Value: Example 3 // 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); } /* New balance is computed as balance * (1 + %rate) double new_balance(double balance, double rate) { double interest_fraction, updated_balance; interest_fraction = rate / 100; updated_balance = balance * (1 + interest_fraction); return updated_balance; } */

COMP104 Lecture 13 / Slide 24 Pass by Value: Example 4 // Input: inches // Output: feet and inches #include using namespace std; // Function prototypes int feet(int); int rinches(int);

COMP104 Lecture 13 / Slide 25 Pass by Value: Example 4 void main() { int inches; // Number of inches cout << "Enter number of inches to convert: "; cin >> inches; cout << "Result is " << feet(inches) << " feet " << rinches(inches) << " inches" << endl; } int feet(int inches) { return inches / 12; } int rinches(int inches) { return inches % 12; }

COMP104 Lecture 13 / Slide 26 Pass by Value: Example 5 // File i2f.h int feet(int); int rinches(int); // File i2f.cpp // Functions for Converting inches to feet int feet(int inches) { return inches / 12; } int rinches(int inches) { return inches % 12; }

COMP104 Lecture 13 / Slide 27 Pass by Value: Example 5 // File main.cpp // Input inches // Output feet and inches #include using namespace std; #include "i2f.h" void main() { int inches; // Number of inches cout << "Enter number of inches to convert: "; cin >> inches; cout << "Result is " << feet(inches) << " feet " << rinches(inches) << " inches" << endl; }