Table of contents
Introduction To Functions
In c programming, functions in C is a group of statements that perform some specific task. Functions are a self-contained subprogram that is used to perform some specific and well-def
ined tasks. As we know that all programmers want to write the codes as simple as possible. That’s why programmers break the whole program into various segments as per their requirements. So that it becomes easy and enhances the readability of the program. In c programming language the program starts execution in the main () function. Bulks of sub-programs lead to inconsistency and confusion in performance. The programmer should separate the programs according to their use if it is needed. It is a very good concept in c programming as it establishes the clear-cut program without any misunderstanding. It is a good practice if you apply the function in your coding. But you need to use a function prototype, function definition, and function call in c programming.Advantages of Using Function:
- Generally, the difficult problem is segmented into small pieces of programs and then solved. This method is implemented in C through functions. A program can be divided into functions, each of which does a specific task.
- When the specific code is to be used more than once and at different places in the program, the use of functions avoids repetitions of the code.
- The program becomes easily understandable, modifiable, and easy to debug and test. It becomes simple to write the code and understand what work is done by the program.
- Functions can be stored in the library and reusability can be achieved.
In c language the functions are di vided into two categories such as:
- Library functions
- User-defined functions

Library functions:
C programming has the facility to provide library functions for doing some tasks. These functions are in-built in C library. These functions are called also “predefined functions”. So, the programmer may not need to worry about certain essential functions become all methods are necessary predefined in the library.
Example:
sqrt () is a mathematical library function that is implemented to find out the square- root of a number. The scanf() and printf() functions are defined in the input-output library. Similarly, we have the strlen(),strcmp(),strcpy() etc for string manipulations.We can not access the library functions directly. To use library functions, we have to include the header file using the preprocessor directive (#include<>). For example, to use input-output library functions in c, we have to include stdio.h, for math library math.h is used, and so on.
Example:program to find the square root if a number
#include<stdio.h>
#include<math.h>
int main()
{
Double n,s;
Printf(“enter the number \n”);
scanf(“%lf”, &n);
s=sqrt(n);
printf(“the square root of %.2lf is : %.2lf \n ”,n,s);
}
Output:
Enter the number
25
The square root of 25 is 5
User-defined Functions in C
The users can create their own functions according to their use in the program for performing any specific task of the program. So, these types of functions are called user-defined functions. Before we proceed further, we should know the aspects of it.
How To Write a Function in C
A function is nothing but a group of statements that perform a specific task. The functions are in C programming, at least there will be a single function to start is called the main(). But to write software that can perform a complete job, a developer needs to convert total requirement into multiple sub-module. Each module can have a small function and that will be responsible for that particular task.
Three things should be kept in mind while creating user-defined functions. They are
- Function declaration
- Function definition
- Function call
You can look at the below example of a program to find the sum of 10 natural numbers where how a function is declared, defined, and called.
#include<stdio.h>
Void sum(void); // function declaration
int main()
{
Sum(); //function call
}
Void sum() //function definition
{
int i,s=0;
for(i=1;i<=10;i++)
{
s=s+i;
}
Printf(“the sum of the first 10 numbers is ”,s);
}
Output:
The sum of first 10 numbers is 55
Example2, program to find the sum of two numbers
#include<stdio.h>
Void sum(int x, int y); // function declaration
int main()
{
int a=100,b=200;
Sum(a,b); //function call
}
Void sum(int x, int y) //function definition
{
int s=0;
s=x+y;
Printf(“the sum of a=%d and b=%d is ”,x,y,s);
}
Output:
a=100,b=200
the sum of a=100 and b=200 is 300
Functions Declaration in C
To write any function, a developer needs to declare it first. A function declaration informs the compiler about a function’s name, return type, arguments, and parameters. So that it will help the compiler in the compilation process for adding the address details to execute and pass the data at run time. Generally, the function declaration is the existing identity of the called function as the calling function needs the information about the called function. Some times it is not needed because if it is placed before the calling function. Most of the time in C main () function remains a calling function. The function declaration is also called a function prototype in c.
Example: program to find difference of two numbers
#include<stdio.h>
int diff(int x, int y)
{
int s;
s=x+y;
return s;
}
main ()
{
int a,b,c;
printf(“Enter the two numbers \n”);
scanf(“%d %d”,&a,&b);
c=diff(a,b);
printf(“the difference of %d and %d is %d”,a,b,c);
}
Here the definition of diff is mentioned before the main (), so main () knows everything about the called function. But generally, the main () is placed at the top and user-defined functions are placed after it. The function declaration is known as the function prototype. It informs the compiler about:
- Name of the function
- Number and type of parameter received by the function
- Type of value returned by function after execution

Functions Definition in C
The function definition comprises the whole description and code of the function. The structure tells what the function is doing and what are its inputs and outputs. As we know function definition has two parts -a function header and a function body.
Statements;Syntax: Return type function_name(type1 arg1,type2 arg2,type3 arg3)
{
Local variable declaratons;
Statements;
..............
return(expression);
}

The first in the function definition is called the function header and the body of the functions is written enclosed in curly braces. The return type decides the type of value that will be returned by the function. The return type is optional, it is assumed to be int by default.so a function can return either one value or no value. Return type void is written in case, the function does not return any value.
Function_ name tells the name of the function and can be of any C identifier. The arguments are declared in the parenthesis, which mentions the type of arguments. These arguments are known as formal arguments and used to accept the value. The function may take any number of arguments or even no arguments at all.
The body of the function is generally a block of statements and variables followed by an optional return statement. The variable declared inside that function is local to that function. That variable can not be assessed in other functions. The function definition can be placed anywhere in the program. But it is generally placed after the main () function. The formal variable is also used as a local variable in the function.
Functions call in C
As we know function definition tells what a function can do, but to actually implement the function in the program it should be called somewhere in the program. The function is called by writing the function name followed by the arguments if to be passed.
Syntax:
Func_name(arg1,arg2,arg3);
The arguments passed as parameters are called actual parameters or arguments. Here, the functions known as called function. Similarly, the function in which the called function is called is known as calling function. For example, main () is calling the function, sum () is the called function and k, n are actual arguments.
S=sum(k,n);
When the function is called, the control goes to the called function and executes the statements inside the function and after that, the result is returned to the calling function if either return type is mentioned or executes the result inside the function. The following figure shows the transfer of control when three functions are called from main ().

In case the function returns any value, then this function call can be used as an operand in any expression anywhere in the program. For example,
s=sum(k,n); //assigning the return value of sum () to a variable s
if the function is declared as void then it can not be used in any expressions because it does not return any value. For example
bathroom (); // does not return any value to the calling function here, the statements are executed inside the called function. But does not return any value to the function.
Point to remember: we can not place the function on the left hand of an assignment statement. func (x,y)=h; // Invalid
How to use a function in C Language
The use of function in c generally depends on a programmer. If a software developer understood all the above-explained points, I think it is sufficient to write any function. But for a real professional developer should have some deep knowledge about the use of function in a different way. To write a smart program in c, you need to know the below defined points.
Static Function in C
I hope we all know about the variable declaration with the storage class facility. By which we can give protection to a variable. So that any variable access can be limited within a file or whole project. Like this, a function also can be defined as a static or non-static storage class. If a function is only used in a single file, then you can declare it as a static function or else keep it as it is. You can look at the below example for how to declare and define a static function.
//Declaration of a static function
static int adder(int number1, int number2);
//Definition of a static function
static int adder(int number1, int number2)
{
int result = 0;
result = number1 + number2;
return result;
}
Related Article
Discover more from PiEmbSysTech
Subscribe to get the latest posts sent to your email.