Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Remember: Examination is a chance not ability. By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL.

Similar presentations


Presentation on theme: "1 Remember: Examination is a chance not ability. By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL."— Presentation transcript:

1 1 Remember: Examination is a chance not ability. By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL

2 Chapter No Chapter Name Page No 1. FUNCTIONS 162 2. OBJECTS AND CLASSES 215 3. ARRAYS AND STRINGS 263 4. OPERATOR OVERLOADING 319 5. INHERITANCE 371 6. POINTERS 429 2 Course Outline By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL

3 Chapter No: 01 3 By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL

4 4 What is Function A function is a self-contained block of statements that perform some kind of pre-defined task. Every C++ program can be thought of as a collection of these functions. Advantages – Writing functions avoids rewriting the same code over and over. Using functions – It becomes easier to write programs and keep track of what they are doing. – If the operation of a program can be divided into separate activities, and each activity placed in a different function – Separating the code into modular functions also makes the program easier to design and understand – Program are easier to debug – Program are well organized – Programmer can create his/her own customized library of functions – The program size is reduces By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL

5 5 Types of Functions There are basically two types of functions Library functions/pre-defined function As the name suggests, library functions are nothing but commonly required functions grouped together & stored in what is called library. These functions are also called as pre-defined functions Examples are: main( ), clrscr(), getch( ) etc User defined functions User defined functions are those functions which are written by the programmer for a special purpose or task & can be called in main program The user defined functions are written for formulas for which library functions are not available in the library of C++. By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL

6 Rules for declaring user-define functions The following are some rules keep in mind while declaring the functions: A. in a program two functions don’t have the same identical names. B. in a single program if two functions have the same identical names then their arguments should be different. C. A function name always starts with the same rules for variables name. D. No special characters or symbols other than underscore can be allowed in a function name. 6By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL

7 7 Syntax for defining function return typefunction-name (parameters) { ------------------ return (value); } void line(void) { int j; for (j=1;j<=50;j++) cout<<“*”; } 7 By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL

8 8 For User defined function To define user-defined function we need – Function declaration / Prototype – Calling the function. – Functions definition It can be explained in the following example 8By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL

9 9 void line (void); main() { line( ); cout<<“\n I LOVE AFGHANISTAN”; line( ); } void line(void) { int j; for (j=1;j<=50;j++) cout<<“*”; } ************************ I LOVE AFGHANISTAN ************************ function call function definition Function prototype Output

10 Function Declaration/prototype void line (void); main() { line( ); cout<<“\n MIHE”; line( ); } void line(void) { int j; for (j=1;j<=50;j++) cout<<“*”; } 10By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL

11 11 Calling the Function Calling the function means to call the function in the main program and to activate it. In our program function line( ) is called in the body of main ( ) program. Function Definition The function definition is function itself. It tells the compiler that a function is being defined, The actual steps of the function subprogram is written here. void line (void); main() { line( ); cout<<“\n RANA”; line( ); } void line(void) { int j; for (j=1;j<=50;j++) cout<<“*”; } 11 By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL

12 12 Calling function and called function main() { line( ); cout <<“\n I LOVE AFGHANISTAN”; line( ); } void line(void) { int j; for (j=1;j<=50;j++) cout <<“*”; } main() is calling function line() is called function

13 Write a program that print a message on the screen using function 13 By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL

14 Write a prog that show the use of parameters in the functions 14By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL

15 15 The Return Statement The return statement in C++, function serves two purposes i)On executing the return statement if immediately transfers the control back to the calling function ii)It returns the value present in the parenthesis after return, to the calling function The return statement need not be at the end of the function. It can occur anywhere in the function There is no restriction on the number of return statements that may be presented in a single function. A return statement can return only one value a time. 15By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL

16 Write a prog that show the use of returning value by a function when it is called 16By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL

17 17 Parameter passing to function A value passed to a function is called parameter. There are two methods for parameter passing to function parameter passing by value parameter passing by address By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL

18 18 The C++ language uses call by value to pass information from calling function to called function. This means that the supplied variable’s value is passed to the function, and not the variable itself. parameter passing by value Sum(int x, int y) { -------------- cout<<x+y; } a=8b=9 x=8Y=9 main() { int a=8,b=9; -------------- Sum(a, b); ------------- }

19 Create a c++ prog to pass two values from a function then compare these values and print result while no return value from the function 19 #include void comp(int x, int y) #include { void comp(int, int); if(x>y) void main(void) cout <<“first value is greater { than second value”<<endl; int n1, n2; else clrscr(); cout <<“the second value is cout<<“enter first value ”<<endl; greater than first value”<<endl; cin >> n1;} cout << “enter second value “<<endl; cin >> n2; comp(n1, n2); getch(); } #include void comp(int x, int y) #include { void comp(int, int); if(x>y) void main(void) cout <<“first value is greater { than second value”<<endl; int n1, n2; else clrscr(); cout <<“the second value is cout<<“enter first value ”<<endl; greater than first value”<<endl; cin >> n1;} cout << “enter second value “<<endl; cin >> n2; comp(n1, n2); getch(); } By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL

20 20 The C++ language uses call by address to pass addresses from calling function to called function. This means that the supplied variable’s address is passed to the function. parameter passing by address Sum(int* x, int* y) { -------------- cout <<x <<y; } Sum(int* x, int* y) { -------------- cout <<x <<y; } Ram a=8b=9 x=100Y=102 main() { int a=8,b=9; -------------- Sum(&a, &b); ------------- } main() { int a=8,b=9; -------------- Sum(&a, &b); ------------- } 100 102

21 Create a c++ prog in which call a function by passing address, the variables will pass with storage address locations 21 By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL

22 Create a prog to exchange the values of two variables by using reference arguments in the function. 22 #include void exch(int& x, int& y) #include { void exch(int &, int &) int t; void main(void) t = x; { x = y; int a, b; y = t; clrscr(); } cout<<“enter first value ”<<endl; cin >> a; cout << “enter second value “<<endl; cin >> b; cout << “first value is= “<<a <<“\n second value =“<<b; exch(a, b); cout <<first value is =“<<a<<“\n second value is =“<<b; } By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL

23 23 Pre-processor directives The pre-processor directive is the instruction begin with # symbol most often placed at the beginning of the program and process before even compilation process begin, we would learn the following pre-processor directives here #define(macro expansion) By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL

24 24 #define(macro expansion) This statement is called macro definition or macro. during preprocessing, the preprocessor replaces every occurrence of Pi in the program with 3.14 Syntax: #define Marco-Name value In this example Pi is macro, whereas, 3.14 is called their macro expansion. macro can be used instead of function #define Pi 3.14 main() { -------------- ------------Pi Pi----------- -------------- ----Pi-------- -------------- ------------ -------Pi*Pi ------------- ---Pi--------- ------------- } #define Pi 3.14 main() { -------------- ------------Pi Pi----------- -------------- ----Pi-------- -------------- ------------ -------Pi*Pi ------------- ---Pi--------- ------------- } By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL

25 25 macro instead of function #define cls clrscr(); #define w cout<<“***; #define p cout<<“---; main() { cls p cout<<“\n MIHE\n”; p w } #define cls clrscr(); #define w cout<<“***; #define p cout<<“---; main() { cls p cout<<“\n MIHE\n”; p w } main() { clrscr(); cout<< “-----; cout<<“\n MIHE\n”; cout<< “----; cout<<“***; } main() { clrscr(); cout<< “-----; cout<<“\n MIHE\n”; cout<< “----; cout<<“***; } After compilation Object Code contains the Macro expansion By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL

26 Write a c++ prog that show the use of a macro in the programming by using #define 26 #include #define R x+((y*z) /2) void main(void) { int x, y, z, result; x=10; y= 5; z= 2; clrscr(); result = R; cout <<“result = “<<result <<endl; result = R + 10; cout<<“increment in result = ”<< result<<endl; result = R – 15; cout<<“decrement in the result = “<<result<<endl; cout<<“end of program”<<endl; getch(); } By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL

27 Write a c++ prog that show a largest value in the two given values by using #define or macro 27 #include #define LARGE (x, y) ((x>y)? x: y) void main(void) { int x, y; clrscr(); cout <<“enter value for x “<<endl; cin >> x; cout<<“enter value for y ”<<endl; cin >> y; cout<<“the Largest value = “<<LARGE (x, y)<<endl; cout<<“end of program”<<endl; getch(); } #include #define LARGE (x, y) ((x>y)? x: y) void main(void) { int x, y; clrscr(); cout <<“enter value for x “<<endl; cin >> x; cout<<“enter value for y ”<<endl; cin >> y; cout<<“the Largest value = “<<LARGE (x, y)<<endl; cout<<“end of program”<<endl; getch(); }


Download ppt "1 Remember: Examination is a chance not ability. By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL."

Similar presentations


Ads by Google