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

Function in C language

In C programming, a function is a reusable block of code that performs a specific task. Functions are essential constructs that allow you to break down complex problems into smaller, more manageable pieces, making the code more organized, readable, and maintainable.

In C, a function is defined using the following syntax:

     
         return_type function_name(parameters) {
        // Function body (code)
        return value; // If the function has a return type
        } 

                
            

Let's break down the parts of a function declaration:

return_type :This specifies the data type of the value that the function returns after completing its task. If the function does not return any value, the return type is 'void'.

function_name :This is the unique name given to the function, which is used to call it from other parts of the program.

parameters : These are input variables passed to the function that it can use during its execution. Functions can have zero or more parameters.

Function body :This is the block of code that contains the statements to be executed when the function is called.

return value :If the function has a return type other than `void`, it must return a value of that type using the `return` statement.

Here's a simple example of a function in C:

        
        #include <stdio.h>
         // Function declaration  
         int add(int a, int b) {
        return a + b; 
        } 
        
        int main() { 
        int result = add(5, 3); // Calling the function 
         printf("Result: %d\n", result); 
        return 0; 
                    } 

        Output: 
        Result: 8 

                
            

In this example, we defined a function named 'add' that takes two integer parameters 'a' and 'b', and it returns their sum. The function is called inside the 'main' function, and the result is printed