4 type conversion functions
DESCRIPTION
Type conversion functionsTRANSCRIPT
1. Converting by assignment operator
2. Using cast operatorSyntax:(cast_type) expression;orcast_type (expression);
04/12/231 VIT - SCSE
Type Conversion
#include<iostream.h>#include<conio.h>void main(){char ch='A';int x;x=(int)ch;cout<<x;getch();}
Enum TypesThe 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};
04/12/232 VIT - SCSE
# 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 day2\n";
}
04/12/233 VIT - SCSE
FunctionsA 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 function2. Easy to read, write and debug a function3. Easier to maintain or modify such a function4. It can be called any number of times in any place with
different parameters
04/12/234 VIT - SCSE
04/12/235 VIT - SCSE
The Function DeclarationThe Function DefinitionFunction Calling
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);
04/12/236 VIT - SCSE
//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)
{
04/12/237 VIT - SCSE
if(a>b){
if(a>c)return
(a);elsereturn
(c);}else{
if(b>c)return
(b);elsereturn
(c);}
}
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.
04/12/238 VIT - SCSE
Function Arguments
The arguments can be classified into two groups:
1. Actual argument2. Formal argument
Local and Global variable
04/12/239 VIT - SCSE
A function which calls itself directly or indirectly again and again is known as the recursive function.
04/12/2310 VIT - SCSE
Recursive function
int sum(int n){int value=0;if(n= = 0)return(value);elsevalue = 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;}
E.x: void sum(int a,int b,int c=6,int d=10);
04/12/2311 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}
04/12/2312 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);
04/12/2313 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);}