Loading...
Loading...
00:00:00

What Is Function in C?

Function is group of statement that perform some specific task. Function makes program reusable and readable or easy to debug and optimize. Using function, we can reduce rewriting same code again and again in program. function once create and call multiple times from anywhere in program. In c programming language, A program has atleast one function that is called main() function. C program's Execution start from main() function.

Function syntax

return_tye function_name(parameters)
{
    // function body 
}

There Are Two Types Of Functions

Library function(Built in function) - In c language There are so many Built in libraries that have alot of functions. such as string.h header file has string related functions declarations and it's implementations is in it's library files.

User-defined function - This kind of functions are created by programmers.

According To Parameters(Arguments) And Return Type Functions Are Four Types

  1. No argument and no return value- This kinds of function cannot takes any parameters and no returns any kinds of value after function Execution.

  2. take arguments and no return value - This kinds of function can takes argument for perform some specific task and after function Execution it cannot returns value.

  3. no arguments and return value- This kinds of function cannot takes arguments but returns some value after function Execution is complete. function returns only one value.

  4. take arguments and return value- This kinds of function takes arguments and after function Execution is complete, it can returns some value. function returns only one value.

Here is the examples of all types of functions

No argument and no return value

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    int x,y;
    void add()   // function definition with no args and no returns 
    {
        x = 30;
        y = 50;
        printf("The sum of %d and %d is %d\n",x,y,x+y);
    }
    add();   // function call
    return 0;
}

takes argument and no return value

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    int x,y;
    x = 50, y = 30;
    void add(int a, int b)   // function definition taking args but no returns 
    {
        printf("The sum of %d and %d is %d\n",a,b,a+b);
    }
    add(x,y);   // function call with parameters
    return 0;
}

No argument and but return value

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    int x = 50, y = 30;
    int add()   // function definition takes no parameters but return type
    {
        return x + y;   //function return value 
    }
    printf("The sum is %d",add());   // function call with return value
    return 0;
}

takes argument and return value

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    int x = 50, y = 30;
    int add(int a, int b)   // function definition takes parameters, return type
    {
        return x + y;   //function return value 
    }
    printf("The sum is %d",add(x,y));   // function call with return value
    return 0;
}

Functions General Terms

function declarations - function declaration tells the compiler about the function's name, return types and its parameters. A function declaration must be declare globally in C program.

function definition - function definition is actul coding in function body that contains some statement that will be execute when function call.

function call -In C program, function code does not execute unless we call the function. function call mean, when we use function in any where in program that is called function calling. function call may take parameters and it may return some value.

function body - Where we write actual code of particular task in function. function body is enclosed by {} brackets.

function parameters(arguments) - When we call function in C program it may takes some values, passing values in function called function parameters. {} brackets.

function return types - When function is called it may return some value. function can return int, char, float, pointer data type values.

signature of function -In C program, function name and its types of parameters known as signature of functions.