Pages

Saturday 4 September 2021

Functions in c++

Functions are used for Placing or Storing the Code which is to be Repeated Several Times. For Example, if we need Same Code, then we must have to Write that Code Again and Again So that for Removing this Task, we uses functions

Functions Provides us the Following Features

• Reusability of Code : Means Once a Code has Developed then we can use that Code any Time.

• Remove Redundancy: Means a user doesn’t need to Write Code Again and Again.

• Decrease Complexity: Means a Large program will be Stored in the Two or More Functions. So that this will makes easy for a user to understand that Code.

There are Two Types of Functions 

• Built in Functions

All the functions those are provided by the C++ Language are Pre defined and Stored in the Form of header Files and they are refered to the Built in Functions For example. cin and cout, getch , Clrscr 

• User Defined functions

But in the other hand the Functions those are developed by the user for their Programs are known as User Defined Programs. When a user wants to make his Own Function, then he must have to follow the Following Operations.

• Function Declaration or Prototyping

• Function Defining

• Calling a Function 

1) Function Declaration or Prototyping: The Function declaration contains the Name of Function, Return type of the Function and also the Number of Arguments that a User will takes for performing the Operation.

The Function Prototyping Contains 

1) Return Type of a function: If a Function is declared with the void Keyword or if a Function Contains a void then that’s means a Function Never Returns a value. If a Function Contain any other data type means if a Function Contains int or Float then the Function must return a value to the user.

2) Name of Function : The Name of Function must be valid and the name of function must be Start from any Alphabet and the Name of Function doesn’t Contains any Spaces and Special Character For Example Space , * sign etc.

3) Argument List: A Function may have zero or More Arguments.The Arguments those are used by the Function Calling are known as the Actual Arguments and the Arguments those are used in the Function declaration are Known as the Formal Arguments, When we call any Function then the Actual Arguments will Match the Formal Arguments and if a proper Match is Found, then this will Executes the Statements of the Function otherwise this will gives you an error Message.

There are Two Ways for Calling a Function 
1) Call by Value:-when we call a Function then we will just pass the variables or the Arguments and we doesn’t Pass the Address of Variables ,So that the function will never effects on the Values or on the variables. So Call by value is just the Concept in which you must have to Remember that the values those are Passed to the Functions will never effect the Actual Values those are Stored into the variables.

2) Call By Reference :-When we pass the Address of variables to the Arguments then a Function may effect on the Variables. Means When a Function will Change the Values then the values of Variables gets Automatically Changed. And When a Function performs Some Operation on the Passed values, then this will also effect on the Actual Values.

Program on Call by value in C++
In call by value, original value is not modified.
#include<iostream.h>
#include<conio.h>
void cbv(int,int); // function declaration or prototype

void cbv(int p,int q)  // called function
 {
  int temp;
  temp=p;
  p=q;
  q=temp;
  cout<<"\n Value of p (inside function) = "<<p;
  cout<<"\n Value of q (inside function) = "<<q;
 }

void main()
{
 clrscr();
 int a,b;
 cout<<"\n Enter a value :";
 cin>>a;
 cout<<"\n Enter b value :";
 cin>>b;
 cout<<"\n Initial values of a= " <<a << " and b is:"<<b;
 cout<<"\n Calling the function ";
 cbv(a,b);   // calling function
 cout<<"\n Final values of a= " <<a << " and b is :"<< b;
 getch();
}

Output:
Enter a value :11
Enter b value :22
Initial values of a=11 and b is: 22
Calling the function 
Value of p (inside function) = 22
Value of q (inside function) = 11
Final values of a=11 and b is: 22

Example 2:
#include<iostream.h>  
#include<conio.h>  
void change(int data);  
void main()  
{  
int data = 3;  
change(data);  
cout << "Value of the data is: " << data<< endl;  
getch();  
}  
void change(int data)  
{  
data = 5;  
}  

Output:
Value of the data is: 3


Program on Call by reference in C++
In call by reference, original value is modified because we pass reference (address).

#include<iostream.h>
#include<conio.h>
void cbr(int *,int *); // function declaration or prototype

void cbr(int *p,int *q)  // called function
 {
  int temp;
  temp=*p;
  *p=*q;
  *q=temp;
  cout<<"\n Value of p (inside function) = "<<*p;
  cout<<"\n Value of q (inside function) = "<<*q;
 }

void main()
{
 int a,b;
 cout<<"\n Enter a value :";
 cin>>a;
 cout<<"\n Enter b value :";
 cin>>b;
 cout<<"\n Initial values of a= "<<a <<" and b is :" <<b;
 cout<<"\n Calling the function ";
 cbr(&a,&b);   // calling function
 cout<<"\n Final values of a= "<<a <<" and b is :" <<b;
 getch();
}

Output:
Enter a value :11
Enter b value :22
Initial values of a=11 and b is: 22
Calling the function 
Value of p (inside function) = 22
Value of q (inside function) = 11
Final values of a=22 and b is:11

No comments:

Post a Comment

Constructors & Destructors in c++

  Constructors :  A Constructor is a special member function, which is used to initialize the objects of its class. The Constructor is invok...