Category Archives: C Functions

C Recursion

C Recursion

In C programming, recursion is a process where a function calls itself to solve a complex iterative task. The function that calls itself is known as a recursive function. The recursive function calls itself to solve a complex iterative task by dividing it into sub-tasks. In this tutorial, you’ll learn about recursion in C. In particular, you’ll learn:

  • What Is Recursion
  • What Is Recursive Function
  • Working Of Recursive Function
  • Recursion flowchart
  • Characteristics of a Recursive function
  • Advantages Of Recursion
  • Disadvantages Of Recursion

What Is Recursion In C

Recursion is the process where a function calls itself its subroutine in order to solve a complex iterative task by dividing it into sub-tasks. The function that calls itself is known as a recursive function. Any function which calls itself recursively is called a recursive function. The process of calling a function by itself is called recursion. It is an efficient approach to solving a complex mathematical computation task by dividing it into sub-tasks. The approach to solving a problem in recursion is called Divide and Conquer.

Any problem that can be solved recursively, can also be solved iteratively but recursion is considered a more efficient method of programming. It requires the least amount of code to perform some complex tasks using recursion instead of another method. Although recursion is not recommended for all problems, it is best suited for some problems like sorting, searching, Inorder/Preorder/Postorder Tree Traversals, DFS of Graph algorithms. However, recursion must be implemented carefully. Otherwise, it may lead to an infinite loop if no base condition is met that will terminate the function.

What Is Recursive Function

Any function which calls itself recursively is called a recursive function. A recursive function calls itself to solve a complex iterative task by dividing it into sub-tasks. A recursive function repeats itself several times to compute or return the final output. Recursion must be implemented carefully otherwise it may lead to an infinite loop. The recursive function must have a valid terminating condition or base case that will terminate the function. At this point, recursion stops, and the final result is returned from the function.

Syntax Of Recursive Function In C

The general pseudocode representation or syntax of a recursive function in c programming is as follows:

Syntax:-

Characteristics of a recursive function In C

The General Characteristics of a recursive function is as follows:

  • A recursive function is a function that calls itself.
  • The speed of a recursive program is slower because of stack overheads.
  • A recursive function must have terminating conditions and recursive expressions.

Note:- Recursive function must have a valid terminating condition otherwise it leads to an infinite loop.

How does recursion work In C?

To understand how recursion works let’s have one of the popular examples of recursion. In this example, we will calculate the factorial of n numbers. The factorial of n numbers is expressed as a series of repetitive multiplication as shown below:

Suppose we want to find a factorial of 5, then this will be represented as following using recursion:

The flowchart image illustrates the working of recursive function to find factorial of 5 :

c-recursion-flowchart

Here, the recurse() function is a recursive function. It calls itself inside the functional body.

Factorial Program in C Using Recursion

The following factorial program demonstrates the use of the recursive function to find the factorial of a given number using recursion in c programming.

Program:-

Output:-

Suppose the value of i=5, since i is not equal to 1, the statement:

will be executed with i=5 i.e.

As you can see this statement again calls a factorial function with value i-1 which will return the value:

This recursive calling process continues until the value of i is equal to 1. When i is equal to 1 it returns 1 and execution of this function stops. We can review the series of recursive call as follow:

Fibonacci Series In C Using Recursion

The following Fibonacci series program demonstrates the use of the recursive function to generate the Fibonacci series of a given number using recursion in c programming.

Program:-

Output:-

Advantages of Recursion In C

Advantages of recursion are as follows:

  • It requires a few variables which make the program clean.
  • It shortens the complex and nested code.

Disadvantages of Recursion In C

Disadvantages of recursion are as follows:

  • It is hard to debug the recursive functions.
  • It is tough to understand the logic of a recursive function.

C Variable Arguments

In C Programming Language a function usually takes a fixed number of arguments and their data type is also declared therein function declaration, in this scenario we are aware of the number of c variable arguments required, but there are situations come across where the number of arguments is not fixed or not known beforehand when the function is written. The most well-known example would be printf, which takes one required parameter, and may also optionally be passed one or more additional parameters.

Let’s take look at the below example code –

In C Programming Language we can define a function with a variable number of arguments. This can be achieved using three special macros va_start, va_arg & va_end are defined in stdarg. h, to process the variable arguments.

In order to access the c variable argument list, va_start must be called as below –

va_start() is used to initialize pvar to the beginning of the variable argument list. va_start() must be invoked before any access to the unnamed arguments.

The parameter ‘pname’ is the identifier of the rightmost parameter in the variable parameter list in the function definition (the one just before the ellipses ie., “, …”).

After the initialization, we can access the variable argument list via va_arg macro.

pvar is a pointer to the variable argument list, and type is the type name of the next argument to be returned.

va_arg() expands to an expression that has the type and value of the next argument in the call. The parameter pvar must be initialized by va_start(). Each call to this macro will extract the next argument from the argument list as a value of the specified type.

When all the arguments have been processed, the va_end function should be called. This will prevent the va_list supplied from being used any further. The va_end function is declared as

pvar is a pointer to the variable argument list

The va_end() macro is used to clean up. If va_end is not used, the behavior is undefined. The entire argument list can be re-traversed by calling va_start again, after calling va_end.

Example:

 

C Functions

As we know C Programming Language is a function-oriented programming language, functions are one of the key building blocks of any c program. The function is a way to wrap up the set of statements to perform the specific task and give it a name so that it can be invoked from anywhere in the program.

Function in C Programming language gives us a way to divide the complete program into subunits that perform a specific task for that program, this way it enhances the modular approach and increases the reusability of the program.

We pass information in a function call as its parameter and the function can either return some value to the point is where it called from or returns nothing.

Advantages of function

  • It enhances the modularity of the program.
  • It enhances the reusability.
  • It makes development easy as development can be shared in a team.
  • It reduces the coupling.
  • It reduces duplication.

Type Of Function –

  • Built-in function
  • User-defined functions

Built-In Function In C-

Built-in functions are the functions available in C Programming Language to perform some common and standard tasks, these functions are available in the C library. These libraries include the functions for file access, mathematical computations, graphics, memory management, etc.
A built-in function can be accessed simply by including the relative header file and at the point of function call by just writing the function name, followed by an optional list of arguments.

User-defined functions In C –

User-defined functions are custom functions defined by the user itself to perform a custom task that is not available as built-in, in this way users can define and write subprograms as functions to perform a task relevant to their programs.

Function Type Based on Structure of function

  • Functions with no arguments and no return value.
  • Functions with arguments but no return value.
  • Function with no arguments but with the return value.
  • Functions with arguments and with the return value.

Functions with no arguments and no return value in C

Syntax:

Functions with arguments and no return value in C

Syntax:

Function with no arguments but with return value in C

Syntax:

Functions with arguments and with return value in C

Syntax:

Structure of a Function

There are two main parts of the function. The function header and the function body.

Function Header

In the first line of the above code

It has three main parts –

  1. The name of the function
  2. The parameters of the function enclosed in parenthesis
  3. Return value type

Function Body

Whatever is written within { } in the above example is the body of the function.

Function Prototype or Function Declaration

A function prototype or declaration is made by declaring the return type of the function, the name of the function, and the data types of the parameters of the function. A function declaration is the same as the declaration of the variable. The function declaration is always terminated by the semicolon. A call to the function cannot be made unless it is declared. The general form of the declaration is:-

Syntax:

Example:

The variable’s name need not be the same as the variables of the parameter list of the function. Another method can be

The variables in the function declaration can be optional but data types are necessary.

Function in Action

Below is a program to swap two numbers with the use of the function –

Output: