Functions in C

Last Updated :

A function is a block of code that performs a specific task. It is a set of statements that take inputs, do some specific computation and produces output. A function can be called multiple times to provide reusability and modularity to the C program.

Example :

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

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

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

Advantage of functions in C :

There are the following advantages of C functions.

  • Reusability is the main achievement of C functions.
  • Functions help us in reducing code redundancy.
  • Functions make code modular.
  • Functions provide abstraction.
  • Debugging of the code would be easier if you use functions, as errors are easy to be traced.

However, Function calling is always a overhead in a C program.

Types of functions in C:

  1. Predefined standard library functions :
    Standard library functions are also known as built-in functions. These functions are already defined in header files (files with .h extensions are called header files such as stdio.h). Functions such as puts(), gets(), printf(), scanf() etc are standard library functions. printf() function is defined in header file.

  2. User Defined functions :
    These functions 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 user defined function :

return_type function_name (argument list)
{
    // code of this function
}
  1. return_type: Return type can be of any data type such as int, double, char, void, short etc.
  2. function_name: It can be anything, but should be meaning and relevant name.
  3. argument list: Argument list contains variables names along with their data types.
  4. code of this function: Set of C statements which perform required task in function.

A function may or may not accept any argument. It may or may not return any value.

  • function without arguments and without return value
  • function without arguments and with return value
  • function with arguments and without return value
  • function with arguments and with return value

Notes :

  1. C uses call by value parameter passing technique.
  2. main() in C program is also a function.
  3. A C program can have any number of functions.
  4. A function can call itself and it is known as “Recursion”.



Please write comments if you find anything incorrect. A gentle request to share this topic on your social media profile.

Comment
Next Article
CPP_Programmer Published 20 Aug, 2020 · 2 min read
Article Tags :

Similar Reads

  • What should we use void main() or int main() ?

    There is question that what should we use void main() or int main() ? void main() { /* ... */ } Or, int main() { /* ... */…

    2 min read
  • C Comments

    Prerequisite – C Language Introduction C comments explain code and improve readability. These do not affect program execution. You can use comments in C to clarify code and describe…

    1 min read
  • C Programming Language Standard

    Prerequisite – C Language Introduction The C programming language has various versions: C89/C90, C99, C11, and C18. C89/C90: Released in 1989/1990. It introduced key language features. C99: This…

    2 min read
  • Features of C Programming Language

    Prerequisite – C Language Introduction C is a simple language made in 1972 by Dennis Ritchie. It’s for system programming. C has low-level memory access, basic keywords, good…

    1 min read
  • C Language Introduction

    C is a programming language developed by Dennis Ritchie in 1972. It has a significant historical impact. It was originally created at Bell Laboratories of AT&T Labs for…

    4 min read
  • Code editors

    Code editors are where programmers spend most of their time. There are two main types of code editors: IDEs and Lightweight editors. IDEs (Integrated Development Environments) IDEs (Integrated…

    1 min read