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? so continue reading What is Function? 

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

Graphics Program In C (click)

What is function
What is function

What is Function Explanation

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

Why use a Function

:- 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 functions:- 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 does Function work?

  • Once a function is defined and called, it takes some data from the calling function and returns a value to the called function.
  • The detail of the inner working of a function is unknown to the rest of the program whenever a function is called, control passes to the called function is completed control return back to the calling function’s and execute the next statement.
  • The value of the actual argument passes by the calling function is received by the formal argument of the called function. The number of actual and formal arguments should be the same.
  • The Function Operates on formal arguments and sends back the result to the calling function. The return ( ) statement performs this task.

Types of Function.

In C++ There are two types of Functions.

1.User defined Function.

2. System defined function.

1.User defined Function.

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

Example of Function in C and CPP

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

Ex. A. Write a program to Find the 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 the 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 the 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 functions are also called Library function. We use this function with HEADER_FILE. Basically, it has eight types of function’s is the following:-

  1. strlen():-These functions are used for the length of the String.
  2. strcpy():-This function is used for Copy of the String.
  3. strcat():-This function is used for Concatenate (connect with)of the String.
  4. strcmp():-This function is used for Compare of the String.
  5. strip():-This function is used for Compare of the String case wise.
  6. straw():-This function is used to Convert a String in the Lower case.
  7. strupr():-These functions are used to Convert a String in the Upper case.
  8. strrev():-This Function’s are use to Reverse the String.

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

ex.1:-write a program to find the length of a string using Function.
#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 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 strings are equal or not by Their Value using Function.
#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 strings are Equal or Not by their Case using functions.
#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

END

Leave a Reply

x