More ‘concepts’ on Function

Slides:



Advertisements
Similar presentations
1 Programming in C++ Lecture Notes 9 Functions (Returning Values) Andreas Savva.
Advertisements

C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
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.
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
Writing and Testing Programs Drivers and Stubs Supplement to text.
Overview creating your own functions calling your own functions.
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.
Function Part II: Some ‘advanced’ concepts on functions.
CS 1400 Chap 6 Functions. Library routines are functions! root = sqrt (a); power = pow (b, c); function name argument arguments.
1 Chapter 9 Scope, Lifetime, and More on Functions.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Chapter 5 Functions For All Subtasks. Void functions Do not return a value. Keyword void is used as the return type in the function prototype to show.
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
Functions—Part I. Slide 2 Where are we now? Simple types of variables 3 program structures cin(>>)/cout(
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
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.
Chapter 6: Programmer- defined Functions Development of simple functions using value and reference parameters JPC and JWD © 2002 McGraw-Hill, Inc. Modified.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
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.
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.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
April 11, 2005 More about Functions. 1.Is the following a function call or a function header? calcTotal(); 2.Is the following a function call or a function.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
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 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 7: User-Defined Functions II
FUNCTIONS In C++.
Chapter 5 Function Basics
Functions and an Introduction to Recursion
Chapter 5 Function Basics
Chapter 5 Functions.
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
User-Defined Functions
User-defined Functions
User Defined Functions
Chapter 5 Function Basics
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.
Name: Rubaisha Rajpoot
6 Chapter Functions.
Functions.
Functions.
User-defined Functions
CS150 Introduction to Computer Science 1
Functions How to reuse code.
Anatomy of a Function Part 1
Pass by Reference.
Local, Global Variables, and Scope
The Function Prototype
Functions and an Introduction to Recursion
Function Defaults C++ permits functions to be declared with default values for some, or all, of its parameters Allows for the function to be called without.
CS150 Introduction to Computer Science 1
COMS 261 Computer Science I
Functions Imran Rashid CTO at ManiWeber Technologies.
CMSC 202 Lesson 6 Functions II.
CS1201: Programming Language 2
Introduction to Functions
Instructor: Hidayah Elias Course: COMPUTER PROGRAMMING (BFC 2042)
More ‘concepts’ on Function
Presentation transcript:

More ‘concepts’ on Function

Declaration vs. definition Before a name (identifier) can be used, it has to be declared: tell the compiler the type: int a; double b; Most declarations are also definitions: allocation of memory. A function is declared by writing down its interface (prototype) A declaration can appear many times (even locally) Compiler does not generate the codes It only checks types and number of arguments A function is defined by writing down its header and body A definition is done only once Compiler generates the codes

A function must be declared before it can be used All functions (like main()) ‘see’ each other Don’t ‘nest’ functions! (you cannot unlike Pascal) All functions are at the same level, global!

Function Prototype The function prototype declares the interface, or input and output parameters of the function, leaving the implementation for the function definition. This separates the concept (‘declaration’) from the implementation (‘definition’)! This leads to ‘separate compilation’ as well!

Equivalent! #include <iostream> using namespace std; // 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); } 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; #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 = 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); Equivalent!

Prototype vs signature Prototype = return type + name + signature The signature of a function is the list of formal parameters, without the name and the return type The prototype is the full ‘ID’ of a function (with the return type)

Function overloading (an OOP concept) Functions may have the SAME name, but different signatures! The ‘conflict’ is AUTOMATICALLY resolved by the difference of the signature! Exact matches between parameter types No exact match: pre-defined rules (type conversion by first widening, then narrowing) We already do it with the operator or function ‘+’!

Good examples int max(int x, int y) { return (x>y) ? x:y;} int max(int x, int y, int z) { return max(max(x,y),z);} double max(double a, double b) { return (a>b) ? a:b;} // return type is not part of the signature // as long as the signature is different, // it’s valid ‘over-loading’ void swap(int& a, int& b) {int temp=a; a=b; b=temp;} void swap(float& a, float& b) {float temp=a; a=b; b=temp;} void swap(double& a, double& b) {double temp=a; a=b; b=temp;}

Bad examples // bad design! Compiler does not know which to call int absolute(int a) {return (a<0) ? –a:a;} int absolute(int& a) {return(a=(a<0)?-a:a);} // wrong! It’s not a valid ‘over-loading’ // as the return type is not part of the signature! // We have two functions having the same signatures, no difference void swap(int& a, int& b) {int temp=a;a=b;b=temp;} int swap(int& a, int& b) {int temp=a;a=b;b=temp;return a;} int one(int x, double y) { return x;} double one(int x, double y) {return y;}

Good design, but bad usage! int test(int a, double b); int test(double a, int b); test(3.2,4.6) is ambiguous! test(3,4) is ambiguous Compilation error! test(3,4.6)  the first test(3.0,4) the second test(‘a’,4.6) the first, ‘a’int

Default function argument We can define ‘default’ parameters for more ‘smartness’ Calling function can have fewer actual parameters (but we can not have fewer formal parameters 

Example int increment(int x, int step = 1) { return (x+step); } Int main() { cout << increment(10) << endl; cout << increment(10,5) << endl; return 0;

The default parameters specified only once, either in declaration or definition, but usually in declaration at the end of formal parameter list not function overloading

Example int increment(int, int=1); // or // int increment(int x, int step=1); int main() { cout << increment(10) << endl; cout << increment(10,5) << endl; return 0; } int increment(int x, int step) { return (x+step);

void swap(int& a, int& b) {int temp=a;a=b;b=temp;} // sort_order is 1 for ascending, otherwise descending void sort(int& x, int& y, int order=1) { if (order == 1) { if (x>y) swap(x,y); } else { if(x<y) swap(x,y); int main() { int a = 24, b=8; sort(a,b); cout << a << b << endl; sort(a,b,2); cout … return 0;