Friday 9 February 2018

Functions in C++

Function is a Self-contained block of statement.
Functions are used to divide a large code into module so that it is easy to debug the program.
Types:
     There are two types of functions in C++.They are

1)Library function  or Predefined function :
Library function are those functions which are predefined by C++ compiler e.g printf(),scanf(),getch(),clrscr(),sqrt() etc.

2)User defined function.

Advantages:
Code Reusability.
Less Code: Less code is required in C++ because you don’t need to write the code again and again. It saves code

User defined Functions
The function which is defined by user is called User defined function.

Function Declaration

Return_type  function_name(parameter);

Function definition

Return_type  function_name(parameter)
{  
//function body
}  
Function Call
function_name();

Example 1( Using No parameter)

#include<iostream.h>
#include<conio.h>

void show(); // declaring a function

void show()  // defining function
{

cout<<" Welcome To LearningPoint92"<<endl;
}

void main()
{
clrscr();
show();  // calling function
getch();
}
Output:
Welcome To LearningPoint92

Example 2(Using parameter)

#include<iostream.h>
#include<conio.h>
void add(int a,int b); // declaring a function
void add(int a,int b)  // defining function
{
int c=a+b;
cout<<"Sum: "<<c;
}
void main()
{
clrscr();
add(45,65);  // calling function
getch();
}
Output:
Sum:110
 

Return Statement
#include<iostream.h>
#include<conio.h>

int add(int a,int b); // declaring a function

int add(int a,int b)  // defining function
{
int c=a+b;
return c;
}

int main()
{
int result=add(45,65);  // calling function
cout<<"sum is:"<<result<<endl;
return 0;
}
Output:
Sum:110

Example 3(Taking value from user)

#include<iostream.h>
#include<conio.h>
void add(int a,int b);  // declaring a function

void add(int a,int b)  // defining function
{
int c=a+b;
cout<<"Sum: "<<c;
}

void main()
{
int x,y;
clrscr();
cout<<"Enter two number"<<endl;
cin>>x>>y;
add(x,y);  // calling function
getch();
}

Output:
Enter two number
23
20
Sum:43

Default Argument
Default argument is an argument which will take the default value if you don't specify any argument at the time of function call.
Example:
#include<iostream.h>
int sum(int a, int b, int c=0, int d=0)
{
    return (a + b + c + d);
}

int main()
{
    cout << sum(100, 5) << endl;
    cout << sum(100, 5, 25) << endl;
    cout << sum(100, 5, 25, 30) << endl;
    return 0;
}

Output:
105
130
160

Call by Value 
Original value cannot be changed after function call.
Value passed to the function is called as call by value.
Example 1:
#include<iostream.h>
#include<conio.h>
void show(int num) {    
    num=num+10;    
    cout<<"after addition num is="<< num<<endl;    
}    
void main() {    
    int a=20;    
    cout<<"Before function call a="<< a<<endl;    
    show(a);//passing value in function    
   cout<<"After function call a="<< a<<endl;    
getch();  

}   
Before function call a=20                                                                                                      
after addition num is=30                                                                                                       
After function call a=20

Example 2:
#include<iostream.h>
#include<conio.h>
void swap(int a, int b);

void swap(int a, int b)
{
 int temp;
 temp=a;
 a=b;
 b=temp;
}
void main()
{ 
 int a=50, b=60; 
clrscr(); 
cout<<"Value of a and b before swapping"<<a<<" "<<b;
swap(a, b);  // passing value to function
 cout<<"Value of a and b after swapping"<<a<<" "<<b;
 getch(); 
} 
Output:
Value of a and b before swapping 50 60
Value of a and b after swapping 60 50

Call by Reference
Original value can be changed after function call.
We need to use ampersand (&) symbol before the argument name to pass value as a reference.
Example 1:
#include<iostream.h>
#include<conio.h>
void show(int *num) {    
    *num=*num+10;    
   cout<<"after addition num is="<< *num<<endl;       
}    
void main() {    
    int a=20;    
     cout<<"Before function call  a= "<< a<<endl;    
    show(&a);//passing value in function    
    cout<<"After function call a=" << a<<endl;    
getch();  

 
Output:

Before function call  a=20                                                                                                      
after addition num is=30                                                                                                       
After function call a=30

Example 2:
#include<iostream.h>
#include<conio.h>
void swap(int *a, int *b)
{
 int temp;
 temp=*a;
 *a=*b;
 *b=temp;
}

void main()
{ 
 int a=50, b=60; 
 clrscr(); 
 cout<<"Value of a and b before swapping"<<a<<" "<<b;
 swap(&a,&b);  // passing value to function
 cout<<"Value of a and b after swapping"<<a<<" "<<b;
 getch(); 
}
 Output: 
Value of a and b before swapping 50 60
Value of a and b after swapping 60 50


No comments:

Post a Comment

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...