A function is a self contain set of statements that perform a specific task.With the help of function larage program is divided into small program called function. main() is an example of user-defined function and strev(),sin() etc is an example of library function.
Advantages of function
- With the help of function larage program is divided into small program is called function.
- Remove error more easily.
- Reduce the repetition of code.
- Reduce the complexity of a program.
Types of function
C++ functions can be classified into two categories, namely library function and user-defined functions.main() is an example of user-defined function and sin(),cos() etc. is an example of library function.- Library Function
- User-defined Function
- Library function :- There are certain set of general purpose operations which are quite frequently used by many programmers in their programs.For example- To calculate the square root of a number,to calculate power of number and many more.
The library functions are predefined functions which are designed to perform some specific tasks.Before using library function in the program, it is necessary to include header files.The header file contain the information about library functions. The library functions that perform mathematical operations.Ex:- sin(),cos() etc.They are include math.h header file.The library functions that perform string operations.Ex:-strev,strcmp() etc.They are include String.h header file. - User-defined functions:-These functions which are defined by the user to meet his requirements are called user-defined functions. Every C++ program contain atleast one function.The function which is present in every C++ program is main( ) which is user-defined function.The library and user-defined functions are normally used in the main() function.
a) Function Definition
b) Function declaration
c) Function call
Function Definition
Function definition contain the actual code of a function.The general syntax of function definition is
ret_type func_name(data_type par1,data_type par2,..........)
{
local variable declaration;
statement1;
........................
........................
return(expression);
}
The first line of function definition is called the function declarator that consists of ret_type which specific the data type of the value to be returned,function_name which specifies the name of the function and set of parameters and their data type separated by commas enclosed in pair of parentheses
Function declaration
While using library functions in the program, it is necessary to include the header file which contains the declaration of related library function.Similarly in C++,it is unavoidable to use a user-defined function without its declaration.Infact ,before a function can be called,it must be declared in the function that will do the calling. A function declaration specific the name of the function,number and type of the parameters and its return type followed by a semicolon.
The syntax of function declaration is as follows :
ret_type func_name(data_type par1,data_type par2,..........);
Here,the ret_type specifies the data type of the value returned by the function.If the function does not return any value.The func_name is any valid identifier which is followed by list of parameter(s) preceded by their data types separated by commas and enclosed in parentheses.
Function call
A function which is declared and defined needs to be used somewhere in the program so that it can perform its intended task for which it is designed.In order to use it in the program,we need to call it.A function can be called by specifying the function name followed by a list of arguments separated by coms enclosed in the pair of parentheses.
Program of using function addition of two number
#include<iostream.h>
#include<conio.h>
main()
{
int num1=10,num2=20;
clrscr();
void add(int,int); //Function declaration
#include<conio.h>
main()
{
int num1=10,num2=20;
clrscr();
void add(int,int); //Function declaration
add(num1,num2); //Function call
getch();
}
void add(int x,int y) //Function definition
{
int sum=x+y;
getch();
}
void add(int x,int y) //Function definition
{
int sum=x+y;
cout<<"Sum="<<sum;
}
}
Output of this program
Sum=30
Pass argument in three different ways
#include<iostream.h>
#include<conio.h>
main()
{
int num1=10,num2=20;
clrscr();
void swap(int,int);
cout<<"num1="<<num1<<endl<<"num2="<<num2<<endl;
swap(num1,num2);
getch();
}
void swap(int x,int y)
{
int temp=x;
x=y;
y=temp;
cout<<"After swaping value is x="<<x<<" and "<<"y="<<y;
}
Output of this program
num1=10
num2=20
After swaping value is x=20 and y=10
Argument Passing Technique
Argument passing is a process of transfer data from calling function to called function.Pass argument in three different ways
- Pass by value
- Pass by address or pass by pointer
- Pass by refference
- Pass by value :- Pass by value is a technique used for argument passing.In this technique copy of actual argument is made and pass to the formal argument.Any change made in the formal argument are not refer back to the actual argument.
#include<iostream.h>
#include<conio.h>
main()
{
int num1=10,num2=20;
clrscr();
void swap(int,int);
cout<<"num1="<<num1<<endl<<"num2="<<num2<<endl;
swap(num1,num2);
getch();
}
void swap(int x,int y)
{
int temp=x;
x=y;
y=temp;
cout<<"After swaping value is x="<<x<<" and "<<"y="<<y;
}
Output of this program
num1=10
num2=20
After swaping value is x=20 and y=10
- Pass by address and pass by pointer :- Pass by address is also another technique used for argument passing.In this technique as compare to pass value.We pass the address of actual argument to the formal argument that act like a pointer variable.In this any change made in formal argument are refer back to actual argument. Program using Pass by address technique swap two number #include<iostream.h>#include<conio.h>
main()
{
int num1=10,num2=20;
clrscr();
void swap(int *,int *);
cout<<"Num1="<<num1<<endl<<"Num2="<<num2<<endl;
swap(&num1,&num2);
cout<<"After swaping num1="<<num1<<" and "<<"num2="<<num2;
getch();
}
void swap(int *x,int *y)
{
int temp=*x;
*x=*y;
*y=temp;
cout<<"After swaping x="<<*x<<" and "<<"y="<<*y<<endl;
} Output of this program Num1=10 Num2=20 After swaping x=20 and y=10 After swaping num1=20 and num2 10
- Pass by reference :-The pass by reference is a very easy and simple technique of argument passing which is only available in C++. Using this technique,any modifications in the formal arguments(s) in the called function are reflected back to the actual argument(s) in the calling function.This method of argument passing which allows the modifications back to the actual arguments(s) is similar to the pass by address technique of argument passing but without using the complex syntax of pointers.Before discussing the concept of argument passing by reference.
#include<iostream.h>
#include<conio.h>
main()
{
int num1=10,num2=20;
clrscr();
cout<<"num1="<<num1<<endl<<"num2="<<num2<<endl;
void swap(int &,int &);
swap(num1,num2);
cout<<"After swaping num1="<<num1<<" and "<<"num2="<<num2;
getch();
}
void swap(int &x,int &y)
{
int temp=x;
x=y;
y=temp;
cout<<"After swaping x="<<x<<" and "<<"y="<<y<<endl;
}
Output of this program
num1=10#include<conio.h>
main()
{
int num1=10,num2=20;
clrscr();
cout<<"num1="<<num1<<endl<<"num2="<<num2<<endl;
void swap(int &,int &);
swap(num1,num2);
cout<<"After swaping num1="<<num1<<" and "<<"num2="<<num2;
getch();
}
void swap(int &x,int &y)
{
int temp=x;
x=y;
y=temp;
cout<<"After swaping x="<<x<<" and "<<"y="<<y<<endl;
}
Output of this program
num2=20
After swaping x=20 and y=10
After swaping num1=20 and num2=10
Comments
Post a Comment