Examples Of Functions In C Programming

This tutorial will teach you how to use functions in C programming. A function is a collection of statements that accomplishes a specific task. Assume you’re writing a C program and you need to perform the same task multiple times. In this case, you have two choices:
a) Repeat the same set of statements each time you want to complete the task.
b) Create a function to perform that task and simply call it whenever you need to.
Option (b) is a good practice, and a good programmer always uses functions when writing C code.

Why do we need C functions?

Functions are used for the following reasons:
a) To improve code readability.
b) Increases code reusability; the same function can be used in any program rather than having to write the same code from scratch.
c) Using functions makes debugging the code easier because errors are easily traced.
d) Reduces code size by replacing duplicate statements with function calls.

Function classifications

1) Standard library functions that are predefined

Built-in functions are another name for standard library functions. Standard library functions include puts(), gets(), printf(), scanf(), and others. These functions are already defined in header files (files with.h extensions, such as stdio.h), so we simply call them whenever they are required.
For example, the printf() function is defined in the stdio.h> header file, so in order to use it, we must include the stdio.h> header file in our program with #include stdio.h>.

2) Customizable functions

The functions that we create in a program are known as user defined functions, or a function created by a user is known as a user defined function.

We will now learn how to create and use user defined functions in C programming.

A function’s syntax

return_type function_name (argument list)
    Set of statements  Block of code

return type: Any data type can be used as the return type, such as int, double, char, void, short, and so on. Don’t worry, once you’ve gone through the examples below, you’ll have a better understanding of these terms.
function name: It can be anything, but it is recommended that the functions have meaningful names so that the purpose of the function can be understood simply by looking at it.
argument list: An argument list contains the names of variables as well as their data types. These arguments serve as inputs to the function. For example, a function that adds two integer variables will have two integer arguments.
Code block: A collection of C statements that will be executed whenever the function is called.
Do you find the above terms to be perplexing? – Don’t worry, I’m not going to stop this guide until you’ve learned them all:)
Consider the following scenario: suppose you want to write a function that adds two integer variables.
Let us divide the problem so that it is easier to understand –
Because the function will add the two numbers, it should have a meaningful name such as sum, addition, etc. Take, for example, the name addition for this function.

return_type addition(argument list)

Because this function adds two integer variables, I’ll need two integer variables as input, so let’s include two integer parameters in the function signature. – would be the function signature.

return_type addition(int num1, int num2)

The sum of two integers would yield only integers. Hence function should return an integer value – I got my return type – It would be integer –

int addition(int num1, int num2);

You now have your function prototype or signature. You can now implement the logic in C code as follows:

In C, how do you call a function?

Think about the following: The C programming language

Example1: Including a user-defined function ()

#include <stdio.h>
int addition(int num1, int num2)
     int sum;
     /* Arguments are used here*/
     sum = num1+num2;

     /* Function return type is integer so we are returning
      * an integer value, the sum of the passed numbers.
     return sum;

int main()
     int var1, var2;
     printf("Enter number 1: ");
     printf("Enter number 2: ");

     /* Calling the function here, the function return type
      * is integer so we need an integer variable to hold the
      * returned value of this function.
     int res = addition(var1, var2);
     printf ("Output: %d", res);

     return 0;


Enter number 1: 100
Enter number 2: 120
Output: 220

Example 2: Creating a void user defined function that returns null

#include <stdio.h>
/* function return type is void and it doesn't have parameters*/
void introduction()
    printf("My name is Chaitanya\n");
    printf("How are you?");
    /* There is no return statement inside this function, since its
     * return type is void

int main()
     /*calling function*/
     return 0;


My name is Chaitanya
How are you?

A Few Points to Remember About C Functions:

1) In a C program, main() is also a function.
2) Every C program must have at least one main function ().
3) There is no limit to the number of functions that a C program can have.
4) “Recursion” occurs when a function calls itself. I created a separate guide for it.

Functions in C Terminologies to remember when returning type:

The data type of the returned value. It can also be void, in which case the function returns null.
For example, if the function’s return type is char, the function should return a char value, and the main() function should have a char data type variable to store the returned value.
Structure would resemble –

char abc(char ch1, char ch2)
   char ch3;
   return ch3;

int main()
   char c1 = abc('a', 'x');

Additional Functions in C Topics
1) Function – Call by value method – The actual arguments are copied to the formal arguments in the call by value method, so any function operation on arguments has no effect on the actual parameters.
2) Function – Call by reference method – In contrast to call by value, the address of actual arguments (or parameters) is passed to the formal parameters in this method, which means that any operation performed on formal parameters affects the value of actual parameters.

Related Articles

Leave a Reply

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

Back to top button