1 Functions Part 1 Prototypes Arguments Overloading Return values.

Slides:



Advertisements
Similar presentations
User Defined Functions
Advertisements

Spring Semester 2013 Lecture 5
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
Chapter 6: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
CS 201 Functions Debzani Deb.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
Chapter 6: User-Defined Functions I
C++ Functions CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
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.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
Chapter 4 Procedural Abstraction and Functions That Return a Value.
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
Functions Structured Programming 256 Chapter 6 Functions g prototypes g arguments g overloading g return values part I.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
1 Functions Chapter 7 2 Hope you can function! What is R2D2 doing here? What is his function? Who is Nibble? Can he function? IS he a function? Who is.
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.
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.
Passing Data - by Reference Syntax && double Pythagorus(double &, double &); Pythagorus(height, base); & & double Pythagorus(double& a, double& b) function.
Functions Pass by reference, or Call by reference Passing addresses Use of & part 3.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
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.
Learners Support Publications Functions in C++
Functions g prototypes g arguments g overloading g return values part I Re-read Section 1.2.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Cosc175/module.ppt1 Introduction to Modularity Function/procedures void/value-returning Arguments/parameters Formal arguments/actual arguments Pass by.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
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: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
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.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Chapter 6: User-Defined Functions I
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Functions prototypes arguments overloading return values part I.
CSCI 161: Introduction to Programming Function
User-Defined Functions
User Defined Functions
Chapter 6: User-Defined Functions I
Presentation transcript:

1 Functions Part 1 Prototypes Arguments Overloading Return values

2 Functions Provide the principle means of organizing and structuring procedural programs. Are self-contained blocks of code, the inner workings of which are hidden to the remainder of the program. Are subprograms in C++. Perform a specific task. Can act on data and return a value. Every C++ program has at least one function: main().

3 Why use functions? Make programs easier to: write save repeated typing of blocks of code debug isolate error sources maintain Programs are clearer Make programs more reliable Testing of individual functions rigorously Enable programming in the large. Structured programming Provide some reuse of code Build user defined libraries *

4 Types of functions Predefined -- found in the header files pow(x,n), strlen(word), etc. User-defined -- today’s topic DisplayMenu(); F(x);

5 Types of Functions Procedural functions general purpose, does not have to compute a value. Communication via parameters usually called on their own name Verb Noun e.g. PrintReport(); Functional functions focus on return value usually right hand side of = name describes return data description cos(x), exp(x), NextCutomerID()

6 Good Practice Make function names descriptive Procedural verb followed by a noun Functional describe return value Avoid wishy-washy names DoIt() Go() DealWithInput() Make sure each function does only one thing sin(), GetCustomerName(), EraseFile(), DisplayMenu() GetNameAndChangePhoneNumberAndPrintReport();

7 Function properties When you use a function you call the function May be passed data called arguments to function parameters May return directly a value to the calling program using return statement. May return indirectly more than one value via parameters and arguments pass by reference Can not change arguments passed to a parameter. Pass by value

8 Functions #include void demofunction(void); int main(void) { cout << "In main\n"; demofunction(); cout << "Back in main\n"; return 0; } void demofunction(void) { cout << "In DemoFunction\n"; cout << “Still in function.\n”; } Prototype Call Definition (called) Demonstrate using debugger Caller

9 Function Prototype (declaration ) double Pythagorus( double, double ); data types Semicolon! do not need to specify parameter names! BUT DO IT ANYWAY

10 Function Prototype (declaration ) double Pythagorus( double B, double C); data types Semicolon! do not need to specify parameter names! BUT DO IT ANYWAY

11 Purpose of Prototypes A function cannot be called unless it has been declared. It describes the identifier return type and parameters types it therefore ensure it is called correctly. They enable access to functions written in other source files

12 Function Definition double Pythagorus(double B, double C) { double A; A = sqrt(B*B + C*C); return A; } * no semicolon! The instructions needed to complete sub-task parameter names must be specified Local variable

13 Function Call #include double Pythagorus(double, double); int main(void){ cout << “The hypotenuse is “<< Pythagorus(3, 4); return 0; } double Pythagorus(double B, double C) { double A; A = sqrt(B*B + C*C); return A; } OUTPUT The hypotenuse is 5 Demonstrate! Values passed to function Values received by arguments a and b

14 Program Structure #include function prototypes; Global constants int main(void){ variable declarations; statements [including function calls] return 0; } function definition(s)

15 Function Prototypes Examples double Pythagorus(double, double); void DisplayMenu(void); double times_em(int, int, int, int); double Logistic(double); void print_data(char, double);

16 Returning Values A function can receive many values Only one value can be directly returned

17 Returning Values used in “functional” functions The return statement: As in return 0 in main tells the function which value to send back to the calling function terminates the function call and returns immediately to the calling program note void functions do not need a return;

18 Return Statement Syntax return expression; Examples return c; return hypotenuse; return n*n*n; return status;

19 Return Statement int find_max(int x, int y) { int maximum; if (x >= y) maximum = x; else maximum = y; return maximum; } same data type *

20 Passing Data passing by value passes a single value to receiving parameter does not change value of variables referred to in call. passing by reference Changes value of variables referred to indirectly may change any number of variables. Accomplished by using references (this topic) using pointers (later topic) *

21 Passing by value before call double Biggest(double x, double y) { do stuff; } int main() { double M=2,N=7; double Max = 0 Max = Biggest(M, N); return 0; } M N Max

22 Passing by value during call double Biggest(double x, double y) { do stuff; } x y int main() { double M=2,N=7; double Max = 0 Max = Biggest(M, N); return 0; } M N Max

23 Passing by value after call double Biggest(double x, double y) { do stuff; } int main() { double M=2,N=7; double Max = 0 Max = Biggest(M, N); return 0; } M N Max

24 Passing Data - by Value passing by value: copy A copy of a value is passed from the calling function to the called function. * double Pythagorus(double B, double C){ double A A = sqrt(B*B + C*C); return A; }

25 x=865 find_max(x, y) y=9090 * find_max(firstnum, secnum); call to find_max value in first_num is passed value in sec_num is passed Storing Values into Parameters firstnum = 865 secnum = 9090 arguments parameters

26 Passing Data - by Value void DoubleMe(int); // function prototype int main(void){ int w = 3; cout <<"w before the function call is "<<w<<‘\n’; DoubleMe(w); cout <<"w after the function call is "<<w<<‘\n’; return 0; } void DoubleMe(int q) { cout<<"Value passed to the function is "<<q<<endl; q = q *2;// doubles the value cout<<"Value at the end of the function is "<< q <<endl; } Demonstrate Chris

27 Passing Data - by Reference to change value of calling variable not in C Syntax &B&C double Pythagorus(double &B, double &C); Pythagorus(height, base); & B& C double Pythagorus(double& B, double& C) function prototype function call function definition note use of & tells compiler to pass by reference

28 Passing Data - by Reference * void DoubleMe(int &); // function prototype int main(void){ int w = 3; cout <<"w before the function call is "<<w<<‘\n’; DoubleMe(w); cout <<"w after the function call is "<<w<<‘\n’; return 0; } void DoubleMe(int & q) { cout<<"Value passed to the function is "<<q<<endl; q = q *2;// doubles the value cout<<"Value at the end of the function is "<< q <<endl; } A reference to the address of w is passed q is an alias for calling variable it has the same address

29 Passing by reference before call void DoubleMe(double & x) { x = 2*x; } int main() { double M=2; DoubleMe(M); return 0; } M void DoubleMe(double & x) ;

30 Passing by reference during call void DoubleMe(double & x) { x = 2*x; } x int main() { double M=2,N=7; double Max = 0 DoubleMe(M); return 0; } M

31 Passing by reference after call void DoubleMe(double & x) { x = 2*x; } int main() { double M=2; DoubleMe(M); return 0; } M

32 Swap Routine (Classic) void swap(float& num1, float& num2) { float temp; temp = num1; num1 = num2; num2 = temp; }

33 Passing by reference before call M void Swap(double & x, double & y) { double temp; temp = x; x = y; y = temp; } int main() { double M=2; N = 7; Swap(M,N); return 0; } N

34 Passing by reference during call x M void Swap(double & x, double & y) { double temp; temp = x; x = y; y = temp; } int main() { double M=2; N = 7; Swap(M,N); return 0; } N y temp

35 Passing by reference during call x M void Swap(double & x, double & y) { double temp; temp = x; x = y; y = temp; } int main() { double M=2; N = 7; Swap(M,N); return 0; } N y temp

36 Passing by reference during call x M void Swap(double & x, double & y) { double temp; temp = x; x = y; y = temp; } int main() { double M=2; N = 7; Swap(M,N); return 0; } N y temp

37 Passing by reference during call x M void Swap(double & x, double & y) { double temp; temp = x; x = y; y = temp; } int main() { double M=2; N = 7; Swap(M,N); return 0; } N y temp

38 Passing by reference after call int main() { double M=2; N = 7; Swap(M,N); return 0; } M N void Swap(double & x, double & y) { double temp; temp = x; x = y; y = temp;; }

39 Demo swap using call by reference and by value

40 What’s happening ?

41 Passing by value before call M void Swap(double x, double y) { double temp; temp = x; x = y; y = temp; } int main() { double M=2; N = 7; Swap(M,N); return 0; } N

42 Passing by value during call x M void Swap(double x, double y) { double temp; temp = x; x = y; y = temp; } int main() { double M=2; N = 7; Swap(M,N); return 0; } N y temp

43 Passing by value during call x M void Swap(double x, double y) { double temp; temp = x; x = y; y = temp; } int main() { double M=2; N = 7; Swap(M,N); return 0; } N y temp

44 Passing by value during call x M void Swap(double x, double y) { double temp; temp = x; x = y; y = temp; } int main() { double M=2; N = 7; Swap(M,N); return 0; } N y temp

45 Passing by value during call x M void Swap(double x, double y) { double temp; temp = x; x = y; y = temp; } int main() { double M=2; N = 7; Swap(M,N); return 0; } N y temp

46 Passing by value after call int main() { double M=2; N = 7; Swap(M,N); return 0; } M N void Swap(double x, double y) { double temp; temp = x; x = y; y = temp;; }

47 Data Type Mismatch value parameters implicit type conversion - value of the actual argument is coerced to the data type of the formal parameter reference parameters no coercion because an address is passed, not a value *

48 A Comparison formalactual parameter isargument may be valuevariable, constant, or expression type coercion may take place referenceargument only of exact same type as formal