×

Types of Function in C

A function is a piece of code that accomplish a certain operation for a specific task.

There are two types of functions in C:

  • System-defined function (Library Function)
  • User-defined function

1. System defined Function (Library Function):

The system-defined functions are also known as the pre-defined standard library functions, which are already defined in the C library. 

Functions such as printf() , scanf() ,put() ,gets(), etc., are the library functions.

These functions are already specified in header files, and such header files start with a .h extension, for example <stdio.h>. We call these header files when we need to use them.

When we use the pre-defined functions, we must include the header file as the header file contains the declaration of a pre-defined function.

2. User-defined function:

 The C programming language allows programmers to define their functions despite having hundreds of library functions. User-defined functions are those that are defined by the end-user of the software. According to the requirements, a programmer may define any number of functions.

The declaration and function definition of a user-defined function is a must. Without the declaration and definition, we cannot access our user-defined function.

The definition of the function is executed each time it is called.

For example, Let’s say we create a function called sum() with two parameters and a return value, then its declaration and definition can be done as illustrated in below code:

Program:

#include<stdio.h>
int main(){
   int number1, number2, output ;
   int sum(int,int) ; // function declaration
   printf("Enter any two integer numbers : ") ;
   scanf("%d\n%d", &number1, &number2);
   
   output = sum(number1, number2) ; // function call 
   
   printf("Sum of the two number is = %d", output);
   return 0;
}
int sum(int x, int y) // function definition
{
   return x+y ;
}

Explanation:

  • Here we have declared a function "int sum(int, int),"  which has two arguments and the return type is int.
  • Function definition runs the code contained therein, returning to the function call with the return value.
  • The calling function "sum(number1, number2)" gets the execution control back from the return, which is present in the sum function body. And the value of "x+y" replaces the calling function, and this value is assigned to the output.
  • We get our sum of two numbers on display due to the printf() function.

Program Output:

Types of Function in C

The function call works as below:

  • When we call a function, the execution control moves from the calling function to the called function.
  • When the execution of the called function is completed, the execution control moves back to the calling function from the called function.
  • The called function arguments receive one or more data from the calling function. In contrast, after completing the execution of the called function, the called function gets control back and receives only one value known as the "return value". This means that the data values transmitted from the calling function to the called function are referred to as parameters. The data values transferred from the called function to the calling function are known as returned values.

Categorization of Functions based on Arguments and Return Value

The functions are categorized as follows based on the data flow between the calling function and the called function.

  1. Without arguments and without return value
  2. With arguments and without return value
  3. Without arguments and with return value
  4. With arguments and with return value

1. Without arguments and without return value:

These types of functions have no arguments in parentheses and do not return any value.

This means in these types of functions, we would not pass any parameter while defining, declaring, or calling it.

Between the caller function and the called function, no data is sent. 

Syntax: 

void FunctionName()
{
// function body contains statement
}

Program:

#include<stdio.h>


int main(){
   int sum() ; // function declaration
   sum() ; // function call    
   return 0;
}
void sum() // function definition
{
   int number1, number2 ;
   printf("Enter any two integer numbers : ") ;
   scanf("%d\n%d", &number1, &number2);
   printf("Sum of the number1 and number2 is= %d", number1+number2 ) ;
}

Explanation:

  • Here we declared a function "int sum()"  which has no arguments and does not return any value.
  • When the execution starts, then the control goes on calling the function. As we call our sum() function, control goes to the sum() function body. 
  • Because this sum() function takes nothing nature type, so we have to declare variables “number1” and “number2” with the integer data type. And also, there are no passing values to the called function. That’s why we used the scanf() function in the sum() body for taking input through the user.
  • This sum() function takes nothing nature type, thats why we did not use return keyword and also instead the use of “int” data type, we use “void” data type which do not return any value.
  • Following the user's input, the printf() function will run, and control will once more return to the calling function.

Program Output:

Types of Function in C

2. With arguments and without return value

In these functions, called function takes arguments from the calling function.

The parameters provided in the called function are referred to as formal parameters.

The arguments used in the calling function are the actual parameters.

This function does not return any value.

Program:

#include<stdio.h>


void addition(int, int);        


int main()
{
  int x, y;


  printf("\n Enter any desire number \n");
  scanf("%d %d",&x, &y);




  addition(x, y); // call by value
  return 0;
}


void addition(int a, int b) 
{
  int c; 
  
 c = a + b;


  printf("Addition of %d and %d is = %d \n", a, b, c);
}

Explanation:

  • Here we declared a function “void addition(int, int),” which has two parameters of int data type.
  • We defined our function as “void addition(int a, int b)”.
  • We can easily see in the function definition that this function is a “takes something” type because the addition function has two formal arguments which receive the values from the calling function,
  • The addition function is also a “returns nothing ” type which means this function does not return any value. For this, we used void data type.
  • In the function definition, we have variable “c” with an int data type.
  • The “c” variable stores the value of the sum of “a” and “b”.
  • In the main() function body, we have two variables, “x” and “y,” and our calling function, “addition(x, y)”.
  • As the execution starts, control goes on the main function body, printf() and scanf() function gets executed, and then execution control goes on the addition function.
  • As the addition function is called by the main() function, execution control goes on the addition function body. And as the function is called, then data of the “x” and “y” variables will pass on to “a” and “b” variables.
  • The addition function body will execute, and control will move back to the called function.
  • We get our result of the addition of two integers.

Program Output:

Types of Function in C

3. Without arguments and with return value

In this kind of function, no value is passed from calling function to called function However, this method returns a value that is passed to the function that called it. and the return value replaces the calling function.

So, this is the function of takes nothing but return something type.

Program:

#include<stdio.h>
int addition();        
int main()
{
  int s;
  s=addition();
  printf("addition of the two integer is: %d",s);




   return 0;
}


int addition() 
{
  int a,b,c; 
   printf("\n Enter any desire number \n");
  scanf("%d %d",&a, &b);
  c = a + b;
  return c;
}

Explanation:

  • Here we declared a function “int addition(),” which has no arguments in the parenthesis. But this function returns some value.
  • In the addition() function body, we can see that we would not get any value from the calling function because we do not have any arguments in function parenthesis. That's why we have to take input from the user. For this, we use the scanf() function in the function body.
  • We have three variables, “a”, “b”, and “c,” with the integer data type.
  • We stored our addition result in the “c” variable.
  • Then we use the return keyword, which returns the value of “c”. and as it returns the value of “c”, controls will move back to the calling function, which is present in the main() function body.
  • In the main function body, we have one variable, “s” with the integer data type.
  • “s” variable assigned with the calling function. As our control moves back to calling function, then calling function is replaced by the “c” value.
  • The “c” value is assigned to the “s” variable, and with the help of the printf() function, we get our output.

Program Output:

Types of Function in C

4. With arguments and with return value

In these types of functions, we can pass the values from calling function to called function.

This function also returns some value.

The return type of return depends on the return type of function declaration.

Program:

#include<stdio.h>


int addition(int,int);        


int main()
{
  int x,y,s;
     printf("\n Enter any desire number \n");
  scanf("%d %d",&x, &y);
  s=addition(x,y);
  printf("addition of the two integer is: %d",s);
   return 0;
}


int addition(int a, int b) 
{
  int c; 
  c = a + b;
  return c;
}

Explanation:

  • Here we declared a function “int addition(int, int),” which has arguments in the parenthesis and returns some value, and the return type is int.
  • In the addition() function definition, we get the values of arguments which are present in the parenthesis of called function because we get the value of arguments, so in that case, we do not need any input from the user side.
  • In the addition function body, we have a variable “c” with the int data type.
  • We stored our addition result in the “c” variable.
  • Then we used the return keyword, which returns the value of “c”. and as it returns the value of “c”, controls will move back on the calling function, which is present in the main() function body.
  • In the main() function body, we have three variables “x”, “y,” and “z”.
  • We take input from the user, and these values of “x” and “y” will pass on the called function arguments means these values are received by the “a” and “b” variables.
  • “s” variable is assigned with the calling function “addition(x,y)”. As our control moves back to the calling function, then the calling function replace the “c” value.
  • The “c” value is then assigned to the “s” variable, and with the help of the printf() function, we get our output.

Program Output:

Types of Function in C

Related Topics

Sum of N numbers in C using For loop

Before we move on the program of sum of N numbers, first we have to know about the For Loop statement. The syntax of ‘for’ loop in C programming language is...

3 minutes read.

Loop Statement in C

Loop statement is used to execute one or more statement repeatedly multiple times. There are three types of loops in C language. Why use loop? We can use loop because it executes a...

2 minutes read.

Simple Programs in C Language

In this article, we will discuss the basic programs in C language. Addition of two values in C. Swapping two values. Finding the odd or even values. Finding vowels and consonants in C. Finding the...

11 minutes read.

LCM of two numbers in C

LCM is a mathematical term which stands for Least Common Multiple. LCM of any two numbers is the smallest positive value(number) which is evenly divisible by the two given numbers. Consider...

4 minutes read.

C Array

An array is a collection of elements that are used to hold the fixed number of values of the same type. We cannot change the size and type of array...

2 minutes read.

Unformatted input() and output() function in C

Unformatted Input and Output functions take the character, character array, and strings as input. We can read-only character data types with these functions. These functions are already defined in the...

5 minutes read.

While Loop Syntax in C

What is Loop? The statements in the sequence are repeatedly executed via looping statements in C until the condition is met. The body of a loop and a control statement make...

4 minutes read.

How to open a C file on android mobile

A C file is a file that has a .c extension and contains code written in the C language. C is a computer programming language developed by Dennis Ritchie at...

4 minutes read.

Typecast vs. typedef in C

Typecast vs. typedef in C Typecast In the C programming language, converting the data type from one form to another is known as type casting or the type conversion. It is a...

5 minutes read.

Conio.h in C

Header files are the source files with the extension of .h. In c language header files are referred to as the helping files and they contain definitions of various functions...

4 minutes read.

C Program to Find the Largest Number using Ternary Operator

In this tutorial, we will write some program to determine which of the provided integers is larger using the ternary operator, also known as the conditional operator in C. Ternary Operator: The ternary...

3 minutes read.

Builtin functions of GCC compiler

Certain built-in functions are present in the GCC compiler. These features are as follows: Function_ built in_popcount (x) The number of 1s in data of the integer type can be counted using...

3 minutes read.

Results of Comparison Operations in C and C++

In this tutorial, we will explore comparison operators and how the system should compare an incoming value supplied as a context parameter to a given value or range of values. A...

2 minutes read.

Data Structures And Algorithms in C

C language is one of the most flexible and simple languages. So, it is always better to learn any concept in C language compared to other languages. Let us look...

13 minutes read.

Data Types in C

Data type is a very important concept in C programming language. Simply, “A data type is the classification of data values that a data item can have.” We need to...

11 minutes read.

Binomial Coefficient Program in C

What is Binomial coefficient? In the given set of n possibilities, the binomial coefficient(n,k) indicates the order of choosing 'K' results from those possibilities. Binomial coeeficient of posistive n and k...

3 minutes read.

GPA Calculator in C

GPA stands for Grade Point Average. This GPA is used to measure the student's academic performance in educational institutes. By using this, segregation takes place and lets us know how...

4 minutes read.

User Defined Functions in C

C Functions: A function is defined as a block of code that is used to perform some specific task. In functions, we use flower brackets ({}) which contain the set of programming...

5 minutes read.

Runtime vs compile time in C

In the C programming language, the often used terms in every step consist of compile time and runtime. The compile time is referred to the source code that will be...

4 minutes read.

Break, continue and goto statement in C

Break statement Break statement is used to break the process of a loop (while, do while and for) and switch case. Syntax: break; Example 1 while(test Expression) { // codes if(condition for break){ break; } // codes } Example 2 For(int it, condition,...

1 minute read.