Functions. Introduction A sequence of statements that perform some related and useful processing. Functions have 3 components :- ( ) Note :- Declaration.

Slides:



Advertisements
Similar presentations
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Advertisements

What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Chapter 7: User-Defined Functions II
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
C++ Pointer and Functions
 2006 Pearson Education, Inc. All rights reserved Templates.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
OOP Spring 2007 – Recitation 21 Object Oriented Programming Spring 2007 Recitation 2.
Parameter Passing. Expressions with lvalues and rvalues An expression has an lvalue/rvalue if it can be placed on the left/right side of an assignment.
Miscellaneous topicsCS-2301 B-term Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The C Programming.
Operator Overloading in C++
Review of C++ Programming Part II Sheng-Fang Huang.
Operator Overloading and Type Conversions
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
EECE 310: Software Engineering Lecture 2: Understanding Objects in Java and Types.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
1 Homework / Exam Finish up K&R Chapters 3 & 4 Starting K&R Chapter 5 Next Class HW4 due next class Go over HW3 solutions.
CHAPTER 3 Function Overloading. 2 Introduction The polymorphism refers to ‘one name having many forms’ ‘different behaviour of an instance depending upon.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
18. DECLARATIONS.
Learners Support Publications Classes and Objects.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Learners Support Publications Functions in C++
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
1 Functions in C++ Default arguments Overloading Inlining Call-by-reference Scope of variables, static, extern namespace new and delete assert.
SNU OOPSLA Lab. 7. Functions © copyright 2001 SNU OOPSLA Lab.
Functions Sujana Jyothi C++ Workshop Day 2. Functions 3 Parameter transmission modes pass by value (default) pass by reference (&) pass by const reference.
Function Overloading and References
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
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.
Functions. Predefined Functions C++ comes with libraries of code that can be reused in your programs. The code comes in the form of predefined functions.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
C# Programming Methods.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
ECE 103 Engineering Programming Chapter 41 C Pointers, Part 3 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Welcome to FUNCTION OVERLOADING Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS) KV jhagrakhand.
 2006 Pearson Education, Inc. All rights reserved Templates.
C++ Functions A bit of review (things we’ve covered so far)
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Programming with ANSI C ++
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Introduction to C++ Systems Programming.
Programming with ANSI C ++
FUNCTIONS In C++.
Constructor & Destructor
Programming Fundamentals Lecture #7 Functions
C Basics.
Lecture 6 C++ Programming
Pointers and References
Operator overloading Dr. Bhargavi Goswami
Parameters and Overloading
Java Programming Language
Presentation transcript:

Functions

Introduction A sequence of statements that perform some related and useful processing. Functions have 3 components :- ( ) Note :- Declaration does not allocate any space to the argument list and thus, the argument names are ignored. Example) char* strcpy(char* to,char* from)

Function Definition Actual body of code for the function. Names all or some of the function parameters. It is possible to have unnamed parameters for future use. Example) void Foo(int N,char C,float) { if(N>30 && N<100) N=N-30; if(C>='a' && C<='z') C=C-30; }

Argument Passing A store is set aside for the formal arguments and each formal argument’s memory location is initialized by the actual argument value. Type-Checking is performed and all promotions,standard conversions and user- defined type-conversions are performed. Local copy vs. original copy.

example void f(int val,int& ref) { val++; ref++; } void g() { int i=1; int j=1; f(i,j); }

const and non-const arguments const arguments :- Safeguards programs from modifying original parameter incase of pass-by- reference. Example) ex.1) int strcmp(const char*,const char*); //compares 2 strings.Care is taken to safeguard from accidental change to the original strings ex.2) float fsqrt(const float& rr); void g(double d) { float r=fsqrt(2.0f); //pass by ref to temp holding 2.0f r=fsqrt(r); //pass reference to r r=fsqrt(d); //pass reference to temp holding float(d) }

Array Arguments Argument of array type (i.e. T[]) is intrinsically converted to T* before passing to a function. Thus, an array CANNOT be passed by value. Use const modifier is necessary. Example) void Foo1(int* A); void Foo2(int[] A); void g() { int A[]={2,3,4,5}; Foo1(A); Foo2(A); }

Return Value Return type must be specified, if none then specify as void. Recursion. A return statement is considered to initialize an unnamed, temporary variable of the return type. Type checking for return values is done. Do not attempt to return references to local function variables.

example ex.1) int fac(int n) { if(n>1) return n * fac(n-1); return 1; } ex.2) double f(){....; return 1;} //1 is implicitly converted to double(1)

Function Overloading Functions that have same “Function Name” but different argument-lists. Basic intention is to allow functions to perform semantically similar actions n different contexts. Return types are not taken into account. Functions in different scopes do not overload.

example void print (int); //print an int void print(const char*); //print a string float sqrt(float); double sqrt(double); void f() { float a=9.3f; double b=34.55; a=sqrt(a); //calls sqrt(float) b=sqrt(b); //calls sqrt(double) }

example (cont.) void f(int); void g() { void f(double); f(1); //calls f(double), no ambiguity }

Overloaded Function Resolution Rules to find the best-match between the type of passed argument and type of formal argument. Rules :- –Exact Match; trivial or no conversions (array name to pointer, function name to pointer and T to const T) –Match using promotions; integral promotions, float to double, double to long double. –Match using standard conversion; (int to double, double to int) –Match using user-defined conversions. –Match using ellipses.

example void print(int); void print(const char*); void print(double); void print(char); void h(char c,int i,short s,float f) { print(c); //exact match: invoke print(char) print(i); //exact match: invoke print(int) print(c); //integral promotion match: invoke print(int) print('a'); //exact match: invoke print(char) print(49); //exact match: invoke print(int) print(0); //exact match: invoke print(int) print("a"); //exact match: invoke print(const char*) }

Manual Ambiguity Resolution Too few or too many overloaded functions Try to analyze and resolve ambiguities manually. Try to use non-overloaded functions. void f1(char); void f1(long); void f2(char*); void f2(int*); void k(int i) { f1(i); //ambiguos : f1(char) or f1(long) f2(0); //ambiguos : f2(char*) or f2(int*) }

Resolution of Multiple Arguments A function that is the best match for one argument and a better than or equal match for all other arguments is called. int pow(int,int); int pow(double,double); void Foo(double,float); void Foo(int,int); void k() { int r=pow(2,2); int r=pow(2.0,2.0); int r=pow(2.0,2); //error Foo(2.0,2); //Foo(double,float is called }

Default Argument Allows certain formal arguments to be initialized with some default value incase the call does not explicitly pass values for every argument. Aimed at having functions that have a short and simple form without overloading. Default arguments are type-checked at the time of function declaration and evaluated at the time of function call. Only trailing arguments can be made default.

example void print(int value,int base=10); void f() { print(31); print(31,10); print(31,16); print(31,2); } Output :: f int f(int,int=0;char* =0); //ok.note the space between * and = int f(int=0,int=0;char*); //error int f(int=0,int;char* =0); //error

Unspecified Arguments For those functions for which all argument type may not be specified. The compiler has no information to process the unknown portion of the argument list. Completely programmer dependent. library is used.

void error(int severity...) //"severity followed by zero- terminated list of char*'s { va_list ap; //container for the argument list va_start(ap,severity); //load the argument list for(;;) { char* p=va_arg(ap,char*); if(p==0) break; cerr<<p<<' '; } va_end(ap); //cleanup }

Macros #define preprocessor directive to produce text replacement. Processed by the preprocessor and no participation from the compiler. No C++ type check or scope rules. Overloading and recursion are not allowed. Use inline functions instead.

example #define MAC(x,y) x+y #define SQUARE(a) a*a int m=10; int l = SQUARE(m+2);

Inline Functions The “inline” keyword is a hint to the compiler that it should replace the function call by function code. No guarantee that inline behavior will be followed. inline functions behave like normal functions other than their call. inline int Foo(int N) { return (N*N)/(N-1); }