C Function 

A function is a set of statements that take inputs, do some specific computation and produces output.

The idea is to put some commonly or repeatedly done task together and make a function, so that instead of writing the same code again and again for different inputs, we can call the function.

Types of Functions

Depending on whether a function is defined by the user or already included in C compilers, there are two types of functions in C programming

Library Functions : functions which are declared in the C header files such as scanf(), printf(), gets(), puts(), ceil(), floor() etc.

User Defined Functions : functions which are created by the C programmer, so that he/she can use it many times. It reduces the complexity of a big program and optimizes the code.

Advantage of functions in C :

By using functions, we can avoid rewriting same logic/code again and again in a program.

We can call C functions any number of times in a program and from any place in a program.

We can track a large C program easily when it is divided into multiple functions.

Reusability is the main achievement of C functions.

However, Function calling is always a overhead in a C program.

Function Aspects

There are three aspects of a C function.

Function declaration : A function must be declared globally in a c program to tell the compiler about the function name, function parameters, and return type.

Function call : Function can be called from anywhere in the program. The parameter list must not differ in function calling and function declaration. We must pass the same number of functions as it is declared in the function declaration.

Function definition : It contains the actual statements which are to be executed. It is the most important aspect to which the control comes when the function is called. Here, we must notice that only one value can be returned from the function.

Function Syntax

return_type function_name( type1 argument1, type2 argument2, ... )

{


    body of the function


}

Return Type :A function may return a value. The return_type is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return_type is the keyword void.


Function Name :This is the actual name of the function. The function name and the argument(parameter) list together constitute the function signature.


Arguments :A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters.


Function Body :The function body contains a collection of statements that define what the function does.




Example for Function without argument and return value

#include <stdio.h>

void printName();

void main()

{

     printf("Hello");

     printName();

}

void printName()

{

     printf(" Learners");

}


Output: Hello Learners


Example for Function without argument and with return value

#include <stdio.h>

int sum();

void main()

{

    int result;

    printf("\nGoing to calculate the sum of two numbers:");

    result = sum();

    printf("%d",result);

}

int sum()

{

    int a,b;

    printf("\nEnter two numbers");

    scanf("%d %d",&a,&b);

    return a+b;

}

Output:

Going to calculate the sum of two numbers:


Enter two numbers 10 24

The sum is 34


Example for Function with argument and without return value

#include <stdio.h>

void sum(int, int);

void main()

{

    int a,b,result;

    printf("\nGoing to calculate the sum of two numbers:");


    printf("\nEnter two numbers:");


    scanf("%d %d",&a,&b);


    sum(a,b);

}

void sum(int a, int b)

{

    printf("\nThe sum is %d",a+b);

}

Output:

Going to calculate the sum of two numbers:


Enter two numbers 10 24

The sum is 34


Example for Function with argument and with return value

#include <stdio.h>

int sum(int, int);

void main()

{

    int a,b,result;


    printf("\nGoing to calculate the sum of two numbers:");

    printf("\nEnter two numbers:");

    scanf("%d %d",&a,&b);

    result = sum(a,b);

    printf("\nThe sum is : %d",result);

}

int sum(int a, int b)

{

    return a+b;

}


Output:

Going to calculate the sum of two numbers:


Enter two numbers 10 24

The sum is 34

Post a Comment

If you have any doubts, Please let me know
Thanks!

Previous Post Next Post