Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 06 FUNCTIONS 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1.

Slides:



Advertisements
Similar presentations
Spring Semester 2013 Lecture 5
Advertisements

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions.
Functions in C++. Functions  Groups a number of program statements into a unit & gives it a name.  Is a complete and independent program.  Divides.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
An Introduction to Programming with C++ Fifth Edition
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 6: User-Defined Functions I
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
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.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Course Title Object Oriented Programming with C++ Course instructor ADEEL ANJUM Chapter No: 05 ARRAY 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1.
Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 1 BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
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.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
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.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Learners Support Publications Functions in C++
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
1 Remember: Examination is a chance not ability. By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL.
UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)
GE 211 Dr. Ahmed Telba. // compound assignment operators #include using namespace std; int main () { a =5 int a, b=3; a = b; a+=2; // equivalent to a=a+2.
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.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 07 classes 1 BY ADEEL ANJUM ( MSc - cs, CCNA, WEB DEVELOPER )
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.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
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.
Chapter 3: User-Defined Functions I
Classes, Interfaces and Packages
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Course Title Object Oriented Programming with C++ Course instructor ADEEL ANJUM Chapter No: 04 loops 1 BY ADEEL ANJUM ( MSc - cs, CCNA, WEB DEVELOPER )
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
Programming Fundamentals Enumerations and Functions.
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
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.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Object-Oriented Programming in C++ Exception.
Functions + Overloading + Scope
Chapter 9: Value-Returning Functions
User-Written Functions
Chapter 6: User-Defined Functions I
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
6 Chapter Functions.
Chapter 6: User-Defined Functions I
Chapter 9: Value-Returning Functions
Based on slides created by Bjarne Stroustrup & Tony Gaddis
TOPIC: FUNCTION OVERLOADING
CPS125.
Presentation transcript:

Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 06 FUNCTIONS 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1

FUNCTIONS A function is set of instruction that are designed to perform specific task. The functions are written to write the code of a large program by dividing into small units. The functions are also written to avoid replication of a code in the program. Code is written once as a function and function is called to execute its code. 2

TYPES OF FUNCTIONS Functions are of two type : 1.built in functions 2.user defined functions 3

Conti… BUILT IN FUNCTIONS The functions that have already defined as a part of the language and can be used in any program are called built in functions. USER DEFINED FUNCTIONS The functions created by user are called user defined functions. These functions are written for a specific use. 4

PART OF USER DEFINED FUNCTION Function declaration (prototype) Function definition Function calling 5

FUNCTION DECLARATION (PROTOTYPE) The function declaration is also called prototype. Prototype means sample or model. In function declaration the information about the function is provided to the compiler: The name of function The type of data returned by the function The number of parameters used in the function 6

Conti… semicolon is used at the end of function declaration to indicates the end of function declaration. The function declaration is similar to a variable declaration. The rules for naming function are same as those for naming variables Syntax: TYPE FUNCTION NAME (ARGUMENT); example: void display(void); 7

FUNCTION DEFINITION The actual code of the function is called function definition. It is the set of instruction that are written to perform a specific task. Function definition is always done outside the main () function. It can be written before or after main() function. 8

Conti….. The function definition contains two parts. these are : 1. declarator 2. body of function 9

DECLARATOR It is the heading of the function definition. The heading line of function is same as the function declaration. but it is not terminated by the semicolon. general format: type function name (argument) declarator { set of statementsbody of function } 10

BODY OF FUNCTION The set of a statement enclosed in curly braces after the declarator are called the body of the function. type function name (argument) declarator { set of statementsbody of function } 11

CALLING FUNCTION Executing the statement of a function to perform a task is called calling of function. A function is called by referencing its name. When a function is called the control shifts to the function definition and the statement in the body of the function are executed. After executing the statements in the body of function, the control returns to the calling function and the next statement after the function call is executed. 12

Program to print message using functions #include void main (void) { void display (void); // function declaration. clrscr(); display(); // function calling cout<<“ok”; getch(); } void display (void) // function definition { cout<<“its my first function”<<endl; } 13

#include void main (void) { void sum (int, int); clrscr(); sum(10,5); cout<<“ok”; getch(); } void sum(int x,int y) { int s; s=x + y; return(s); } 14

Passing arguments to functions If a function needs data to perform a specific task, this data is provided through the arguments of the function. The arguments are placed in parentheses. The arguments are either constants or variables. They are written in the same sequence in which they are defined in the function declaration. The data type of an argument in the function call must also match the corresponding data type in the function declaration. 15

# include void main () { void sum(int, int); clrscr(); sum(10,15); cout<<“ok”; getch(); } void sum(int x, int y ) { int s; s= x + y; cout<<“sum=”<<s<<endl; } 16

# include void main () { clrscr(); void cal (int, char, int); //function declaration int n1,n2; char op; cout<<“Enter 1 st value,operator, 2 nd value “<<endl; cin>>n1>>op>>n2; cal (n1,op, n2); //function call getch(); } void cal (int x, char op, int y) // function definition { switch(op) { case’+’: cout<<x+y; break; case’*’: cout<<x*y; break; case’-’: cout<<x-y; break ; default: cout<<“invalid input”; } 17

RETURNING DATA FROM FUNCTION A function can return only one value. It can be of any type except string and array. The type of data that a user defined function return is declared in function declaration. The value or data is returned from the function to the calling function by the “return” statement. 18

RETURN STATEMENT The “return” statement is used to return the calculated value from function definition to the calling function. its syntax is: return expression; If the function has a “void “ return data type, then the use of this statement is optional. 19

# include void main () { clrscr(); int kg,gm; int grams(int); cout<<“Enter value in kilogram ?”; cin>>kg; gm=grams(kg); cout<<“Answer in grams =“<<gm; } int grams(int val) { return val*1000; } 20

FUNCTIONS OVERLOADING Declaring more than one function with same but with different set of argument and return type is called functions overloading. for example, three functions with same name “sum” and having different parameters are declared as: int sum(int, int); int sum(int,int, int); double sum(double, double,double); 21

Conti…. The first function “sum” has two parameters both of int type and returned type is also int type. The second function “sum ” has three parameters, all of int type. The return data type is also of type int. The third function “sum” has three parameters,all of double data type and return type is also double. 22

Conti….. when the program that has overloaded functions is compiled, the c ++ compiler checks the number of parameters,their order and data type and marks a proper function name for each function. At function call, it executes that functions whose return type or parameters matched with the return type or parameters given in function call. The following program example explains the concepts of functions overloading. 23

# include main() { int sum(int,int); // function declaration. int sum(int,int,int); double sum (double, double ); clrscr(); cout<<“sum ”<<sum(2,5); cout<<“sum”<<sum(2,5,7); cout<<“sum”<<sum(2.3,3.3); cout<<“ok”; } // main closed // function definition: int sum(int x, int y) { return x+y ; } int sum(int x, int y, int z) { return x + y+ z; } double sum ( double x,double y) { return x+y; } 24

Conti… In the above program, three functions are defined with the same name “sum”. When the function “sum ” is called by passing 2 integer values as parameters, the control will shifts to the 1 st function that has two integer type arguments. Similarly, when the function is called by passing float data type value,control shifts to 3 rd function that matches the parameters. 25

FUNCTION TEMPLATES In functions overloading,more than one functions with same name are declared and defined. This makes program more complex and lengthy. To overcome this problem, function template is used. In function template, a single typeless function is defined such that arguments of any standard data type can be passed to the function. 26

Conti… The general syntax of a function template is: template T f-name(T argument(s)) { statements(s); } 27

template: The keyword template is used to define the function template. class: In a function template a general data name is defined by using the keyword “class” in the above syntax it is represented by “T”. any character or word can be used after the keyword class to specify the general data name. f-name: it specify the function name. arguments: These are the arguments that are to be passed to the function. The arguments are declared of type T, as the type T specify any standard data type. statement(s): it specifies the actual statement of the function. 28

Program to calculate sum of integer and floating value using function templates. # include template T sum (T a, T b, T c) { return a + b + c; } main() { clrscr(); cout<<“sum of floating values ”<<sum(2.5,5.5,6.5); cout<<“sum of integers values ”<<sum(2,5,7); cout<<“ok”; } 29

. 30