Code Point Blog

Iam presenting here ,all type of knowledge if you want to interest knowing about any topic then click below thumbnail view button and knowing.

All Chapter Back to Home Page

Type of function in c language

Functions in C are an integral part of the language and serve various purposes. There are several types of functions based on their return types and parameters:

1.Functions with a Return Value :These functions return a value of a specified data type after performing their task. The return type is explicitly mentioned in the function declaration.

For example:

            
            #include <stdio.h>
            int add(int a,int b);
            return a+b;
            }
                            
            
            

2.Functions with No Return Value (void functions) :These functions do not return any value. They are typically used for tasks that do not need to produce a result. For example:

                
                #include <stdio.h>
                void greet(){
                    printf("hello world !");

                }
                                
                
                

3.Functions with Parameters :

These functions accept input values (parameters) that are used in their operations. Parameters are specified in the function declaration and definition.
For example:
            
            #include <stdio.h>
            int square(int num){
                return num*num;
            }
                            
            
            

4.functions without Parameters : These functions do not take any parameters. They perform their tasks based on predefined logic within the function body.

For example:

            
            #include <stdio.h>
            int gererateRandomNumber(){
                return rand();
            }
                            
            
            

5.Recursive FunctionsThese are functions that call themselves either directly or indirectly. Recursive functions are used to solve problems that can be divided into smaller, similar sub-problems.

For example:

            
            #include <stdio.h>
            int factorial(int n) { 
                if (n == 0 || n == 1) 
                return 1; 
                else 
                return n * factorial(n - 1); 
                } 
                            
            
            

function call

In C language, there are two main types of function calls:

1.Regular (or Direct) Function Call :


This is the most common type of function call in C. When you call a function directly, the function executes, and after completing its task, control returns to the point in the program where the function was called. Regular function calls are straightforward and widely used.
Here's an example of a regular function call:
     
        #include <stdio.h>
        // Function definition
        void greet() { 
        printf("Hello, World!\n"); 
        } 

        int main() { 
        // Regular function call
        greet(); // Function is called directly 
        printf("After the function call.\n"); 
        return 0; 
        } 
        

        Output: 
    
        Hello, World! 
        After the function call. 
                                    
    
    

2.Recursive Function Call :


A recursive function is a function that calls itself, either directly or indirectly. Recursive function calls are used when a problem can be divided into smaller, similar sub-problems, and the solution to the main problem depends on the solution to its sub-problems.

Here's an example of a recursive function call to calculate the factorial of a number:
        
        #include <stdio.h>
        int factorial(int n) { 
            if (n == 0 || n == 1) 
            return 1; 
            else 
            return n * factorial(n - 1); // Recursive function call 
            } 

            int main() { 
            int num = 5; 
            int result = factorial(num); // Recursive function call 
printf("Factorial of %d is %d\n", num, result); return 0; } Output: Factorial of 5 is 120
In the recursive function call example, the `factorial` function calls itself with a smaller value (`n - 1`) until it reaches the base case (`n == 0` or `n == 1`), where the recursion stops.
Regular function calls are the standard and straightforward way to execute functions in C. Recursive function calls are used for specific problems that can benefit from breaking down into smaller sub-problems.