Skip to content
  • Facebook
GeekCer Logo

GeekCer

The geek's Coding education and Review centre

  • Home
  • Tutorials
    • Java
    • Servlet
    • JSP
    • Python
    • C Tutorial
    • Spring
    • Spring Boot
    • MongoDB
    • Hibernate
    • Data Structure
  • General Knowledge
  • Biography
  • Grammar
  • Festival (त्योहार)
  • Interview
  • Differences
  • Important
  • Toggle search form

Home » C Language » Function in C Language | Recursive function with example

  • Linear search algorithm | Linear search program in C, Example
    Linear search algorithm | Linear search program in C, Example Data Structure
  • What is Array in Java? Types of array in Java
    What is Array in Java? Types of array in Java Java
  • Different Types of Loans in India, Eligibility, Documents, Benefits
    Different Types of Loans in India, Eligibility, Documents, Benefits Important
  • Lambda Expression in Java 8 | Functional Interface | Example
    Lambda Expression in Java 8 | Functional Interface | Example Java
  • Dynamic memory allocation in C, malloc, realloc, calloc and free
    Dynamic memory allocation in C, malloc, realloc, calloc and free Data Structure
  • Java Comments
    Java Comments : Types of java comments, Syntax & Examples Java
  • Yrkkh - Yeh Rishta Kya Kehlata Hai Serial, Episode, Cast
    Yrkkh – Yeh Rishta Kya Kehlata Hai Serial, Episode, Cast News
  • What is Verb
    What is Verb? Types of Verbs and Examples Grammar

Function in C Language | Recursive function with example

Posted on December 12, 2021March 29, 2022 By admin No Comments on Function in C Language | Recursive function with example
Function in C Language | Recursive function with example

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
    • Built-in functions / Predefined library functions
      • Example of Built-In function
    • User-defined functions
      • Example of User-defined function
  • Advantages of using function in programming in C
  • Main parts of a function in C Programming
    • Function Declaration
    • Function Definition
    • Calling of a Function
  • Example of a function in C programming
  • Difference between Actual and Formal parameters with example in C
  • Call by value and Call by reference in C
    • Call by value in C
    • Call by reference in C
  • Recursive function in C

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

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • More
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Also Read

C Language Tags:What are types of functions in C language?, What are user-defined functions in c programming?, What is a function explain with example?, What is actual and formal arguments?, What is example of function in C?

Post navigation

Previous Post: String Array in C | What is an array of string | String Functions
Next Post: Pointers in C Programming, Definition, Types & Example

More Related Articles

Programming with C Language Tutorial Programming with C Language Tutorial C Language
Structure in C program with example | Nested structure in C Structure in C program with example | Nested structure in C C Language
Array in C programming | Character Array in C Array in C programming | Character Array in C C Language
File Handling in C with examples | File Operations in C File Handling in C with examples | File Operations in C C Language
Statements in C Language | Types of statements in C Language Statements in C Language | Types of statements in C Language C Language
String Array in C | What is an array of string | String Functions String Array in C | What is an array of string | String Functions C Language

Related Posts

  • Programming with C Language Tutorial
    Programming with C Language Tutorial C Language
  • Structure in C program with example | Nested structure in C
    Structure in C program with example | Nested structure in C C Language
  • Array in C programming | Character Array in C
    Array in C programming | Character Array in C C Language
  • File Handling in C with examples | File Operations in C
    File Handling in C with examples | File Operations in C C Language
  • Statements in C Language | Types of statements in C Language
    Statements in C Language | Types of statements in C Language C Language
  • String Array in C | What is an array of string | String Functions
    String Array in C | What is an array of string | String Functions C Language

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • C Home
  • Constants and Operators
  • Statements in C Language
  • Array in C
  • String Array in C
  • Functions in C
  • Pointers in C
  • Structure in C
  • Storage Classes
  • File Handling in C
  • Top C Programs

Recent Posts

  • Structured Vs Unstructured Data in Hindi | Key Difference
  • Jhansi Ki Rani Lakshmi Bai History, Story, Information in Hindi
  • Elon musk Hindi : एलन मस्क हिंदी में, Autobiography,  Net Worth
  • World Environment Day in Hindi : Objective, Importance, Theme
  • Thomas Edison Biography in Hindi – थॉमस एडिसन जीवनी
  • International Nurses Day in Hindi | नर्स दिवस क्यों मनाते हैं?
  • Fork/Join Framework in Java | RecursiveTask, RecursiveAction
  • DBMS in Hindi | DBMS क्या है? | DBMS की विशेषताएं और प्रकार
  • Linear search algorithm | Linear search program in C, Example
    Linear search algorithm | Linear search program in C, Example Data Structure
  • What is Array in Java? Types of array in Java
    What is Array in Java? Types of array in Java Java
  • Different Types of Loans in India, Eligibility, Documents, Benefits
    Different Types of Loans in India, Eligibility, Documents, Benefits Important
  • Lambda Expression in Java 8 | Functional Interface | Example
    Lambda Expression in Java 8 | Functional Interface | Example Java
  • Dynamic memory allocation in C, malloc, realloc, calloc and free
    Dynamic memory allocation in C, malloc, realloc, calloc and free Data Structure
  • Java Comments
    Java Comments : Types of java comments, Syntax & Examples Java
  • Yrkkh - Yeh Rishta Kya Kehlata Hai Serial, Episode, Cast
    Yrkkh – Yeh Rishta Kya Kehlata Hai Serial, Episode, Cast News
  • What is Verb
    What is Verb? Types of Verbs and Examples Grammar
  • Java Tutorial
  • Servlet Tutorial
  • JSP Tutorial
  • Maven Tutorial
  • HTML Tutorial
  • Programs
  • Hindi/English Grammar
  • Difference Between ... and ...
  • HR Interview
  • Important Articles

Write to Us:
geekcer.code@gmail.com

  • About Us
  • Privacy and Policy
  • Disclaimer
  • Contact Us
  • Sitemap

Copyright © GeekCer 2022 All Rights reserved