Module : 6
Function
• 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
Types of Functions
• There are two types of functions in C:
1.Library Functions
2.User Defined Functions
• 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.
• Functions that the programmer creates are known as User-Defined 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 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.
➔ 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
Advantages of Functions in C
• 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.
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.
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. 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 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.
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.
• Function Arguments
• Function Arguments (also known as Function Parameters) are the
data that is passed to a function.
How Does C Function Work?
• Declaring a function: Declaring a function is a step where we declare a
function. Here we define the return types and parameters of the
function.
• Defining a function:
• Calling the function: Calling the function is a step where we call the
function by passing the arguments in the function.
• Executing the function: Executing the function is a step where we can
run all the statements inside the function to get the final result.
• Returning a value: Returning a value is the step where the calculated
value after the execution of the function is returned. Exiting the function
is the final step where all the allocated memory to the variables,
functions, etc is destroyed before giving full control to the main function.
Passing Parameters to Functions
• The data passed when the function is being
invoked is known as the Actual parameters.
• In the program, 10 and 30 are known as
actual parameters.
• Formal Parameters are the variable and the
data type as mentioned in the function
declaration. Here a and b are known as
formal parameters.
➔ We can pass arguments to the C function in two ways:
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.
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.