Functions
What is a function?
• When the algorithm of a certain problem involves
long and complex logic,
– It is broken into smaller, independent and
reusable blocks
– These small blocks of code are known by
different names in different programming
languages such as a module, a subroutine,
a function or a method
• A function
– Is a name given to a block of code that
performs a specific task
Need of function
• A function Allows us to write a piece of logic once
and reuse it wherever needed in the program
• Functions enhance modularity, code reusability,
and readability by dividing complex programs
into smaller, manageable parts
• Use of functions help to keep the code clean,
organized, easier to understand and manage
• Every C program has at least one function, which
is main() because
• The main function in C is the entry point of any
C program.
• It is where the execution of the program begins
Types of Functions
• In C programming, functions can be grouped into
two main categories:
– Library Functions
– Use Defined functions
Library Functions
• These are lots of built-in functions provided by C,
such as printf(), scanf(), sqrt(), etc.
• Library functions can be used by including the
appropriate header files, such as #include <stdio.
h>, #include <math.h>, etc.
User-defined Functions
• Based on how input and
output are handled, user-
defined functions can be
further classified into four
different types
• No arguments, no return
value
• The function neither
takes input nor returns
any result
• Arguments, no return
value
– The function takes input
Declaring a Function
• A function declaration tells the compiler about a
function name and how to call the function
Function
• Syntax prototype
return_type function_name (
parameter_list ) ;
• Example
int max ( int num1, int num2 ) ;
Declaring a Function
• Return type
– Specifies the type of value the function will return
– Void is used if the function does not return anything
• Function name
– A unique name that identifies the function
– It follows the same naming rules as variables
• Parameter list
– A set of input values passed to the function
– If the function takes no inputs, this can be left empty or
written as void
• Function body
– The block of code that runs when the function is called
– It is enclosed in curly braces { }
Example of User-defined
Function
#include <stdio.h> int main()
{
int max(int num1, int int x = max( 75, 57 );
num2) printf("The answer is: %d",
{ x);
int result; return 0;
if(num1 > num2) }
result = num1; Output:
else
result = num2; The answer is 75
return result;
}
Calling a Function
• While creating a C function, a definition of the
function should be given i.e., what the function
has to do
• To use a function, you will have to call that
function to perform the defined task
• To call a function properly, you need to comply
with the declaration of the function prototype
• When a function is defined with arguments, the
arguments in front of the function name are
called formal arguments. When a function is
Calling a Function
• When a program calls a function, the program
control is transferred to the called function. A
called function performs a defined task and when
its return statement is executed or when its
function-ending closing brace is reached, it
returns the program control back to the main
program.
Example of User-defined
Function
#include <stdio.h> int max(int num1, int
num2)
Int max(int, int); {
int result;
if(num1 > num2)
int main() result = num1;
{ else
int x = max( 75, 57 ); result = num2;
printf("The answer is: %d", return result;
x); }
return 0;
} Output:
The answer is 75
Parameter Passing to a
Function
• In C, functions can receive arguments in two ways:
– Call by Value and
– Call by Reference
• These methods determine how data is passed to a
function and whether the original data can be
modified
Call by Value Method
• The function works with a copy of the variable
• Changes inside the function do not affect the
original variable
• Safer as the original data is preserved
• Suitable for small data types but inefficient for
large data structures due to memory overhead
Example of Call By Value
#include <stdio.h> int main()
{
int a = 10, b = 20;
void swap(int x, int y)
swap(a, b);
{ printf(“Inside main: a = %d,
int temp = x; b=
x = y; %d\n“, a,
y = temp; b);
printf(“Inside swap: x = %d, y = return 0;
%d\n", }
x,
y);
} Output:
Inside swap: x = 20, y =
10
Inside main : a = 10, b =
Call by Reference Method
• The function works with the actual variable via
its address
• Changes inside the function affect the original
variable
• Efficient for large data structures as no copies are
made
• Risky as unintended modifications can occur
Example of User-defined
Function
#include <stdio.h> int main()
void swap(int *x, int *y) {
{ int a = 10, b = 20;
swap(&a, &b);
int temp = *x;
printf("Inside main: a =
*x = *y; %d, b = %d\n", a, b);
*y = temp; return 0;
printf(“Before: x = %d, y = }
%d\n",
*x,
*y);
} Output:
Inside swap: x = 20, y =
10
Inside main: a = 20, b =
Passing Arrays to Functions in
• In C, arrays are alwaysCpassed to a function by
reference, meaning the function receives a
pointer to the array's first element rather than a
copy of the entire array
• Since the function is working with the original
memory location, any changes made to the array
inside the function will persist outside of it.
• The standard practice is to pass both the array
and its size to the function
Passing Arrays to Functions in
C to a function, what is
• When an array is passed
actually passed is a pointer to the first element of
the array
• This allows the function to directly access and
modify the original array
Example of Passing 1-D Array to
Function
#include <stdio.h> float average(int x[], int len)
{
float average( int arr[], int int sum=0; avg, i;
ln ); for (i = 0; i < len; i++)
{
printf("arr[%d]:%d “, i,
int main() x[i]);
{ sum += x[i];
int x[] = {10, 34, 21, 78, }
5}; avg = (float)sum / len;
float avg = average(x, 5); return avg;
}
printf(“\naverage: %f", Output:
avg); arr[0]: 10 arr[1]: 34 arr[2]: 21
} arr[3]: 78 arr[4]: 5
average: 29.600000
Passing a multi-dimensional
array
• To pass multidimensional arrays to a function,
only the name of the array is passed to the
function (similar to one-dimensional arrays)
Example of Passing 2-D Array to
Function
#include <stdio.h> void print(int num[2][2], int
void print(int num[2][2], int n); n)
{
int main() printf("Displaying: ");
{ for (int i = 0; i < n; ++i)
int num[2][2]; {
printf("Enter 4 numbers: for (int j = 0; j < n; ++j)
\n"); {
for (int i = 0; i < 2; ++i) printf("%d ",
{ num[i][j]);
for (int j = 0; j < 2; ++j) }
{ }
scanf("%d", } Output:
&num[i][j]); Enter 4 numbers: 2 3 4 5
} Displaying: 2 3 4 5
Print(num, 2);