C Programming Questions and Answers

Questions Index

Question: Write and explain the following types of functions with the help of an example program for each :
(i) Function with no arguments and no return value.
(ii) Function with arguments and no return value.
(iii) Function with no arguments and with return value.
(iv) Function with arguments and with return value.

Answer:

(i) Function with no arguments and no return value.

This kind of function does not receive any data from the calling function and does not return any value.

#include <stdio.h>

void message();

int main()
{
    printf("Statement 1\n");
    message();
    printf("Statement 2\n");

    return 0;
}

void message()
{
   printf("Inside function\n");
}

(ii) Function with arguments and no return value.

This kind of function receives data from the calling function but does not return any value.

#include <stdio.h>

void print_area(float);

int main()
{
    float radius;
    printf("Enter radius of circle: ");
    scanf("%f", &radius);
    print_area(radius);

    return 0;
}

void print_area(float r)
{
    float area;
    area = 3.14 * r * r;
    printf("The area of circle is %f\n", area);
}

(iii) Function with no arguments and with return value.

This kind of function does not receive any data from the calling function but returns a value.

#include <stdio.h>

int get_sum();

int main()
{
   int s = get_sum();
   printf("%d", s);
   return 0;
}

int get_sum()
{
    int i, sum=0;
    for(i=1;i<=10;i++)
	   sum+= i;
    return sum;
}

(iv) Function with arguments and with return value.

This kind of function receives data from the calling function and returns a value.

#include <stdio.h>

int area_rectangle(int, int);

int main()
{
    int l, b, area;
    printf("Enter the length : ");
    scanf("%d", &l);

    printf("Enter the width : ");
    scanf("%d", &b);

    area = area_rectangle(l, b);

    printf("The area of the rectangle : %d", area);

    return 0;
}

int area_rectangle(int length, int width)
{
    return length * width;
}