Operators
Pointers

Functions

What is Function in C ?

Function in a C-language is a group of statements that combines to perform a task. Every Function in c language has at least one function, which is main(), and all the most trivial programs can define additional functions . You can can also make more than on functions in the same code. functions are simple, but because of how C works, the power of functions is a bit limited.

  • Functions receive either a fixed or variable amount of arguments.
  • Functions can only return one value, or return no value.

Why we use Function?

Functions make our code neat and easily readable and avoid us to rewrite the same code again and again.

Types of Functions in C

So, there are two types of functions in C.

  • User-defined Functions
  • Library Functions

Library Function

Library functions in C language are inbuilt functions which are grouped together and placed in a common place called library functions, like Printf()  and Scanf()

User-defined Functions

  • We can also define our own functions in C.
  • Let’s first see how to declare a function of our own.

Declare a Function

We can declare a function as follows.

return_type  function_name ( parameters ) ;

float average( int num1, int num2 );

Lets see an example.

#include<stdio.h>
float square ( float x );
int main( ) { float m, n ; printf ( "\nEnter some number for finding square \n"); scanf ( "%f", &m ) ; // function call n = square ( m ) ; printf ( "\nSquare of the given number %f is %f",m,n ); } float square ( float x ) // function definition { float p ; p = x * x ; return ( p ) ; }

Enter some number for finding square
2
Square of the given number 2.000000 is 4.000000

Function Calling

To use a function, we need to call it. Once we call a function, it performs its operations and after that the control again passes to the main program.

#include<stdio.h>
float average( int num1, int num2 ); /* declaring function named average */


int main() 
{
    int num1, num2; 
    float c; 
    printf("Enter first number\n");
    scanf("%d", &num1);
    printf("Enter second number\n");
    scanf("%d", &num2);
    c = average( num1, num2 ); /* calling the function average and storing its value in c*/
    printf("Average is %.2f\n", c);
    return 0;
}


float average( int num1, int num2 ) /* function */
{
    float avg; /* declaring local variable */
    avg = ( num1 + num2 )/2.0; 
    return avg; /* returning the average value */
}

Enter first number 

Enter second number 

Average is 4.50

#include
// function prototype, also called function declaration
void swap(int *a, int *b); 
 
int main()
{
    int m = 22, n = 44;
    //  calling swap function by reference
    printf("values before swap m = %d \n and n = %d",m,n);
    swap(&m, &n);         
}
 
void swap(int *a, int *b)
{
    int tmp;
    tmp = *a;
    *a = *b;
    *b = tmp;
    printf("\n values after swap a = %d \nand b = %d", *a, *b);
}
values before swap m = 22 
and n = 44
values after swap a = 44
and b = 22

Lets pass an array to Funtion.

#include <stdio.h>
 
#define MAX 5
 
void swap(int *x, int *y);
void bubble_sort(int a[], int size);
void display_array(int a[],int size);
 
void main()
{
    int a[MAX] = {1,3,2,5,4};
 
    printf("The array before sorting:\n");
    display_array(a,MAX);
 
    bubble_sort(a,MAX);
 
    printf("The array after sorting:\n");
    display_array(a,MAX);
}
 
void swap(int *x, int *y)
{
    int tmp = *x;
        *x = *y;
        *y = tmp;
}
 
void bubble_sort(int a[], int size)
{
   int i,j;
   for(i = 0; i < (size-1);i++)
   {
        for(j = 0; j < (size-(i+1));j++)
        {
          if(a[j] > a[j+1]){
            swap(&a[j],&a[j+1]);
          }
        }
   }
}
 
void display_array(int a[],int size)
{
    int i;
    for(i = 0;i < size; i++)
    {
        printf("%d\t",a[i]);
    }
    printf("\n");
}
 
The array before sorting:
1       3       2       5       4
The array after sorting:
1       2       3       4       5

Recursion

Recursion is the calling of a function within the same function.

Lets take factorial of 4.

#include <stdio.h>

int factorial( int a ) /* function */
{
    if( a == 0 || a == 1)
    {
        return 1;
    }
    else
    {
        return a*factorial(a-1);
    }
}

int main() 
{
    int n; 

    printf("Enter number\n");
    scanf("%d", &n);

    int fact = factorial(n);

    printf("Factorial of %d is %d\n", n, fact);
    return 0;
}

 

Enter number 

Factorial of 4 is 24

Comments are closed.