Unit 5
Unit 5
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
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
Example
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. The below
example serves as both a function definition and a declaration.
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.
In the below example, the first sum function is called and 10,30 are passed to the sum function.
After the function call sum of a and b is returned and control is also returned back to the main
function of the program.
Types of Functions
There are two types of functions in C:
1. Library Functions
2. User Defined Functions
1. Library 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.
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.
Passing Parameters to Functions
The data passed when the function is being invoked is known as the Actual parameters. In the
below program, 10 and 30 are known as actual parameters. Formal Parameters are the variable
and the data type as mentioned in the function declaration. In the below program, a and b are
known as formal parameters.
1. Pass by Value
2. Pass by Reference
1. Pass by Value
Parameter passing in this method copies values from actual parameters into formal function
parameters. As a result, any changes made inside the functions do not reflect in the caller’s
parameters.
Example:
C
// of call by value
#include <stdio.h>
var1 = var2;
var2 = temp;
// Driver code
int main()
{
int var1 = 3, var2 = 2;
var1, var2);
swap(var1, var2);
var1, var2);
return 0;
Output
2. Pass by Reference
The caller’s actual parameters and the function’s actual parameters refer to the same locations,
so any changes made inside the function are reflected in the caller’s actual parameters.
Example:
C
#include <stdio.h>
*var1 = *var2;
*var2 = temp;
// Driver code
int main()
swap(&var1, &var2);
var1, var2);
return 0;
Output
Advantages of Functions in C
Functions in C is a highly useful feature of C with many advantages as mentioned below:
1. The function can reduce the repetition of the same statements in the program.
2. The function makes code readable by providing modularity to our program.
3. There is no fixed number of calling functions it can be called as many times as you want.
4. The function reduces the size of the program.
5. 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:
In C, there are various general problems which requires passing more than one variable of the
same type to a function. For example, consider a function which sorts the 10 elements in
ascending order. Such a function requires 10 numbers to be passed as the actual parameters from
the main function. Here, instead of declaring 10 different numbers and then passing into the
function, we can declare and initialize an array and pass that into the function. This will resolve
all the complexity since the function will now work for any number of values.
functionname(arrayname);//passing array
There are 3 ways to declare the function which is intended to receive an array as an argument.
First way:
Second way:
Third way:
int main(){
int i=0,min=0;
int numbers[]={4,5,7,3,8,9};//declaration of array
Output
minimum number is 3