Showing posts with label Function. Show all posts
Showing posts with label Function. Show all posts

Wednesday, 4 July 2018

C Function

When a program grow in size and complexity then we break that program into small module . These module are known as the function . Function increases the understandably of a program logic using function module independent logic can be defined .

Function provides the concept of modularity in a program which is best method to divide a complex program . 

Types of Function :

Function can be divided into two categories :

1. Library Function or  ( built in function )

2. User define or  ( Programmer define function )

1 . Library Function :

      These function are the ready made function provides by the library of the compiler itself . some library function are as the follows . 

A pow () ;-  This function is used to calculate the power of a given number this function works upon two arguments or parameter and after performing the operation always a integers types of variable . This required a header file for this function is < math.h>

Syntax :  int variable name = pow ( argument 1 , argument 2 ) ;

B . sqrt () ;-  This function is used to calculate the square root of a number . This function works on one argument and always return an integers type value .

Syntax :  int variable name = sqrt ( arg ) ;

C . toupper () ;  This function is used to convert a lower case character into upper case . the required header file is < c type.h >

Syntax :   character variable name = toupper ( char variable ) ;

D . tolower () ; This function is used to convert a upper case character into lower case required file    < c type .h >

syntax ; -  character variable name = tolower ( char variable ) ;


EXAMPLE :  Program power of two number using library function in C .

/* power of a number */
# include <stdio.h>
# include <math.h>
void main () 
{
int a ,b , c ;
clrscr () ;
printf ("\n Enter value of a:") ;
scanf ("%d", & a) ;
printf ("\n Enter value of b:") ;
scanf ("%d", & b) ;
c = pow ( a , b ) ;
printf ("\n %d", c) ;
getch () ;
}


EXAMPLE :  Program to convert a upper case character into lower case character  in C .

# include <stdio.h>
# include <math.h>
void main () ;
{
char ch ;
clrscr () ;
printf ("\n Enter a upper case character:") ;
scanf ("%d", & ch) ;
printf ("%c", tolower ( ch) ) ;
getch () ;
}

 User define function in next page 





Friday, 29 June 2018

Difference between Call by value and Call by references using program of swapping of variable in C

Using call by value method :
#include <stdio.h> 
void swap ( int , int ) ;
void main ()
{
int a , b , ;
clrscr () ;
printf ("\n Enter value of a:") ;
scanf ("%d", & a) ;
printf ("\n Enter value of b:") ;
scanf ("%d", & b) ;
swap ( a , b ) ;
getch () ;
}
void swap ( int a , int b )
{
int temp ;
temp = a ;?
a = b ;
b = temp ;
printf ("\n %d", a) ;
printf ("\n %d", b) ;
}

Working :   In this program the value of actual argument will be copied in the formal argument which is written in main () and function definition respectively .
                                                                                            These two types of argument will be totally different because of different address the changes which is done in the function definition will not be visible in the main function . This method in which the value of main function is copied in the variables of function definition is known as the call by value method or argument passed by value method .

Using call by references method : 

/* swapping of variables */
#include <stdio.h> 
void swap ( int* , int * ) ;
void main () 
{
int a ,b ; 
clrscr () ;
printf ("\n Enter a number:") ;
scanf ("%d", &a) ;
printf ("\n Enter a number:") ;
scanf ("%d", &b) ;
swap ( &a , &b ) ;
printf ("\n a = %d b = %d", a,b) ;
getch () ;
}
void swap ( int *a , int *b )
{
int temp ;
temp = *a ;
*a = *b ;
*b = temp ;
}

Working :   In these types of program if we pass the address from main function to the function definition then these address must be stored in a special types of variable and the changes will be visible throughout the program in the function definition and as well as the main function .











 

Monday, 18 June 2018

Program to find out the swap of two variable in C using function

/* swap of variables */
# include <stdio.h>
void swap (int , int) ;
void main ()
{
int a,b ;
clrscr () ;
printf ("\n Enter value of a:") ;
scanf ("%d", & a) ;
printf ("\n Enter value of b:") ;
scanf ("%d", & b) ;
swap (a,b) ;
getch () ;
}
void swap (int a , int b)
{
int temp ;
temp = a ;
a = b ;
b = temp ;
printf ("\n a = %d", a) ;
printf ("\n b = %d", b) ;
}


Sunday, 17 June 2018

Program in C to change its case ( upper to lower and lower to upper) as input from the user using function

/* function prototype */
# include <stdio.h>
char change-case (char) ;
void main ()
{
char ch,c ;
clrscr () ;
printf ("\n Enter a character:") ;
scanf ("%C",  & ch) ;
c = change-case (ch) ;
printf ("%c", c) ;
}
char change-case (char ch)
{
if ( (ch>97) && (ch<122))
{
ch = ch-32 ;
return ch ;
}
else
{
ch = ch+32 ;
return ch ;
}
}

Saturday, 16 June 2018

Program to convert a upper case character into lower case character using function in C

/* upper to lower */
# include <stdio.h>
void upper-to-lower (char) ;
void main ()
{
char ch ;
printf ("\n Enter a upper case character:") ;
scanf ("%d", & ch) ;
upper-to-lower (ch) ;
getch () ;
}
void upper-to-lower (char ch)
{
ch = ch+32 ;
printf ("%c", ch) ;
}

Friday, 15 June 2018

Program to convert lower case character into upper without use of library function in C

/* lower to upper */
# include <stdio.h>
void lower -to- upper (char) ;
void main () ;
{
char ch ;
clrscr () ;
printf ("\n Enter a lower case character:") ;
scanf ("%d", & ch) ;
lower to upper (ch) ;
getch () ;
}
Void lower to upper (char ch)
{
ch = ch-32 ;
printf ("%c", ch) ;
}

Program to find factorial of a number by using user define function in C

/* function prototype */
# include <stdio.h>
int fact (int) ;
void main ()
{
int a,b ;
clrscr () ;
printf ("\n enter the value of a:") ;
scanf ("%d", & a) ;
b = factorial(a) ;
getch () ;
}
int factorial (int a)
{
int fact = 1 ;
while (a!=0)
{
fact=fact*a ;
a-- ;
}
getch ;
}


Thursday, 14 June 2018

Program to find maximum of two number in C using user define function

# include <stdio.h>
int maxi ( int , int )
void main ()
{
int a,b,c ;
clrscr () ;
printf ("\n Enter value of a:") ;
scanf ("%d", & a) ;
printf ("\n Enter value of b:") ;
scanf ("%d", & b) ;
c= max (a,b) ;
printf ("\n Maximum no is %d", c) ;
getch () ;
}
int maxi (int a , int b)
{
if (a>b)
return a ;
else
return b ;
}



Wednesday, 13 June 2018

Program to print first n terms of fibonacci series by user define function in C

# include <stdio.h>
void  fibo (int ,int ,int) ;
void main ()
{
int a=0,b=1,n ;
clrscr () ;
printf ("\n How many terms of fibo series you want to print:") ;
scanf ("%d", & n) ;
fibo (a,b,n) ;
getch () ;
}
void fibo (int a,int b,int n)
{
int counter =0 ;
int sum ;
printf ("\n %d", a) ;
printf ("\n %d", b) ;
while (counter < n-2)
{
sum =a+b ;
printf ("\n sum =%d", sum) ;
a=b ;
b=sum ;
counter++ ;
}
}

DRY RUN :
If n = 3 , a = o , b = 1 , counter = 0
( 0 < 3 ) TRUE 
Sum = a + b ;  0 + 1 = 1
( 1 < 3 ) TRUE                        Counter = 1
Sum = a + b ;  1 + 1 = 2
( 2 < 3 ) TRUE                         counter = 2
Sum = a + b ;  1 + 2 = 3
( 3 < 3 )  FALSE                       counter = 3

The output of this program is comes as 0 , 1 , 1 , 2 ,3 .            





Featured post

Kalgudi Interview Round!!

Those who cleared the second round of kalgudi got an email from exceller to complete their Kalgudi Interview Round. So here we will discuss ...