Functions:Function is a set of module or we can say a subprogram that acts on a data and often return a value.The basic purpose of a function in c++ is to perform some specific task based upon the instruction given in it..
Functions are basically of two types:
1.Built in Functions:These are those functions which are already define within the complier and available in library. For example sqrt(),pow() etc.
2.User defined functions:These are those functions which are defined by the user and work as per the requirement of the user.
Function include three things which are very important to work with functions these are :
1.Function Definition: This is used to define the function with its name and its basic characteristics or we can say requirement.
Syntax: type function name(parameter list)
{
Body of function
}
Example:int sum(int a,int b)
{
int c;
c=a+b;
}
2.Function Prototype: A function prototype is declaration of function that tells the program the type of value which will be returned by the function during the program run and number of arguments available in function.
Syntax : return type name of function(argument list);
Example: int sum(int a,int b);
3.Function call or access :The function calling is the most important step that is required in c++ as the function call will enable the user to call and use the function in run time in main and perform all activities.
Syntax:function name(parameters);
Example add(a,b);
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
float cube(float); //Function prototype
float x,y;
cout<<"Enter a number to find cube";
cin>>x;
y=cube(x); // Function Call
cout<<"The cube of"<<x<<"is "<<y;
getch();
return 0;
}
float cube(float a) //Function Definition
{
float n;
n=a*a*a;
return(n);
} #include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
float cube(float);
float x,y;
cout<<"Enter a number to find cube";
cin>>x;
y=cube(x);
cout<<"The cube of"<<x<<"is "<<y;
getch();
return 0;
}
float cube(float a)
{
float n;
n=a*a*a;
return(n);
}
Parameters can be default,constant and no parameter
Call by value : The call by value method the value method copies the values of actual parameters into the formal parameters and create own copy of arguments and then uses them.
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
void swap(int,int);
int a,b;
a=7;
b=4;
cout<<"Original value";
cout<<"a="<<a<<"b="<<b<<"\n";
swap(a,b);
cout<<"The value after swap()are"<<"\n";
cout<<"a="<<a<<"b="<<b<<"\n";
getch();
return 0;
}
void swap(int x,int y)
{
int temp;
temp=x;
x=y;
y=temp;
cout<<"Swapped values are"<<"\n";
cout<<"a="<<x<<"b="<<y<<"\n";
}
Call by reference:In call by reference method the instead of passing the value to the function being called a reference to the original value is passed same values can be accessed by other values.
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
void swap(int,int);
int a,b;
a=7;
b=4;
cout<<"Original value";
cout<<"a="<<a<<"b="<<b<<"\n";
swap(a,b);
cout<<"The value after swap()are"<<"\n";
cout<<"a="<<a<<"b="<<b<<"\n";
getch();
return 0;
}
void swap(int &x,int &y) //Function Definition
{
int temp;
temp=x;
x=y;
y=temp;
cout<<"Swapped values are"<<"\n";
cout<<"a="<<x<<"b="<<y<<"\n";
}
Comments