Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 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

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

4 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

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

6 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

7 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

8 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

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

10 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

11 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

12 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

13 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

14 #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

15 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

16 # 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

17 # 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

18 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

19 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

20 # 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

21 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

22 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

23 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

24 # 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

25 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

26 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

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

28 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

29 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 . 30


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

Similar presentations


Ads by Google