×

Function in C

Function is a group of statements that are used to perform any task. In other words, we can say that a function is a self- contained a block of programs that perform a coherent task. Why to use function A function is used to divide a large code into modules that help us to debug and maintain the code. We can call any function multiple times. There are various advantages of function:
  • Code re-usability
  • Easy to debug the program
  • Code optimization.
Types of Functions In C language, there are two types of functions that are given below:  

  1. Library Function :
A library function is used to declare the c header file such as scanf(), printf() and many more.
  1. User-defined function
User define function is used to create own function and we can use it multiple times. Defining a Function We can define a function by using the following syntax: Syntax:
return typefunction name( parameter list ) {
body of the function
}
The function definition consists of a function header and a function body. There are the following parts of a function.
  1. Return type: A function may return a value. If we don’t want to return a value then we have to use void.
  2. Function name: We can define the function name.
  3. Parameters: We can pass the value in function at the time of calling.
  4. Function body: Function body is the collection of statements.
Let us consider an example:
#include <stdio.h>
void sum(); // declaring a function
int a=5,b=10, c;
void sum() // defining function
 {
 c=a*b;
 printf("Multiplication of (A*B) : %d\n", c);
 }
int main()
 {
 sum(); // calling function
 return 0;
 }
Output
Multiplication of (A*B) : 50
Call by value and call by Reference in C In C language, there are two ways to pass the value of data to function.
  • call by value.
  • call by reference.
 

Call by value: We can’t change the original value at the time of passing value in the function but it can be changed for the current function only. It will not change the value of a variable inside the caller method such as main().

Let us consider an example:
#include<stdio.h>
void abc(int id, int mob)
 {
 printf(" Passing value inside the abc function");
  }
int main()
{
int id=101, mob=90972762;
abc(id, mob); // passing value to the function
printf("\nValue of ID: %d",id);
printf("\nValue of NAME: %d",mob);
return 0;
}
Output
Passing value inside the abc function
Value of ID: 101
Value of NAME: 90972762
Call by reference: We can change the original value while using call by reference through pass the reference (address).

Let us consider an example:
#include <stdio.h>
void fun(int *n) {
 printf("Adding value before inside the function=%d \n",*n);
 (*n) += 10;
 printf("Adding value after inside the function=%d \n", *n);
}
int main() {
 int x=10;
 printf("Before function call x=%d \n", x);
 fun(&x);//passing reference in function
 printf("After function call x=%d \n", x);
 return 0;
 }
Output
Before function call x=10 
Adding value before inside the function=10 
Adding value after inside the function=20 
After function call x=20

Related Topics

fork() in C

Introduction To create a new function in the system, there is a need for a system call, i.e. fork system call. It is also known as the child process. These child...

2 minutes read.

C Program to Find Largest Number Using Dynamic Memory Allocation

This tutorial will teach us how to locate the highest number a user has ever input into a dynamically allocated memory. C Program: #include <stdio.h> #include <stdlib.h> int main ()  {   int no;   double *data_n;   printf...

1 minute read.

Pointer to pointer in C

A pointer to another pointer is another type of multiple indirections and a chain of many pointers. Generally, a pointer consists of the address of the variable. Once a pointer to...

4 minutes read.

Errors in C

Errors in C Errors are nothing but problems or faults that pretty much occur in all the programming languages. Errors make the behavior of the program seem abnormal, and even the...

4 minutes read.

Conditional Operator in C

In C programming language, the conditional operator (also known as the ternary operator) is a shorthand way of writing an if-else statement. The syntax of conditional operator in C is...

3 minutes read.

How to Calculate Time Complexity in C?

What is time complexity? An algorithm's time complexity measures how long it takes to complete a task in relation to the size of the input. It should be noted that the...

5 minutes read.

Heap Sort in C

In this tutorial, we will learn about heap sorting in C language, but before going to Heap sort, we have to know the concept of Complete Binary Tree. What is Complete...

8 minutes read.

Memory leak in C

What is memory leak in C? Memory leak occurs when we keep allocating memory in the heap without freeing it, i.e., the allocated memory in heap is not released back to...

3 minutes read.

Types of Pointers in C

Pointers in C A pointer is a variable and this variable contains the address of another variable, i.e it’s a variable which has the address of another variable as its value....

4 minutes read.

Long int in C

We use data type to avoid the type of data to be stored for a variable at the time of declaration.So, we can say that the variable type is known...

3 minutes read.

How to return a Pointer from a Function in C?

In the C programming language, pointers are variables that hold the address memory location of another variable. We could also input pointers to a function and return pointers from a...

3 minutes read.

Calloc in C

The calloc() is a library function in C which is used for memory allocation. The calloc() function dynamically allocates multiple blocks of memory to complex data structures. This includes data structures...

3 minutes read.

Queue implementation in C

In the C programming language, the queue is the abstract concept that is similar to stacks. However, it is the part of the data structures that work opposite to that...

4 minutes read.

Use of fflush(stdin) in C

Use of fflush(stdin) in C Usually, fflush() is only used for the output stream. The purpose is to clean (or flush) the output buffer and transfer the buffered data into...

2 minutes read.

Array to function in C

Array to function in C We can create functions that receive an array as an argument. To pass an array into a function, we need to write the name of the...

4 minutes read.

What is a buffer in C?

A buffer is an area of memory set aside for the temporary storage of data. A data buffer (or just buffer) is a region of a physical memory storage used to...

5 minutes read.

Write() function in c

As the name suggests the write () function is used to write the file descriptor. In other words it is used to write any file name without specifying file name,...

3 minutes read.

Ferror() in c

Ferror():  In C, the ferror() function checks assuming there is a blunder in the given stream. The ferror() function is utilized to check for the document blunder on given stream. A return...

4 minutes read.

Decimal to Binary in C

What is a decimal number? A decimal number is a number represented in the decimal number system. This system of binary conversion uses base 10 to represent numbers, i.e. the digits...

3 minutes read.

While Loop in C programming examples

Introduction There is always a header file containing all the essential data regarding inputs and outputs of various functions in the C programs. The following statement describes how to use/add header file...

22 minutes read.