{}
. Often referred to as subroutines or procedures, functions in C can be broken down into three parts: declaration, definition, and calling. The function declaration specifies the function’s name, return type, and parameters, while the definition contains the executable code. By using functions, C programs become more organized, readable, and efficient, enabling better code reusability and structure.Functions in c is used to perform certain actions.
1.It is the basic building block for c that performs specific actions like modularity and reusability.
2.It is always enclosed with {}(curly braces) performs certain actions and operations
3.They are also called subroutines or procedures.
The syntax of function can be divided into three parts:
In a function declaration,
1.The function name,
2.Its return type,
3.Number and type of its parameters.
Syntax:
return_type name_of_function (parameter_1, parameter_2);
Example of Function Declarations:
int add(int a, int b);
int add(int , int);
int –returnType of function
add –name of the function
parameters int a;
The function definition contains the actual statements that are executed when the function is called. In C, a function is typically defined and declared in a single step because the definition always begins with the function declaration, eliminating the need for a separate declaration.
return_type function_name (para1_type para1_name, para2_type para2_name)
{// body of the function
}
sample example how this function definition can be written is
—————————————————————————————
int add(int a,int b )
{
int c= a+b;
return c;
}
int mul(int a,int b)
{
int c=a*b;
return c;
}
Function call:
The main function is to call the function present somewhere else in the code
Example of the function call
// C program to show function
// call and definition
#include <stdio.h>
// Function that takes two parameters
// their sum
int add(int a, int b)
{
return a + b;
}
// Driver code
int main()
{
// Calling sum function and
int addtion = add(10, 30);
printf(“Sum is: %d”, addition);
return 0;
}
1.with no arguments and no return value
2.with no arguments and with return value
3.with argument and with no return value
4. with arguments and with return value
1.with no arguments (no return value )
Example1:
Void add()
{
Int a,b,c=0;
c=a+b
}
2.with no arguments and no return value
Example 2:
int add()
{
int a,b,c=0;
c=a+b;
return c;
}
3.with arguments (with no return value)
void add(int a,int b)
{
int c;
c=a+b;
}
4. with arguments (with return value)
int add(int a,int b)
{
int c
c=a+b;
return c
}
The function definition contains the actual implementation of the function, including the code that is executed when the function is invoked.
return_type function_name(parameter_type1 parameter1, parameter_type2 parameter2, …);
For example:
int add(int a, int b);
The function definition contains the actual implementation of the function, including the code that is executed when the function is invoked.
return_type function_name(parameter_type1 parameter1, parameter_type2 parameter2, …) { // Code to execute return value; // Optional: return a value based on the function’s return type}
For example:
int add(int a, int b) { return a + b;}
Calling a function is an instruction to the compiler to execute the code within that function.
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(3, 4);
printf(“Result: %d\n”, result); // Output: Result: 7
return 0;
}
If the function has a return type (other than void), it should return a value of the specified type using the return statement. The return statement terminates the function and passes the result back to the caller.
For example, in the add function:
return a + b;
Indian Institute of Embedded Systems – IIES