Saturday, 17 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<stdio.h>
#include<conio.h>

void show(); // declaring a function

void show()  // defining function
{

printf("Welcome To LearningPoint92\n");
}

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

Example 2(Using parameter)

#include<stdio.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;
printf("Sum is: %d \n",c);
}
void main()
{
clrscr();
add(45,65);  // calling function
getch();
}
Output:
Sum:110
 
Return Statement
#include<stdio.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
printf("Sum is: %d",result);
return 0;
}
Output:
Sum:110

Example 3(Taking value from user)

#include<stdio.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;
printf("Sum is: %d\n",c);
}

void main()
{
int x,y;
clrscr();
printf("Enter two number \n");
scanf("%d%d",&x,&y);
add(x,y);  // calling function
getch();
}

Output:
Enter two number
23
20
Sum:43

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<stdio.h>
#include<conio.h>
void show(int num) {    
    num=num+10;    
 printf("after addition num is=%d\n" ,num); 
}  
  
void main() {    
    int a=20;    
      printf("Before function call  a=%d\n",a);
    show(a);//passing value in function    
     printf("After function call a=%d\n",a); 
getch();  

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

Example 2:
#include<stdio.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(); 
 printf(" Value of a and b before swapping %d%d",a,b);

 swap(a,b);  // passing value to function
printf(" Value of a and b after swapping %d%d",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<stdio.h>
#include<conio.h>
void show(int *num) {    
    *num=*num+10;    
  printf("after addition num is=%d\n" ,*num);        
}    
void main() {    
    int a=20;    
     printf("Before function call  a=%d\n",a);
    show(&a);//passing value in function    
     printf("After function call a=%d\n",a);   
getch();  

}   
Output:

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

Example 2:
#include<stdio.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(); 
printf(" Value of a and b before swapping %d%d",a,b);
 swap(&a,&b);  // passing value to function
printf("Value of a and b after swapping %d%d",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...