Introduction to functions
Introduction to functions
Introduction to functions
Functions are important part of code in C. A C program cannot successfully link without the
function main(). A function is C is a named block that is called upon to perform a definite task.
It is provided data to perform the desired operation and it then returns a value to the statement
that called it. C program can be viewed as a collection of independent functions to perform a
logical task of some kind. Function can be called n number of times to perform a similar task,
without having to code for it again and again.
Advantages of functions
1. Code re-usability: The code inside the function can be used N number of times without having
to rewrite the code again and again. The programmer simply needs to place a call to that function
and the compiler takes care of the rest. It reduces the effort of programmer.
2. Lesser Complexity: Large problems can be split into number of smaller and easy to
understand and code functions. The functions can then be called according to requirement.
3. Easy and fast Debugging: Debugging refers to finding logical errors in the program. Using
functions debugging becomes much easier and faster. Errors can be fixed with a lot of ease. It
can get very difficult to find bugs in a single function that is large in size.
4. Recursion: Complex computer science problems can be reduced to almost single line codes
using recursion concept. By recursion we mean when a function calls itself.
5. Improved readability: The program is divided into smaller low level functions reducing the
length of the source program. The functions can then be placed in multiple files, making the
source file easy to understand.
6. Function sharing: A function can be placed in a separate file and then it can be shared by
different program.
Disadvantage of functions
2
Beside all the advantages, there are disadvantages that are associated with the use of
functions. While it adds speed to coding, it slows down the speed of the program execution. The
program during execution has to jump to a function when a call is placed to it and return after
the function completes execution.
Function declaration
Just like any variable, a function should also be declared or prototyped before it can be used.
Compiler should be informed about the existence of a function, i.e. a function of such a name
exists. The function declaration gives the basic structural information about the function being
created to the compiler like, return type, name, type and number of arguments [1]. When a call
to any function is made, it looks to match it with the prototype of that function first. If it does not
find any prototype matching the function that is called, it produces an error. A function can be
declared or prototyped as follows:
Syntax,
For example,
Function call
The program executions starts from the function main(). Rest of the functions cannot execute
on their own. For a function to be executed, a call to that function is required. When a call is
made to the function, compiler looks for the declaration or prototype of a function that matches
the prototype of function called. The program control then jumps to the function definition of the
called function. A called function then executes the piece of code written in its definition and
returns back to the statement that was used to call it. We can also pass certain information
to the function as arguments.
For example,
int a, b, sum;
Function definition
1. Return Type: A function may or may not return a value. In case a function returns a value,
the return_type is the type of value returned by the function. In case the function does not return
a value, the return_type can be specified as void.
2. Function Name: This is the actual name or identifier of the function. More than one function
can also have the same name. so one function is distinguished from the other on the bases of
parameter list.
3. Parameters: A function header consists of parameter list that is used to accept the values
passed during function call. When a function is called, a set of values may be passed to the
parameter. These values are referred to as actual parameters.
4. Function Body: This is where the actual code is written. The function body is block
enclosed using curly braces. The function body contains a collection of statements to perform
the specified task.
Return statement
A function may or may not return a value. The statement used for returning value from a function
is return statement. A function may not return a value, or at most a function can return only one
value. Still, a function can have more than one return statement in the code, but only one of it
can be executed on the basis of a condition. The concept of returning more than one value is
also discussed in this lesson.
#include<conio.h>
#include<stdio.h>
void main()
//accept input
scanf("%d",&var1);
scanf("%d",&var2);
//function call
getch();
//function definition
int rval;
#include<conio.h>
#include<stdio.h>
5
void main()
{
//accept input
scanf("%d",&var1);
//function call
fact = factorial(var1);
getch();
//function definition
//function body
int i, fact=1;
for (i=1;i<=num1;i++)
{
fact = fact * i;
return fact;
The very first program written in this book used functions, even though not created by us. In this
lesson we have also seen how we can create our own functions and how they get executed.
On the basis of these two differentiations, we have 2 types of functions in C.
✓ Library/In-built/standard functions.
✓ User-Defined functions.
Library functions
C language comes with a set of predefined library functions that can be used to directly
achieve some objective without having to code for that. C Library functions are inbuilt
functions in C programming. Function prototypes of these functions are written in their
respective header file. To use these functions, you must include their respective header files.
There are large numbers of library functions available in C programming to help the
programmer to write fast and effective code.
For example, if you want to find the length of the string, you can simply call a library function
called strlen() used to find the length of the string. For using strlen() function the programmer
must include its respective header file in the source program using the preprocessor
#include. The header file for strlen() function is string.h.
User-defined functions
Programmers can also create their own functions in C other than accessing the predefined library
functions. A user must define the following in order to create a user-defined function:
✓ Function prototype
✓ Function call
✓ Function definition
We can create user defined functions of any of the four categories. Let us see the examples for
declaring and calling such functions.
// prototype
//function call
display();
// prototype
//function call
// prototype
int sum(void);
//function call
// prototype
8
//function call