A function is a block of code that performs a specific task.
C language provides an approach in which you can declare and define a group of statements once in the form of a function and it can be called and used whenever required.
C functions can be classified into two categories
1. Library functions
2. User-defined functions
1. Library functions are those functions which are already defined in C library, example printf(), scanf(), strcat() etc. We just need to include appropriate header files to use these functions. These functions are already declared and defined in C libraries.
2. User-defined functions are those functions which are defined by the user at the time of writing program. These functions are made for code reusability and for saving time and space.
Benefits of Using Functions
1. It provides modularity to your program's structure.
2. It makes your code reusable. You just have to call the function by its name to use it, wherever required.
3. In case of large programs with thousands of code lines, debugging and editing becomes easier if you use functions.
4. It makes the program more readable and easy to understand.
Parts of Function :
1. Function Prototype (function declaration)
2. Function Definition
3. Function Call
1. Function Prototype
Syntax:
dataType functionName (Parameter List);
Example: int mutiplication( int a, int b);
2. Function Definition
Syntax:
returnType functionName(Function arguments)
{
// body of the function
}
Example: int multiplication( int x , int y)
{
// body of the function
}
3. Calling a function in C
Syntax:
functionName(parameter list);
Example : int multiplication( int x , int y);
Example 1:
#include <stdio.h>
int average( int a, int b ) /* function */
{
int avg; /* declaring local variable */
avg = ( a + b)/2;
return avg; /* returning the average value */
}
int main()
{
int a, b;
float c;
printf("Enter first number\n");
scanf("%d", &a);
printf("Enter second number\n");
scanf("%d", &b);
c = average( a, b); /* calling the function */
printf("Average is %.2f \n", c);
return 0;
}
Example 2:
#include <stdio.h>
int div1(int a) // function definition
{
if(a%2==0)
{
return 1;
}
else
{
return 0;
}
}
void div2(int b) // function definition
{
if( div_2(b)==1 && b%3 == 0 )
{
printf("This number is divisible by 6.\n");
}
else
{
printf("This number is not divisible by 6.\n");
}
}
int main()
{
div2(12); //function call
div2(25); // function call
return 0;
}
While calling a function, the arguments can be passed to a function in two ways, Call by value and call by reference.
Call by value
Example 1:
#include <stdio.h>
int sum(int a, int b)
{
int c=a+b;
return c;
}
int main()
{
int x =1;
int y = 2;
int z = sum(x, y);
printf("%d", z);
return 0;
}
Example 2:
#include <stdio.h>
void swap( int a, int b ) /* function */
{
int c;
c = a;
a = b;
b = c;
printf("After swapping:\n");
printf("First number: %d\n", a);
printf("Second number: %d\n", b);
}
int main()
{
int n1, n2;
printf("Enter first number:\n");
scanf("%d", &n1);
printf("Enter second number:\n");
scanf("%d", &n2);
swap(n1, n2);
printf("After swapping: \n");
printf("First number: %d\n", n1);
printf("Second number: %d\n", n2);
return 0;
}
Call by reference
Example 1:
#include void swap ( int *a, int *b )
{
int t ;
t = *a ;
*a = *b ;
*b = t ;
}
int main( )
{
int x = 1, y=2 ;
printf("Before swapping:");
printf("\n x value is %d", x);
printf("\n y value is %d", y);
swap( &x, &y); /*calling swap function*/
printf("\n After swapping:");
printf("\n x value is %d", x);
printf("\n y value is %d", y);
return 0;
}
Recursion : Recursion is the calling of a function within the same function.
Example :
#include <stdio.h>
int fact( int c ) /* function definition*/
{
if( c == 0 || c == 1)
{
return 1;
}
else
{
return c*fact(c-1);
}
}
int main()
{
int a;
printf("Enter number\n");
scanf("%d", &a);
int fact = fact(a);
printf("Factorial of %d is %d\n", a, fact);
return 0;
}
No comments:
Post a Comment