Description

In the C programming language, large program are divided into basic building blocks, which we know as functions. function is a set of programming statements that is enclosed by {}. A function can be calle multiple times to do it repeatedly. By doing this the length of out code. We have to write the same code of statement again and again. The function is also known as procedureor subroutinein other programming language. 

Functions like main()printf(), and scanf(), we have already used. All C programs made up of one or more functions. There is only and only one main function is any program.

Advantage of functions in c programming

There are the following advantage of function in c...

  • If we use the function to write the program, then we can avoid writing the same login of code.
  • We can call c functin in any numbers of time in c program.
  • We can easily read a large c program when the program is divided into multiple functions.
  • In c Programming, Reusability of the functions.

Aspects of function

There are three type of function aspects...

  1. Function Declaration
  2. Function call
  3. Function Definition

Function Declaration

The function is declared globally to tell the compiler what is the function name, what are the parameters of the function and what is the return type of the function.

return_type  function_name(argument_list);

Function Call

The function can be called from anywhere in the program. We can call the same function which is declared and the function is not created, we can not call that function.

function_name(argument_list);

Function Definition

It contains the actual statement to be executed. It is the most important aspect to which the control comes when the function is called. We must pass the same number of functions as it is declared in the function.

return_type function_name(argument_list)
{
    function_body;
}

Types of function

In c programming language, there are two types of functions.

  1. Library function: Library function are those functions which are written in the header files such as printf(), scanf(), gets(), puts(), floor(), malloc() etc.
  2. User-defined function: User definded function are those function which the programmer creates himself according to his convenience.

Return Value

A C function may or may not return a value from the function. Including main() all functions can return a value. void return type is specified if function is returning not value.

Example of Function without return value:

void hello(){
    printf("Hello");
}

If want to return the value from the function , you need to use any data type such as int, long, char etc. The return type depends on the value to be returned from the function.

Example of function with return value:

int get(){
    return 4;
}

Different types of function calling

A function may or may not accept any argument. It may or may not return any value. Based on these facts, there are four different types of function call.

  1. function without argument and without return value
  2. function without argument and with return value
  3. function with argument and without return value
  4. function with argument and with return value

Function without argument and without return value

Example 1

#include<stdio.h>

void name();

void main(){
    
    name();
}

void name(){
    print("lapmos");
}

/*
Output:

lapmos

*/

Example 2 : Sum of two numbers using function.

#include<stdio.h>

void sum();

void main(){
    sum();
}

void sum(){
    int n1, n2;
    printf("Enter the two numbers : ");
    scanf("%d %d", &n1, &n2);
    printf("Sum is : %d", n1 + n2);
}

/*
Output:

Enter the two numbers : 4 5
Sum is : 9

*/

function without argument and with return value

Example: Write a program to calculate the area of circle using function.

#include<stdio.h>

float area();

void main()
{
    printf("Area = %f", area());
}

float area(){
    float PI = 3.14, r;
    printf("Enter the radius of Circle : ");
    scanf("%f", &r);
    
    return PI * r * r;
}        

/*
Output:
Enter the radius of Circle : 2
Area = 12.560000

*/

function with argument and without return value

Example: Write a program to calculate the average of 4 numbers.

#include<stdio.h>

void average(int, int, int, int);

void main()
{
    int n1, n2, n3, n4;
    printf("Enter the four numbers : ");
    scanf("%d %d %d %d", &n1, &n2, &n3, &n4);
    average(n1, n2, n3, n4);
}

void average(int a, int b, int c, int d)
{
    float avg = (a + b + c + d)/4.0;
    printf("Average : %f", avg);
}
/*
Output:
Enter the four numbers : 1 2 3 4
Average : 2.500000

*/

function with argument and with return value

Example: Program to check given number is odd or even using by function.

#include<stdio.h>

int even_or_odd(int);

void main(){
    int n1, f;
    printf("Enter the number : ");
    scanf("%d", &n1);
    f = even_or_odd(n1);

    if(f == 1){
       printf("Given number is Even");
    }
    else{
       printf("Given number is Odd");
    }

}

int even_or_odd(int a)
{
    int flag = 0;

    if(a % 2 == 0){
        flag = 1;
    }
    else{
        flag = 0;
    }
    return flag;
}

/*
Output:
Enter the number : 18
Given number is Even

*/

 

Comments

Login is mandatory to comment Please login