
In the C language, a function is a chunk of code that does a certain task. If required, we can call the function many times in the program.
If necessary, a function can return a value after executing an operation. It is optional.
Here you can learn the basics of C programming before going through this article.
Table of Contents
Types of function in C with example
In the C programming language, there are two types of functions:
Built-in functions / Predefined library functions
Built-in functions are functions that are already defined in the C library. All we have to do now is put it to work for us.
Example of Built-In function
printf(), scanf(), getch(), main() etc.
User-defined functions
C enables programmers to create their own functions based on their needs. These types of functions are known as user defined functions.
Example of User-defined function
void display() { printf("Hello, GeekCer!"); }
Advantages of using function in programming in C
- Small functions are easy to write in the ideal world, and a function is simple to understand, write, and debug.
- Small functions are always self-documented and simple to grasp in a short amount of time.
- Any function can be called several times from any location and with any number of parameters.
Main parts of a function in C Programming
There are mainly three parts in the work from start to finish which are as follows
- Function Declaration
- Function Definition
- Calling of a Function
Function Declaration
The term “function declaration” is frequently used to refer to the prototype of a function. The function argument and return type are represented by the name of the function.
void func(int p1, int p2);
Function Definition
The function definition refers to the program’s real logic.
void func(int p1, int p2) { // Function body }
Calling of a Function
A function can be invoked from any program after it has been written. Another function can invoke another function
func(5, 10);
Example of a function in C programming
#include<stdio.h> // Declartion of a function void func(int p1, int p2); void main() { int x, y; x = 5; y = 10; // Calling of a function func(x, y); } // Definition of a function void func(int p1, int p2){ printf("Values are %d and %d.", p1, p2); }
Output: Values are 5 and 10.
Difference between Actual and Formal parameters with example in C
Parameters are divided into two categories: actual and formal parameters. When a function is called, the value must be supplied as an actual parameter/argument to the function. We design a function that accepts one or more parameters. Parameters that accept data from the outside world are known as formal arguments.
#include<stdio.h> // Here parameter1 and parameter2 are the formal parameter/argument void addOperation(int parameter1, int parameter2); void main() { // Here 5 and 10 are the actual parameter/argument addOperation(5, 10); } void addOperation(int parameter1, int parameter2) { printf("Addition is %d", (parameter1 + parameter2)); }
Output: Addition is 15
Call by value and Call by reference in C
These are the methods of passing parameters to the function and this method decides whether the actual parameter changes after modification to the function.
Call by value in C
When using call by value to pass parameters, the called function receives a copy of the original parameter.
The original value will not be altered if the parameter value is changed inside the function.
#include<stdio.h> #include<conio.h> void valueSwap(int val1, int val2); void main () { int val1 = 100; int val2 = 50; valueSwap(val1, val2); printf("\nValue 1 : %d", val1); printf("\nValue 2 : %d", val2); } void valueSwap(int val1, int val2) { int temp; temp = val1; val1 = val2; val2 = temp; }
Output: Value 1 : 100 Value 2 : 50
Call by reference in C
Call by reference will act on the original value, ensuring that any changes made to a variable are permanent rather than temporary.
The actual address of the variable must be sent to the calling function.
#include<stdio.h> #include<conio.h> void swap(int *val1, int *val2); void main () { int val1 = 100; int val2 = 50; printf("\nValues before swap are %d and %d", val1, val2); swap(&val1, &val2); printf("\nValues after swap are %d and %d", val1, val2); } void swap(int *val1, int *val2) { int temp; temp = *val1; *val1 = *val2; *val2 = temp; }
Output: Values before swap are 100 and 50 Values after swap are 50 and 100
Recursive function in C
Recursion is defined as a situation in which a function calls itself. Recursion can be used to modify large complex code by dividing the problem into equivalent problems of its smaller problems.
Here we will see the example of factorial in C programming using recursion method.
#include<stdio.h> #include<conio.h> int factorial(int val) { if(val == 0) return 1; else return (val * factorial(val - 1)); } void main() { int fact = factorial(6); printf("Factorial is %d", fact); }
Output: Factorial is 720