Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science.

Slides:



Advertisements
Similar presentations
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Advertisements

C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Chapter 6: User-Defined Functions I
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Programmer-defined functions Development of simple functions using value parameters.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
Chapter 6: User-Defined Functions I
CHAPTER:09 METHODS(FUNCTIONS) IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
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.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
1 Programming in C++ Dale/Weems/Headington Chapter 7 Functions.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Chapter 6: Functions Starting Out with C++ Early Objects
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.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Functions every C++ program must have a function called main program execution always begins with function main any other functions are subprograms and.
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.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
1 Remember: Examination is a chance not ability. By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
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.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
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.
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.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
1 MORE ON MODULAR DESIGN: MODULE COMMUNICATIONS. 2 WHEN A FUNCTION IS INVOKED, MEMORY IS ALLOCATED LOCALLY FOR THE FORMAL PARAMETERS AND THE VALUE OF.
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.
Programming Fundamentals Enumerations and Functions.
Chapter 6 Functions. 6-2 Topics 6.1 Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5.
 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.
Chapter 9: Value-Returning Functions
Chapter 6: User-Defined Functions I
Chapter 7: User-Defined Functions II
IS Program Design and Software Tools Introduction to C++ Programming
Function There are two types of Function User Defined Function
Functions.
User-Defined Functions
Chapter 5 - Functions Outline 5.1 Introduction
User Defined Functions
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.
Chapter 7: User-Defined Functions II
Chapter 6: User-Defined Functions I
Functions Divide and Conquer
COMS 261 Computer Science I
CS1201: Programming Language 2
FOR statement a compact notation for a WHILE e.g. sumgrades = 0;
CPS125.
Functions Chapter No. 5.
Presentation transcript:

Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science Kendriya Vidyalaya VVNagar

Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 USE DEFINED FUNCTIONS C++

Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 FUNCTIONS  A Function groups a number of program statements into a single unit and gives it a name. It takes arguments and returns a value.  This unit can be called (invoked) from other parts of the program. Functions are the building blocks of C++ programs where all the program activity occurs.  Functions serve two purposes. They allow a programmer to say: `this piece of code does a specific job which stands by itself and should not be mixed up with anyting else', and they make a block of code reusable since a function can be reused in many different contexts without repeating parts of the program text.

Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Need for a Function  Monolethic program (a large single list of instructions) becomes difficult to understand. For this reason functions are used.  A Function has a clearly-defined objective (purpose) and a clearly-defined interface with other functions in the program.  A group of functions together is called a module.  Reduction in program size is the another reason. Any sequences of statements that is repeated in a program can be combined together to form a function. The function code has only one copy in memory, even though it may be executed as many times as a user needs.

Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Advantages of using functions in C++  A complex program can be divided into small subtasks and function subprograms can be written for each.  These are easy to write, understand and debug  A function can be utilized in many programs by separately compiling it and loading them together.  C++ has the facility of defining a function in terms of itself i.e. recursion.  Many functions such as cin, cout, sqrt etc. are kept in C++ library and the compiler of C++ is written for calling any such function.

Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Types of Functions  1.Library Functions / Built in Functions Many functions are in built with C++ language by default. They are library functions and stored in the header files of C++. Eg. pow(), getch(), exit(), etc.  2. User Defined Functions User can create a new functions as per his/her requirement

Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Function Prototype  Like any variable in a C++ program it is necessary to prototype or declare a function before it’s use.  It informs the compiler that the function would be referenced at a later stage in the program.  Eg. void display_message(); is a function prototype or a declaration. Here void specifies that this function does not return any value.

Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Defining a Function  A Function must be defined prior to it’s use in the program. It contains the code of the function.  Syntax of a function definition : Type Name_of_the_function (argument list) { // body of the function }

Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Defining a Function  Example : int Sum (int a, int b) { int c; c = a + b; return c; }

Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Function Call / Invoke a Function  A Function can be used by calling it or invoking it in the main program.  Eg. Total = sum(5, 9);

Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Example of A Function #include int Cube(int); void main() { int number, cube; cout << “\n Enter No :”; cin >> number; cube = Cube(number); cout << “\n Cube of the no is : “ << cube; } Int Cube(int n) { return n * n * n; }

Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Arguments  The inputs which are given to the functions are known as Function Arguments.  Constant Arguments  Default Arguments

Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Arguments Constant Arguments void max (const float x, const float y); Here, the qualifier const informs the compiler that the arguments having const should not be modified by the function max().

Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Arguments Default Arguments C++ allows a function to assign a parameter the default value in case no argument for that parameter is specified in the function call. void pattern (char M, int B = 2); to print the character M for B times. if 4) then it four times. if pattern(‘*’) then it printe * two times.

Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Calling Functions Function can be called by two ways. 1. Call by Value 2. Call by Reference

Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Call by Value Here the function is called by the value of arguments. Here two words are used Actual Parameter and Formal Parameter. The parameter passed in function call is known as Actual Parameter, while the parameter declared in the definition is called as Formal Parameter.

Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Call by Value #include int Sum(int, int); void main() { int n1, n2, Total; cout > n1; cout > n2; Total = Sum(n1, n2);// n1, n2 : Actual Parameters cout << “\n Total : “ << Total; } int Sum (int a, int b)// a, b : Formal Parameters { return a + b; }

Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Call by Reference A reference provides an alias – a different name – for the variable, that is, the same variable’s value can be used by two different names : the original name and the alias name. In call by reference method, a reference to the actual arguments in the calling program is passed (only variables). So the called function does not create its own copy of original value(s) but works with the original values with different name.

Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Call by Reference #include void main() { int n1, n2, Total; int Sum(int &a, int &b); cout > n1; cout > n2; Total = Sum(n1, n2); cout << “\n Total : “ << Total; } int Sum (int &a, int &b) { return a + b; }

Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 How control passed between functions #include int Sum(int, int); void main() { int n1, n2, Total; cout << “\n Enter No 1 :”; cin >> n1; cout << “\n Enter No 2 :”; cin >> n2; Total = Sum(n1, n2); cout << “\n Total : “ << Total; } int Sum (int a, int b) { return a + b; }

Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Returning from Function…  Return Statement is used to return a value from function. int fun() { … return or ; }  A function can return only a single value.  There may be three types of functions :  Computational Functions : Calculate some value and return it.  Manipulative Functions : Manipulate information and return success or failure code.  Procedural Functions : Perform an action and have no explicit return value.

Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Returning (by) Reference  A fuction may also return a reference. int & min (int & a, int & b) { if (a < b) return a; else return b; }  This function returns reference to a or b rather than returning values.  For eg. min (x, y) = -5;  It assigns -5 to lesser of the two x and y.

Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Function Classification… Function can be classified into four categories as per the arguments and return values. 1.No Arguments and No Return Values 2.No Arguments but Return Values 3.Arguments but No Return Values 4.Arguments and Return Values

Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 No Arguments No Return Values #include void print_name(); void main() { print_name(); } void print_name() { cout << “\n Sachin Tendulkar”; }

Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 No Arguments But Return Values #include int get_number(); void main() { int n; n = get_number(); } int get_number() { int a; cout > a; return a; }

Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Arguments but No Return Values #include void print_name(int); void main() { print_name(10); } void print_name(int n) { for (int i= 1; i<=n; i++) cout << “\n Sachin Tendulkar”; }

Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Both Arguments and Return Values #include int Sum(int, int); void main() { int n1, n2, Total; cout > n1; cout > n2; Total = Sum(n1, n2);// n1, n2 : Actual Parameters cout << “\n Total : “ << Total; } int Sum (int a, int b)// a, b : Formal Parameters { return a + b; }

Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Scope Rules The program part (s) in which a particular piece of code or a data value (variable) can be accessed is known as the piece-of-code’s or variable’s scope. Four kinds of Scope in C++ : (i)Local Scope (Block) (ii) Function Scope (iii) File Scope (iv) Class Scope

Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Scope of Variable #include int global_var; // Global Variable void main() { int local_var; // Local Variable/Fun Scope cout << ::global_var; // :: - scope resolution operator { char Block_var; // Block Variable … } void fun() { float fun_var; // Function Scope }

Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Example of Scope Find out the output of the following code : #include int a = 20; void main() { int a = 50; cout << ::a << “\n” << a; }

Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Example of Scope Find out the output of the following code : #include int a = 20; void main() { int a = 50; cout << ::a << “\n” << a; } Output : 20 50

Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Example of Scope Find out the Output for the following : #include int g=10; void func(int &x, int y) { x = x - y; y = x *10; cout<<x<<“ – “<<y<<endl; } void main( ) { int g=7; func(::g, g); cout<<g<<“ – “<<::g<<endl; func(g, ::g); cout<<g<<“ – “<<::g<<endl; }

Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Example of Scope Find out the Output for the following : #include int g=10; void func(int &x, int y) { x = x - y; y = x *10; cout<<x<<“ – “<<y<<endl; } void main( ) { int g=7; func(::g, g); cout<<g<<“ – “<<::g<<endl; func(g, ::g); cout<<g<<“ – “<<::g<<endl; } OUTPUT 3 – 30 7 – 3 4 – 40 4 – 3

Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lifetime of a variable The time interval for which a particular variable or data value lives in the memory is called Lifetime of the variable or data value. Generally a variable’s life time is its parent-block-run i.e. as long as its parent block is executing, the variable lives in the memory. The global variable’s life time is the program-run. The life time of local variable having function scope is the function-run.

Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Thank You…