Functions in C Programming

Functions in C Programming

Functions in c programming; In this tutorial, you will learn everything about functions in C programming with the help of examples.

Functions in C Programming

  • What is a function of C
  • Types of function in C
  • Syntax of a function in C
  • Declare and Call Function in C
  • Advantages of Functions in C
  • Example 1 – C Program to Find the Area of a Rectangle Using Functions
  • Example 2 – C Program to Find the Area of a Circle Using Functions

What is a function of C

A function is a set of statements enclosed by {}, that performs a specific task.

Types of function in C

There are two types of functions in C programming:

  1. Library Functions: are the functions which are declared in the C header files such as scanf(), printf(), gets(), puts(), ceil(), floor() etc.
  2. User-defined functions: are the functions which are created by the C programmer, so that he/she can use it many times. It reduces the complexity of a big program and optimizes the code.

Syntax of a function in C

Syntax of function in c programming; as follows:

return_type function_name (argument list)
{
    Set of statements – Block of code
}

Declare and Call Function in C

Use the following syntax to declare and call a function in c programming; as follows:

#include <stdio.h>
void functionName()
{
    ... .. ...
    ... .. ...
}

int main()
{
    ... .. ...
    ... .. ...

    functionName();
    
    ... .. ...
    ... .. ...
}

Advantages of Function in C

There are main advantages of functions in C programming:

  • The program will be easier to understand, maintain and debug.
  • Reusable codes that can be used in other programs.
  • To avoid rewriting same logic/code again and again in a program.
  • Can call a C function any number of times from any place in a program.
  • Reusability is the main achievement of C functions.
  • A large program can be divided into smaller modules. Hence, a large project can be divided among many programmers.

Example 1 – C Program to Find the Area of a Rectangle Using Functions

Use the following steps to write a C program using a user-defined function to find the area of a rectangle; as follow:

  • Create user-define function, which name findArea().
  • And in findArea() function, pass width & length of area.
  • Find the area of a rectangle using this formula length * width and return the result.
  • Take input length and width of the rectangle in the program.
  • Call findArea() function with width & length and store result in variable.
  • Finally print area of reactangle
#include<stdio.h>

// function to find ara of rectangle
// function definition
float findArea(float l, float b)
{
  float area;
  area = l * b;
  return area; //return statement
}

int main()
{
  float length, width, result;

  printf("Please enter length of the rectangle: ");
  scanf("%f",&length);
  printf("Please enter width of the rectangle: ");
  scanf("%f",&width);

  result = findArea(length, width); //function calling
  printf("Area of rectangle = %.2f\n",result);

  return 0;
}

Output of the above c program; as follow:

Please enter length of the rectangle: 10
Please enter width of the rectangle: 55
Area of rectangle = 550.00

Example 2 – C Program to Find the Area of a Circle Using Functions

Use the following steps to write a C program with a user-defined function to find the area of a circle; as follow:

  • Create user-define function, which name circleArea().
  • And in circleArea() function, pass radious of area.
  • Find the area of a circle using this formula 3.14 * r * r and return the result.
  • Take input radious of circle in the program.
  • Call circleArea() function with radious and store result in variable.
  • Finally print area of circle.
#include<stdio.h>

// function definition
float circleArea(float r)
{
  float area = 3.14 * r * r;
  return area; // return statement
}

int main()
{
  float radius, area;

  printf("Enter the radius of the circle: ");
  scanf("%f", &radius);

  area = circleArea(radius); //function calling
  printf("Area of circle = %.2f\n",area);

  return 0;
}

Output of the above c program; as follow:

Enter the radius of the circle: 25
Area of circle = 1962.50

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *