C Tutorial

C Tutorial C Language Environment Setup Execution flow of C program C printf and Scanf C Data type C Token Variable in C Operators in C Comments in C Escape Sequence in C C – Storage Classes C Decision control statement Loop Statement in C Break, continue and goto statement in C Type Casting in C Function in C Recursion in C String in C C Array Pointer in C Dynamic memory allocation C –Structure Nested Structure in C Union in C File Handling in C C pre-processor Static Function In C Sizeof In C Selection Sort In C Scope Of Variables In C Runtime Vs Compile Time In C Random Access Lseek In C Queue Implementation In C Pseudo Code In C Prototype In C Pointer To Pointer In C Pointer Arithmetic In C Passing Array To Function In C Null Character In C Merge Sort In C Macros In C Library Functions In C Memory Leak In C Int In C Goto And Labels In C Fibonacci Series In C Fflush In C Derived Data Types In C Data Types In C Const Vs Volatile In C Character Set In C Character Class Tests In C Calloc In C C Pointers Arrays In C Include In C Clrscr In C C Vs Java String Literals In C Types Of Pointers In C Variables In C Volatile In C Why C Is A Middle Level Language Infix To Postfix Program In C Ceil function in C LCM of two numbers in C Quick sort in C Static in C function pointer as argument in C Top Array Keywords in C Add two numbers using the function in C Armstrong program in C using function Array, Declaring Arrays and Array Initialization Limitations of Inline Function in C Merge and Merge sort with example in C Do-While Loop in C For Loop in C While-Loop in C Difference between while and do-while loop in C Array Of Structures in C Data Structures And Algorithms in C Types Of Structures In C How to Avoid Structure Padding in C Use of Structure in C Do WHILE LOOP in C Programming Examples For Loop in C Programming Examples Entry Control Loop in C Exit control loop in C Infinite loop in C Nested loop in C pow() function in C String Handling functions in C Prime Number code in C Factorial Program in C using For Loop Factorial Program in C Using While Loop Fibonacci Series in C Using For Loop Fibonacci series in C using while loop Prime Number Program in C using for Loop While Loop in C programming examples Built-in functions in C Assert() Function C vs Java Strings Call Back Function in Embedded C Else If Ladder fgets() function Ftell() Function getc() function getch() function gets() function Heap Sort Nested if-else statement Pi() Function Positioning of file Write() function abs() function in C Attributes in C C program to find factorial of a number using Recursion Ferror() in c fopen() function in C Fibonacci series program in C using Recursion Formatted Input and output function in C Snake Game in C User Defined Functions in C Beep() function in C Cbrt() function in C Hook() function in C Isalnum() function in C C Program to find the Roots of a Quadratic Equation C Switch Statements Difference between rand() and srand() function in C Difference between while and for loop in C Doubly Linked list in C Example of Iteration in C How to use atoi() function in C How to use floor() function in C How to use sine() function in C How to use Typedef Struct in C Integer Promotions in C C Program Swap Numbers in cyclic order Using Call by Reference C Program to Find Largest Number Using Dynamic Memory Allocation C Program to Find the Largest Number using Ternary Operator C/C++ Program to Find the Size of int, float, double and char Find the Largest Three Distinct Elements in an Array using C/C++ Loop Questions in C Modulus on Negative Numbers in C Multiplication table program in C using For loop Nested Loops in C Programming Examples C Program for Mean and Median of an Unsorted Array Results of Comparison Operations in C and C++ Reverse a Stack using Recursion in C Simple hash() function in C strcat() Function in C Sum of N numbers in C using For loop Use of free() function in C Write a program that produces different results in C and C++ C Function Argument and Return Values Keywords in C Bank management system in C Calendar application in C Floor() Function in C Free() Function in C How to delete a file in C How to move a text in C Remove an element from an array in C Unformatted input() and output() function in C What are linker and loader in C SJF Scheduling Program in C Socket Programming in C Structure in C Tower of Hanoi in C Union Program in C Variable Declaration in C What is Linked List in C While Loop Syntax in C fork() in C GCD program in C Branching Statements in C Comma Operator in C Control statement in C Double Specifier in C How to create a binary file in C Long int in C Palindrome Number in C Pure Virtual Function in C Run Time Polymorphism in C Types of Array in C Types of Function in C What is a buffer in C What is required in each C Program Associativity of Operators in C Bit Stuffing Program in C Actual and Formal Parameters Addition of two Numbers in C Advantages of function in C Arithmetic Progression Program in C Binomial Coefficient Program in C Difference between Array and List in C Diffie-Hellman Algorithm in C How to convert a number to words in C How to convert a string to hexadecimal in C Difference between If and Switch Statement in C C and C++ Binary Files C program that does not Suspend when Ctrl+Z is Pressed Different ways to Declare the Variable as Constant in C Range of Int in C C Program to find the size of a File FIFO Example in the C Language For loop in C Programming GCD program of two numbers in C GPA Calculator in C How to Calculate Time Complexity in C How to include graphics.h in C How to measure time taken by a function in C How to return a Pointer from a Function in C What is the main in C Addition of Matrix in C Booleans in C C Program for Extended Euclidean algorithms C Program of Fencing the Ground Ceil and Floor in C Compound Interest Program in C Displaying Array in C Distance Vector Routing Protocol Program in c Dos.h Header File in C Language DSA Program in C Explain the two-way selection in C Fee Management System in C File Operations in C Malloc function in C Multiplication Table in C Simple Programs in C Language tolower() Function in C Type Conversion in the C Why does sizeof(x++) not Increment x in C

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