FUNCTIONS
A function is a block of code which only runs when it is called.
You can pass data, known as parameters, into a function.
Functions are used to perform certain actions, and they are important for
reusing code: Define the code once, and use it many times.
A function in C is a set of statements that when called perform some specific task.
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. They are also
called subroutines or procedures in other languages.
Syntax of Functions in C
The syntax of function can be divided into 3 aspects:
1. Function Declaration
2. Function Definition
3. Function Calls
1) Function Declaration:
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);
Note: The parameter name is not mandatory while declaring functions. We
can also declare the function without using the name of the data variables.
Example:
int speed(int distance, int time);
int speed(int , int);
Page 1 of 10
Note: A function in C must always be declared globally before calling it.
2) Function Definition:
The function definition consists of actual statements which are executed when
the function is called (i.e. when the program control comes to the function).
A C function is generally defined and declared in a single step because the
function definition always starts with the function declaration so we do not
need to declare it explicitly.
Syntax:
return_type function_name (para1_type para1_name, para2_type
para2_name)
{
// body of the function
}
3) 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.
Example:
// 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
Page 2 of 10
int add = sum(10, 30);
printf("Sum is: %d", add);
return 0;
}
___________________________
Output
Sum is: 40
Function Return Type:
Function return type tells what type of value is returned after all function is
executed. When we don’t want to return a value, we can use the void data type.
Example:
int func(parameter_1, parameter_2);
The above function will return an integer value after running statements inside
the function.
Note: Only one value can be returned from a C function. To return multiple
values, we have to use pointers or structures.
Function Arguments:
Function Arguments (also known as Function Parameters) are the data that is
passed to a function.
Example:
int function_name(int var1, int var2);
Page 3 of 10
Conditions of Return Types and Arguments:
In C programming language, functions can be called either with or without
arguments and might return values. They may or might not return values to the
calling functions.
Function with no arguments and no return value
Function with no arguments and with return value
Function with argument and with no return value
Function with arguments and with return value
Types of Functions:
There are two types of functions in C:
a) Library Functions,
b) User Defined Functions
a) Library Function:
A library function is also referred to as a “built-in function”. A compiler
package already exists that contains these functions, each of which has a
specific meaning and is included in the package. Built-in functions have the
advantage of being directly usable without being defined, whereas user-
defined functions must be declared and defined before being used.
For Example:
pow(), sqrt(), strcmp(), strcpy() etc.
Advantages of C library functions:
C Library functions are easy to use and optimized for better
performance.
C library functions save a lot of time i.e, function development time.
C library functions are convenient as they always work.
Page 4 of 10
Example:
// C program to implement the above approach
#include <math.h>
#include <stdio.h>
// Driver code
int main()
{
double Number;
Number = 49;
// Computing the square root with the help of predefined C library function
double squareRoot = sqrt(Number);
printf("The Square root of %.2lf = %.2lf", Number, squareRoot);
return 0;
}
_________________
Output
The Square root of 49.00 = 7.00
b) User Defined Function:
Functions that the programmer creates are known as User-Defined functions
or “tailor-made functions”. User-defined functions can be improved and
modified according to the need of the programmer. Whenever we write a
function that is case-specific and is not defined in any header file, we need to
declare and define our own functions according to the syntax.
Page 5 of 10
Advantages of User-Defined Functions:
Changeable functions can be modified as per need.
The Code of these functions is reusable in other programs.
These functions are easy to understand, debug and maintain.
Example:
// C program to show user-defined functions
#include <stdio.h>
int sum(int a, int b)
{
return a + b;
}
// Driver code
int main()
{
int a = 30, b = 40;
// function call
int res = sum(a, b);
printf("Sum is: %d", res);
return 0;
}
______________
Page 6 of 10
Output
Sum is: 70
Passing Parameters to Functions:
The data passed when the function is being invoked is known as the Actual
parameters.
Formal Parameters are the variable and the data type as mentioned in the
function declaration.
When calling a function in C, arguments can be passed in two ways, by calling
by value and by calling by reference.
i) Call by Value:
The actual parameter is passed to a function.
A new memory area created for the given parameters can be used
only within the function.
The actual parameters cannot be modified here.
Example:
// C program to show use of call by value
#include <stdio.h>
void swap(int var1, int var2)
{
int temp = var1;
var1 = var2;
var2 = temp;
}
// Driver code
Page 7 of 10
int main()
{
int var1 = 3, var2 = 2;
printf("Before swap Value of var1 and var2 is: %d, %d\n", var1, var2);
swap(var1, var2);
printf("After swap Value of var1 and var2 is: %d, %d", var1, var2);
return 0;
}
____________
Output
Before swap Value of var1 and var2 is: 3, 2
After swap Value of var1 and var2 is: 3, 2
ii) Call by Reference :
Instead of copying a variable, a memory address is passed to
function as a parameter.
Address operator (&) is used in the parameter of the called
function.
Changes in function reflect the change of the original variables
(actual paramenters).
Example:
// C program to show use of call by Reference
#include <stdio.h>
void swap(int *var1, int *var2)
{
int temp = *var1;
*var1 = *var2;
*var2 = temp;
Page 8 of 10
}
// Driver code
int main()
{
int var1 = 3, var2 = 2;
printf("Before swap Value of var1 and var2 is: %d, %d\n", var1, var2);
swap(&var1, &var2);
printf("After swap Value of var1 and var2 is: %d, %d", var1, var2);
return 0;
}
_________
Output:
Before swap Value of var1 and var2 is: 3, 2
After swap Value of var1 and var2 is: 2, 3
Page 9 of 10
Advantages of Functions in C:
Functions in C is a highly useful feature of C with many advantages as mentioned
below:
The function can reduce the repetition of the same statements in the program.
The function makes code readable by providing modularity to our program.
There is no fixed number of calling functions it can be called as many times
as you want.
The function reduces the size of the program.
Once the function is declared you can just use it without thinking about the
internal working of the function.
Disadvantages of Functions in C:
The following are the major disadvantages of functions in C:
Cannot return multiple values.
Memory and time overhead due to stack frame allocation and transfer of
program control.
Sources:
i) https://www.geeksforgeeks.org/c-functions/
ii) https://www.w3schools.in/c-programming/function-arguments
iii) https://www.w3schools.com/c/c_functions.php
Page 10 of 10