Function in C Programming

A function is a set of statements used to perform various tasks. In C programming, at least one function must be present. Functions are particularly helpful when you have too many lines of code; you can create a function for a specific task.
Some functions are user-defined, which you can create and use, while others are system-defined (e.g., main(), strrev(), strcat()).
A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function problem. A function is also known as a module.
Functions support modular programming, an approach to solving complex problems.

Responsive image
Advantage of function
  • It facilitates top-down moduler programming as shown in above figure.In this programming styles,the high level logic of the overall problem is saved first while the details of each level function are addressed latter.
  • The length of source program can be reduced by using function at appropriate places. This factor is particularly critical with micro computer where memory space is limited.
  • It is easy to locate and isolate a fault function are further investigate.

Moduler Programming

Moduler Programming is a strategy applied to the design and development of the software system.It is design and development of the software system.It is defined as organizing a large program into small.independent rogram segment called modules.It is basically a 'divide and conquer' approach to problem solving.

Function Types

There are two type of function
  • Library function.
  • User define function

Library Function

In C Programming ,A Function is Standard Library Function if it is system define and we can able to use without any code only use function name.

Example
  scanf(),printf(),strrev(),strcat() etc.

User define function

A user defined function is a programmed routine that has its parameters set by the user of the system. User defined functions often are seen as programming shortcuts as they define functions that perform specific tasks within a larger system.

Example
/* User defined function for addition */
void add(int a, int b)
{
    return a + b;
}
void add(int a,int b);

It is a simple function prototype also function declaration.
In a programming an identifier must be declear first before using in the program,in the same way, A function prototype must be decleared frist before using(calling) in the program.

A function prototype tell three things
  • Function Name : Function name is important part of any function because it is used to call function.
  • Number of arguments and their types : Number of variable or value pass in the function
  • Return types of the function : When we call the function tthen we declear function is return some value or not.

Syntax of Function

return_type function_name(formal arguments or actual arguments)
{
    local declarations or global declarations;
    return statement;
}
Example
#include<stdio.h>
int sum(int x, int y);
int sum(int x, int y)
{
    int z;
    z = x + y;
    return z;
}
int main()
{
    int x, y, add;
    printf("Enter the first value=");
    scanf("%d", &x);
    printf("Enter the second value=");
    scanf("%d", &y);
    add = sum(x, y);
    printf("%d", add);
    return 0;
}

Here,
function Name=sum.
return type=int.
parameters: x and y.