C Functions
Functions
• A function in C is a set of statements that
when called perform some specific tasks. It is
the basic building block of a C program that
provides modularity and code reusability. The
programming statements of a function are
enclosed within { } braces, having certain
meanings and performing certain operations
Syntax of Functions in C
• Syntax of Functions in C
• The syntax of function can be divided into 3
aspects:
• Function Declaration
• Function Definition
• Function Cal
Function Declarations
• In a function declaration, we must provide the function
name, its return type, and the number and type of its
parameters. A function declaration tells the compiler
that there is a function with the given name defined
somewhere else in the program.
Syntax
return_type name_of_the_function (parameter_1, parameter_2);
Example
int sum(int a, int b); // Function declaration with parameter
names
int sum(int , int); // Function declaration without parameter
names
Function Definition
• The function definition consists of actual
statements which are executed when the
function is called
return_type function_name (para1_type para1_name, para2_type
para2_name)
{
// body of the function
}
Function Call
A function call is a statement that instructs the
compiler to execute the function. We use the
function name and parameters in the function
call.
// C program to show function
// call and definition
#include <stdio.h>
// Function that takes two parameters
// a and b as inputs and returns
// their sum
int sum(int a, int b)
{
return a + b;
}
// Driver code
int main()
{
// Calling sum function and
// storing its value in add variable
int add = sum(10, 30);
Output
printf("Sum is: %d", add);
return 0; Sum is: 40
}