C Functions

C Flow Control?

In this tutorial, you will learn about if statement (including if...else and nested if..else) in C programming with the help of examples.

C Functions?

In this tutorial, you will be introduced to functions (both user-defined and standard library functions) in C programming. Also, you will learn why functions are used in programming.

C Programming Arrays?

In this tutorial, you will learn to work with arrays. You will learn to declare, initialize and access elements of an array with the help of examples.

C Programming Pointers?

In this tutorial, you'll learn about pointers; what pointers are, how do you use them and the common mistakes you might face when working with them with the help of examples.

C Programming Strings?

In this tutorial, you'll learn about strings in C programming. You'll learn to declare them, initialize them and use them for various I/O operations with the help of examples.

Structure And Union?

In this tutorial, you'll learn about struct types in C Programming. You will learn to define and use structures with the help of examples.

C Programming Files?

In this tutorial, you will learn about file handling in C. You will learn to handle standard I/O in C using fprintf(), fscanf(), fread(), fwrite(), fseek() etc. with the help of examples.

Additional Topics?

In this tutorial, you will learn about enum (enumeration) in C programming with the help of examples.
C Functions

A function is a block of code that performs a specific task.

Suppose, you need to create a program to create a circle and color it. You can create two functions to solve this problem:

  • create a circle function
  • create a color function

Dividing a complex problem into smaller chunks makes our program easy to understand and reuse.


Types of function

There are two types of function in C programming:


Standard library functions

The standard library functions are built-in functions in C programming.

These functions are defined in header files. For example,

  • The printf() is a standard library function to send formatted output to the screen (display output on the screen). This function is defined in the stdio.h header file.
    Hence, to use the printf()function, we need to include the stdio.h header file using #include <stdio.h>.
  • The sqrt() function calculates the square root of a number. The function is defined in the math.h header file.  

Visit standard library functions in C programming to learn more.


User-defined function

You can also create functions as per your need. Such functions created by the user are known as user-defined functions.

How user-defined function works?

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

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

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

The execution of a C program begins from the main() function.

When the compiler encounters functionName();, control of the program jumps to

 void functionName()

And, the compiler starts executing the codes inside functionName().

 

 
 

The control of the program jumps back to the main() function once code inside the function definition is executed.

How function works in C programming?

Note, function names are identifiers and should be unique.

This is just an overview of user-defined functions. Visit these pages to learn more on:


Advantages of user-defined function

  1. The program will be easier to understand, maintain and debug.
  2. Reusable codes that can be used in other programs
  3. A large program can be divided into smaller modules. Hence, a large project can be divided among many programmers.
Previous Tutorial:
Control Flow Examples
Next Tutorial:
C User-defined Functions