Computer Science 1620 Function Overloading. Review Question: suppose I have the following function: can I call this function with an integer? yes – compiler.

Slides:



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

Chapter 4 Parameters and Overloading. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-2 Learning Objectives Parameters Call-by-value Call-by-reference.
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Functions Prototypes, parameter passing, return values, activation frams.
Review What is a virtual function? What can be achieved with virtual functions? How to define a pure virtual function? What is an abstract class? Can a.
AU/MITM/1.6 By Mohammed A. Saleh 1. Arguments passed by reference  Until now, in all the functions we have seen, the arguments passed to the functions.
Single-Result Functions Section /25/11. Programming Assignment On website.
Chapter 5 Functions.
Computer Science 1620 Default Parameter Values. Default Parameters If a projectile is launched vertically with velocity v 0, the maximum height it will.
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
Computer Science 1620 Other Data Types. Quick Review: checklist for performing user input: 1) Be sure variable is declared 2) Prompt the user for input.
Function Overloading Can enables several function Of same name Of different sets of parameters (at least as far as their types are concerned) Used to create.
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
Function Overloading Can enables several functions Of same name Of different sets of parameters (at least as far as their types are concerned) Used to.
CPSC230 Computers & Programming I Lecture Notes 20 Function 5 Dr. Ming Zhang.
Computer Science 1620 Function Scope & Global Variables.
Computer Science 1620 Programming & Problem Solving.
Computer Science 1620 Arrays. Problem: Given a list of 5 student grades, adjust the grades so that the average is 70%. Program design: 1. read in the.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
Call-by-Value vs. Call-by-Reference Call-by-value parameters are used for passing information from the calling function to the called function (input parameters).
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Lecture-13 Instructor Name: Muhammad Safyan Programming Fundamental.
CHAPTER 3 Function Overloading. 2 Introduction The polymorphism refers to ‘one name having many forms’ ‘different behaviour of an instance depending upon.
CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get.
CS212: Object Oriented Analysis and Design Lecture 9: Function Overloading in C++
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPS120: Introduction to Computer Science Functions.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
1 CS161 Introduction to Computer Science Topic #10.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
Supervisor Ebtsam AbdelHakam Department of Computer Science Najran University 24/2/2014 Ebtsam Abdelhakam 1.
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
CSC 143F 1 CSC 143 Constructors Revisited. CSC 143F 2 Constructors In C++, the constructor is a special function automatically called when a class instance.
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.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Overloading Methods In Java it is possible to define two or more methods within the same class that share the same name, as long as their parameter declarations.
1 Advanced Topics in Functions Lecture Unitary Scope Resolution Operator Unary scope resolution operator ( :: )  Access global variable if.
Method OverloadingtMyn1 Method overloading Methods of the same name can be declared in the same class, as long as they have different sets of parameters.
Templates Where the TYPE is generic. Templates for functions Used when the you want to perform the same operation on different data types. The definition.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
Programming Languages -2 C++ Lecture 3 Method Passing Function Recursion Function Overloading Global and Local variables.
Lecture 17: 4/4/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Functions Modules in C++ are called functions and classes. Main reason to use functions is : – get aid in conceptual organization.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Chapter 5 Function Basics
Zhen Jiang West Chester University
Operator Overloading
Parameters and Overloading
CS150 Introduction to Computer Science 1
CS149D Elements of Computer Science
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Really reusable software
Functions Imran Rashid CTO at ManiWeber Technologies.
Single-Result Functions & Modularity
TOPIC: FUNCTION OVERLOADING
Unit-1 Introduction to Java
CS150 Introduction to Computer Science 1
COP 3330 Object-oriented Programming in C++
Presentation transcript:

Computer Science 1620 Function Overloading

Review Question: suppose I have the following function: can I call this function with an integer? yes – compiler will promote integer to double same thing applies to demotion double square(double x) { return x * x; } int main() { cout << square(3) << endl;

Function Overloading Suppose you are in charge of writing a maximum function The program must work with three different types: int double float

Function Overloading First Way: double maximum(double n1, double n2) { if (n1 > n2) return n1; else return n2; }

Function Overloading The previous example works fine, but inefficient consider following call int x, y; cin >> x >> y; int z = maximum(x,y); consider what happens when compiling the program the values x and y must be converted to type double in order to use the function the return value is of type double … which must be converted back to an integer (which generates a compiler warning)

Function Overloading Second Try: int maximum(int n1, int n2) { if (n1 > n2) return n1; else return n2; }

Function Overloading The previous example works fine for integers, but what about doubles? consider following call double x, y; cin >> x >> y; double z = maximum(x,y); cout << z << endl; suppose I run this program, and type in the values 4.6 and 7.4 what will the output be?

Function Overloading Second try: double maximumDouble(double n1, double n2) { if (n1 > n2) return n1; else return n2; } float maximumFloat(float n1, float n2) { if (n1 > n2) return n1; else return n2; } int maximumInt(int n1, int n2) { if (n1 > n2) return n1; else return n2; }

Function Overloading We can now change our original call int x, y; cin >> x >> y; int z = maximumInt(x,y); no conversions take place some problems though what if a programmer writes a program using integers … and the supervisor decides to use doubles instead not only are you changing your variable types but now you have to change all of your calls to maximum

Function Overloading Solution: Function overloading C++ allows you to use the same function name for different functions the functions must have different parameter lists the function being called will depend on the arguments being sent

Function Overloading Last try: double maximum(double n1, double n2) { if (n1 > n2) return n1; else return n2; } float maximum(float n1, float n2) { if (n1 > n2) return n1; else return n2; } int maximum(int n1, int n2) { if (n1 > n2) return n1; else return n2; }

Function Overloading Now when we make the following call: int x, y; cin >> x >> y; int z = maximum(x,y); no conversions take place if we wanted to change to doubles, we need only change the variable type double x, y; cin >> x >> y; double z = maximum(x,y);

Function Overloading Resolving overloaded definitions the compiler must be able to choose between two function declarations always chooses the best match overload resolution is a complex task beyond scope of class suffices to know that it tries for closest match