Pages

Wednesday, November 23, 2011

Void Function


A function must have a data type when declaring. The data type determines if a function returns a value or not. A function that is declared as void does not return a value and a function that is declared other than void returns a value.

Void Function

A void function is used if the function does not need to return a value after a computation or displaying some strings such as list, menus, options, etc.

Syntax:
void functionname()
{
//body of the function
}

Sample program 1:
           
#include<iostream>
using namespace std;
void myfunction()
{
cout<<"***************************"<<endl
    <<"* This is from a function *"<<endl
    <<"***************************"<<endl;
}

int main()
{
myfunction();

return 0;
}

Sample program 2:
           
#include<iostream>
using namespace std;
void myfunction()
{
cout<<"\t\t\t   JD and D Company"<<endl
    <<"\t\t\t  Salary Computation"<<endl
    <<"\t\t<E>mployee <M>anager <S>upervisor"<<endl;
}

int main()
{
char pos;
myfunction();
cout<<”Position -> “;
cin>>pos;

if(pos ==’e’ || ‘E’)
{
cout<<”You have selected a position of Employee”<<endl;
}
else if(pos ==’m’ || ‘M’)
{
cout<<”You have selected a position of Manager”<<endl;
}
else if(pos ==’s’ || ‘S’)
{
cout<<”You have selected a position of Supervisor”<<endl;
}

return 0;
}

No comments:

Post a Comment