×

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

Strcpy() in C

In C language many operations can be performed on a string. Characters in a sequence are known as string. Note:   To use this function we must include the #include<string.h> header file...

4 minutes read.

Recursion in C

Recursion is a programming technique that allows the programmer to call the function within the same function. The function which calls the same function is known as recursive function but a...

1 minute read.

Associativity of Operators in C

What is an Associativity operator? Two operators with equal priority are connected by employing their association when they are present in an expression. There are two different types of associativity: left to rightright...

4 minutes read.

DSA Program in C

How is a Data Structure Implemented? The foundational terms of a data structure are the following terms. Data may be arranged using data structures to make it easier to use. Each data...

20 minutes read.

Kruskal algorithm in C

Given a weighted graph, Kruskal's algorithm generates a spanning tree with the lowest possible weights. Start by creating an edge list for the given graph, including the weights. Sort the...

3 minutes read.

Float in islower() in C

Introduction: This article briefly discusses the float in islower() in C. The islower() function checks if the input character passed inside the function is lowercase. Lowercase letters include (a-z). The islower()...

4 minutes read.

Consumer billing system in C

Introduction : Billing has always been taught to do perfectly, we know today's world is full of products, and We all are using multiple products. We are buying and selling various...

9 minutes read.

How to use sine() function in C

What is Function? The function is a set of statements. It takes input and performs some computation to produce output. The process is a set of codes only achieved when it is...

3 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.

Commenting in C

Commenting in C Commenting in the C language is used to give out the information about the lines of the code which are included. It is one of the things that...

3 minutes read.

Difference between rand() and srand() function in C

What is the rand()?The rand() means random function. This function is used in C. It generates random numbers in the range of 0 to the RAND_MAX. Suppose we generate a...

3 minutes read.

String Handling functions in C

String :- The String is the collection of characters. Every String ends with the null character, and the String is enclosed in the double quotations .ie, "javaTpoint". If we see any...

5 minutes read.

Classification of Programming language

 Classification of Programming language  The programming language represents a set of instructions compiled along with each other to perform a specific task provided by the CPU (Central Processing Unit). A programming...

3 minutes read.

Bigint (BIG INTEGERS) in C with Example

The maximum number of digits that a long, long int can have in C/C++ is 20. The issue is how to store the 22-digit number, which is difficult to do...

9 minutes read.

String literals in C

Constants refer to the fixed values of the program that may not alter during the compiling ad execution. These fixed values are known as literals. In other words, the values...

3 minutes read.

Int in C

The keyword int in C programming stands for integer and it is a data type which is used for variable declarations or declaration of functions of different types. Similar to...

4 minutes read.

C –Structure

C structure is a collection of different types of data that is grouped together. It is used to represent records.Structkeyword is used to create a structure. Each element of a...

1 minute read.

C Data type

Data Types in C with Examples Data types are used to define the type of data that a variable can store to perform a specific operation.ANSI C provides three types of...

2 minutes read.

Pointers in C

Pointers in C: In the C programming language, a pointer is a pointer variable that points to the address of the other variable. It also stores the address of the...

4 minutes read.

Limitations of Synchronisation and Uses of Static Synchronisation in Multithreading

The multithreading component of java is the element around which the idea rotates as it permits simultaneous execution of at least two program pieces for the most significant usage of...

9 minutes read.