4 Type conversion functions

672 views

Published on

Type conversion functions

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
672
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
32
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

4 Type conversion functions

  1. 1. 1. Converting by assignment operator 2. Using cast operator Syntax: (cast_type) expression; or cast_type (expression); 09/04/131 VIT - SCSE Type Conversion #include<iostream.h> #include<conio.h> void main() { char ch='A'; int x; x=(int)ch; cout<<x; getch(); }
  2. 2. Enum Types The enum keyword automatically enumerates a list of words by assigning them values 0,1,2,3,4, and so on. The general form of enum is: enum variable_name{ list of constants separated by commas }; e.g: enum day{sun,mon,tue,wed,thu,fri,sat}; 09/04/132 VIT - SCSE
  3. 3. # include<iostream.h> enum day{sun,mon,tue,wed,thu,fri,sat}; void main( ) { day d1, d2,d3; d1 = mon; d2 = fri; int diff = d2 – d1; cout<<"days between = "<<diff<<endl; if(d1<d2) cout<< "day1 comes before day2n"; } 09/04/133 VIT - SCSE
  4. 4. Functions A function groups a number of program statements into a unit and gives it a name. This unit can then be invoked from other parts of the program. It is used to reduce program size. The main advantages of using a function are: 1. Easy to write a correct small function 2. Easy to read, write and debug a function 3. Easier to maintain or modify such a function 4. It can be called any number of times in any place with different parameters 09/04/134 VIT - SCSE
  5. 5. 09/04/135 VIT - SCSE The Function Declaration The Function Definition Function Calling
  6. 6. Return statement The keyword return is used to terminate function and return a value to its caller. The return statement may also be used to exit a function without returning a value. The general form of the return statement is.  return;  return ( expression); 09/04/136 VIT - SCSE
  7. 7. //using multiple return statements in a function # include<iostream.h> void main() { float maximum( float, float, float); float x,y,z,max; cout<<"Enter three numbers"; cin>>x>>y>>z; max = maximum(x,y,z); cout<<"maximum"<<max; } float maximum(float a,float b,float c) { 09/04/137 VIT - SCSE if(a>b) { if(a>c) return (a); else return (c); } else { if(b>c) return (b); else return (c); } }
  8. 8. Types of functions A function is invoked without passing any formal argument does not return any value to the calling portion. A function is invoked with formal argument and does not return any value to the calling portion. A function is invoked with formal argument and returns back a value to the calling environment. 09/04/138 VIT - SCSE
  9. 9. Function Arguments The arguments can be classified into two groups: 1. Actual argument 2. Formal argument Local and Global variable 09/04/139 VIT - SCSE
  10. 10. A function which calls itself directly or indirectly again and again is known as the recursive function. 09/04/1310 VIT - SCSE Recursive function int sum(int n) { int value=0; if(n= = 0) return(value); else value = n+sum(n-1); return(value); } # include<iostream.h> void main() { int sum(int); int n,temp; cout<<"Enter any integer number"<<endl; cin>>n; temp = sum(n); cout<<"value = "<<n<<"and its sum ="<< temp; }
  11. 11. E.x: void sum(int a,int b,int c=6,int d=10); 09/04/1311 VIT - SCSE Default arguments void sum(int a1,int a2,int a3,int a4) { int temp; temp = a1+a2+a3+a4; cout<<"sum="<<temp; } // default argument declaration # include<iostream.h> void sum(int a,int b,int c=6,int d=10); //default argument initialization void main() { int a,b,c,d; cout<<"enter any two numbers"<<endl; cin>>a>>b; sum(a,b); //sum of default values }
  12. 12. 09/04/1312 VIT - SCSE Function Overloading cout<<add(5,10); cout<<add(15,10.0); cout<<add(12.5,7.5); cout<<add(5.10,15); cout<<add(0.75,5); int add(int a,int b); int add(int a,int b,int c); double add(double x,double y); double add(int p,double q); double add(double p,int q);
  13. 13. 09/04/1313 VIT - SCSE Function Overloading double volume(double r,int h) { return(3.14519*r*r*h); } long volume(long l,int b,int h) { return (1*b*h); } #include<iostream.h> int volume(int); double volume(double,int); long volume(long,int,int); main() { cout<<volume(10)<< endl; cout<<volume(2.5,8)<<endl; cout<<volume(100L,75,15); getch(); return 0; } int volume(int s) { return (s*s*s); }

×