Types of Function in c++

Types of Function in c++:

1) Library Functions

2) User defined functions

1) Library Functions:

Library functions are the in-built function in C++ programming system.
Examples: – main, cout, cin

2) User defined functions:

C++ allows the user to define their own function according to their requirement. These types of functions are called user-defined functions

#include <iostream.h>
void function_name()// function Declaration and definition
{
…………….
}
void main()
{
………..
function_name();//Calling
………..
………..
}
Or

#include <iostream.h>
#include<conio.h>
void function_name()//Declaration
void main()
{
………..
function_name();//Calling
………..
………..
getch();
}
void function_name()//definition
{
…………….
}

Examples of user defined functions:

Write a program to find sum using function

#include<iostream.h>

#include<conio.h>

int sum(int,int);

void main()

{

int a,x,y;

clrscr();

cout<<“enter the number”;

cin>>x>>y;

sum (x,y);//calling

getch();

}

int sum(int p1, int q1)//definition

{

int c; c=p1+q1;

cout<<“sum is “<<c;

}

Output
enter the number 67

8

sum is 75

Write a program to find greater of two number using Function
#include<iostream.h>

#include<conio.h>

int greater(int,int);

void main()

{

int x,y;

cout<<“enter the value of x and y”;

cin>>x>>y;

greater(x,y);

getch();

}

int greater(int x, int y)

{

if(x>y)

{

cout<<“x is greater”;

}

else

{

cout<<“y is greater no.”;

}

return 0;

}

Output

enter the value of x and y 34

78

y is greater no.

Types of User defined Functions:

1] Function with no arguments and no return value.
2] Function with no arguments and return value.
3] Function having arguments but no return value.
4] Function with arguments and return value.

 

Write a program for a prime number

#include<iostream.h>

#include<conio.h>

void main()

{

int n1,i=3,j,d;

cout<<“enter the number \n”;

cin>>n1;

if(n1>=1)

{

cout<<“the first prime no = \n2 “<<n1;

cout<<“\n1”;

}

for (j=2;j<=n1;)

{

for(d=2;d<=i-1;d++)

{

if(i==0)

break;

}

if(d==i)

{

cout<<“\n”<<i; j++;

}

i++;

}

getch();

}

Output:
enter the number 2

the first prime no =2 2 3

 

 

Example: Function with no arguments and doesn’t return value.

#include<iostream.h>

#include<conio.h>

void sum1()

{

int a = 5;

a = a+10;

cout<<a;

}

void main()

{

sum1();

getch();

}

Output
15
Example:Function with no arguments and return value
#include<iostream.h>

#include<conio.h>

int sum()

{

int a = 5;

a = a+10;

cout<<a; return a;

}

void main()

{

sum();

getch();

}

Output
15
Example:Function with arguments but no return value.
#include<iostream.h>

#include<conio.h>

void sum(int a)

{

a = a + 10;

cout<<a;

}

void main()

{

sum(6);

getch();

}

Output
16
Example:Function with arguments and return value.
#include<iostream.h>

#include<conio.h>

int sum(int a)

{

a = a + 10;

cout<<a;

return a;

}

void main()

{

sum(6);

getch();

}

Output
16
WAP to print sum of n number enter by user using array in c++
#include<iostream.h>

#include<conio.h>

void main()

{

int a1[4];

int i,n1,sum1=0;

clrscr();

cout<<“Enter the no. of Classes “;

cin>>n1;

for(i=0;i<n1;i++)

{

cout<<“\n Enter the number student in Class = “<<i+1;

cin>>a1[i];

}

for(i=0;i<n1;i++)

{

sum1= sum1+a1[i];

}

cout<<“\n Total Number of Student = “<<sum1;

getch();

}

Output:
Enter the no. of Classes 2

Enter the number student in Class =1 50

Enter the number student in Class =2 60

Total Number of Student = 110

 

Leave a Reply

Your email address will not be published. Required fields are marked *