Menu

What Is Function


What Is Function

Hello Dear we learn here about  What is Function. With their Types helps of Programming. And also learn How it’s work in, Any programming languages. And what is the main Purpose of using Function’s. so continue read What is Function ? 

Function:- It is a group of statement which perform some action. A function’s is a Self- contained BLOCK or a Sub-program of one or more statement that perform a special task When Called.What is Function.

What is Function

 

What is Function’s

#syntax:-
return_type function_name(argument/parameter list)
{
\Body of the Function
local variable declaration;
statement 1;
statement 2;
return ( value );
}

Why use Function’s?
:- If we want to perform a task repetitively, then it is not necessary to Re-write the particular block of the program again and again. The particular block of statements
in a User defined function. The function’s defined can be called any number of times to perform the task.

Using function’s:- large programs can be reduced to smaller one’s. It is easy to debug and find out the errors in it. It also increases readability.

How a Function’s Works?
:- Once a function’s is defined and called, it takes some data from the calling function and return a value to the called function.

:- The detail of inner working of a function’s is unknown to the rest of the program when ever a function is called, control passes to the called function is completed control return back to the calling function’s and executes the next statement.

:- The value of actual argument passes by the calling function are received by the formal argument of the called function. The number of actual and formal arguments should be the same.

:-The Function’s Operates on formal arguments and sends back the result to calling function. The return ( ) statement performs this task.

Types of Function’s.

:-In C++ There are two types of Function’s.

1.User defined Function.

2. System defined function.

1.User defined Function.

:- There are three types of user defined function’s.
   A. No Argument No Return Value.
   B. With Argument No Return Value.
   C. With Argument With Return Value.

Example with their  Program:-

function’s Program in C++ ( C Plus Plus)

Ex. A. Write a program to Find sum of two number using No Argument No Return Value:-

#include
#include
#include
void sum();
void main()
{
sum();
}
void sum()
{
int a,b,s;
a=b=s=0;
cout<<“ENTER TWO NUMBER:-“; cin>>a>>b;
s=a+b;
cout<<“n SUM:-“<<s;
getch();
}

INPUT:- 8 “again” 5

OUTPUT:- 13

Ex. B. Write a program to Find sum of two number using with Argument No Return Value:-
#include
#include
#include
void add(int a, int b);
void main()
{
int a,b;
a=b=0;
cout<<“ENTER TWO NUMBER:-“; cin>>a>>b;
add(a,b);
{
void add (int a, int b) ;
}
int r=0;
r=a+b;
cout<<“n SUM:-“<<r;
getch();
}

INPUT:- 7 “again” 5

OUTPUT:- 12

Ex. C. Write a program to Find sum of two number using with Argument with Return Value:-

#include
#include
#include

int sum(int a, int b);
void main()
{
int a,b,r=0;
a=b=0;
cout<<“ENTER TWO NUMBER:-“; cin>>a>>b;
r=sum(a,b);
cout<<“sum:-“<<r;
{
int sum(int a, int b) ;
}

r=a+b;

getch();
}

INPUT:- 4 “again” 5

OUTPUT:- 9

 

:-There are Three component of user defined function’s.
A. Function’s Prototype
B. Calling a Function’s
C. Definition of a Function’s

2. System defined function’s.

:-System defined function’s is also called Library function. We use this function’s with HEADER_FILE . Basically it has eight type of function’s is following:-

(i)strlen():-This function’s are use for Length of the String.

(ii)strcpy():-This function is use for Copy of the String.

(iii)strcat():-This function is use for Concatenate (connect with)of the String.

(iv)strcmp():-This function is use for Compare of the String.

(v)stricmp():-This function is use for Compare of the String case wise.

(vi)strlwr():-This function is use for Convert a String in Lower case.

(vii)strupr():-This function’s are use for Convert a String in Upper case.

(viii)strrev():-This Function’s are use for Reverse the String.

(example of every system defined function’s are here)

ex.1:-write a program to find Length of a string using Function’s.
#include
#include
#include
void main()
{
char str[20];
int l=0;
cout<<“ENTER the STRING:-“;
cin.getline(str, 20) ;
l=strlen(str);
cout<<“n LENGTH:-“<<l;
getch();
}

INPUT:-HOME

OUTPUT:- 4

“This function only count how many character you entered.”

ex.2:-write a program to copy of a string in another string using Function’s.
#include
#include
#include
void main()
{
char str[20],rel[20];
cout<<“ENTER the STRING:-“;
cin.getline(str, 20) ;
strcpy(rel,str);
cout<<“n RESULT:-“<<rel;
getch();
}

 INPUT:- Saurabh Nissa

OUTPUT:- Saurabh Nissa

“Here this function only COPY string in one string to another.”

ex.3:-write a program to Concatenate of a string using Function’s.
#include
#include
#include

void main()
{
char str1[20],str2[20];
cout<<“ENTER the STRING:-“;
cin.getline(str1, 20) ;
cin.getline(str2, 20) ;
strcat(str1,str2);
cout<<“n RESULT:-“<<str1;
getch();

}

Input:- abcd “again” ABCD

Output:-abcdABCD

“Concatenate means adding or merging”

 

 

ex.4:-write a program to check two string are equal or not by Their Value using Function’s.
#include
#include
#include

void main()
{
char str1[20],str2[20];
cout<<“ENTER the STRING:-“;
cin.getline(str1, 20) ;
cin.getline(str2, 20) ;
if(strcmp(str1,str2)==0)
{
cout<<“n Both are EQUAL”;
}
else
{
cout<<“n Both are NOT EQUAL”;
}
getch();

}

INPUT:- Abc  “again” ABC

OUTPUT:-Both are NOT EQUAL

“Here check equal or not by their ASCII value”

ex.5:- write a program to check two string are Equal or Not by their Case using function’s.
#include
#include
#include

void main()
{
char str1[20],str2[20];
cout<<“ENTER the STRING:-“;
cin.getline(str1, 20) ;
cin.getline(str2, 20) ;
if(stricmp(str1,str2)==0)
{
cout<<“n Both are EQUAL”;
}
else
{
cout<<“n Both are NOT EQUAL”;
}
getch();

}

1st run

INPUT:- Abc  “again” ABC

OUTPUT:-Both are NOT EQUAL

2nd run

INPUT:- ABC  “again” ABC

OUTPUT:-Both are  EQUAL

ex.6:- write a program to convert a string in Lower case using function’s.
#include
#include
#include
void main()
{
char str[20];
cout<<“ENTER the STRING:-“;
cin.getline(str, 20) ;
cout<<“n LOWER:-“<<strlwr(str);
getch();
}

INPUT:- AbcD

OUTPUT:-abcd

ex.7:- write a program to convert a string in UPPER case using function’s.
#include
#include
#include
void main()
{
char str[20];
cout<<“ENTER AN STRING IN ANY CASE:-“;
cin.getline(str, 20) ;
cout<<“n UPPER:-“<<strUPR(str);
getch();
}

INPUT:- Abc

OUTPUT:-ABC

ex.8:- write a Program to REVERSE of STRING IN C++ using function’s.
#include
#include
#include
void main()
{
char str[20];
cout<<“ENTER the STRING:-“;
cin.getline(str, 20) ;
cout<<“n REVERSE IS:-“<<strrev(str);
getch();
}

INPUT:- MaNgo

OUTPUT:-ogNaM



What Is Function - The Smolt

What is function, IT's a group of statement which perform some action...Learn more about function, how it work and,Their types with Example...

Course Provider: Organization


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Learn For Free.
T H E S M O L T Learn For Free Download Our Android App in Free DOWNLOAD
+ +