#9 Functions
#9 Functions
#9 Functions
By Saurabh Shukla | 2017©mysirg.com
What is Function?
Function is one of the most important topics in C language. It is more about the arrangement of the
code which has many benefits.
return_type is data type (like int, char, float, etc) , which can be anything depending upon
the requirement.
function_name is any name chosen by the programmer. Naming rules are same as applied in
the case of variable names, that is, name is any combination of alphabet, digit or underscore
and cannot start with digit.
Arguments are 0 or more variable declarations.
Function body or block is everything in the pair of curly braces ( { } ).
You cannot write action statements without function block. The following code will not
compile in C language
You must put action statements in a block. Above code is to add two numbers taken from
user through keyboard. Say this is one task of adding two numbers. The correct way to write
code is:
#9 Functions
C Notes Vol-2 by Saurabh Shukla
www.mysirg.com
You can call a function any number of times. You are calling printf two times in add function
Where is printf defined? All predefined functions reside in C library, which is a set of library
files. After compilation of your code, linker associates your function calls to the appropriate
definition of function from the library.
Function declaration is another jargon which is different from function definition and
function call.
Function declaration is also known as function prototype.
It is a single line statement written for the compiler to process.
You declare variables before using them, why? It is because; variable names are new words
to the compiler. You need to inform compiler about the purpose of the new word in
advance. In the same way, function names are also new to the compiler, you should first
declare them.
ANSI stated about C function declarations- “Function declaration is recommended but not
compulsory”. You can use function without actually declaring them, but recommendation is
to always declare explicitly.
#9 Functions
C Notes Vol-2 by Saurabh Shukla
www.mysirg.com
Benefits of function
You can make a big complex program by simply writing whole code in main function, which should
be discouraged, because of many reasons:
Logical breakdown of overall task of the program make it easy to create, maintain and understand.
Following are the benefits of functions.
So far you have understood how a function can be defined, but there will be more to study. How a
function can take arguments? How a function can return a value? Such questions are answered in
this section. On the basis of arguments and return value, functions can be defined in the following
four ways.
In the above program, function add is defined as takes nothing and return something. Empty
parenthesis of function add() indicates that the function is not taking any value. Data type void
before the function name is used to tell compiler that this function is not going to return any value.
#9 Functions
C Notes Vol-2 by Saurabh Shukla
www.mysirg.com
Execution of the program begins with main function. Main function calls add function. Now add
function is executed. After completion of add function, control goes back to main function, exactly
from where the add function was invoked.
Function can use only those variables which are declared in its body. Function cannot access
variables of other function.
To return a value, you have to use return keyword. In this example, there is no need of
return keyword, as it is of nature, returns nothing.
Suppose main function has two variables, x and y, there are some values in the variables. Now you
want to add them (or any other operation using values of x and y). You know that there is a add()
function defined to add two numbers, but the way it was designed in previous example, can add
only values stored in variables, a and b. To accomplish this task
You can replace a and b with x and y respectively, but you are wrong. Function add() can’t
access variables, x and y, as they belongs to main function. You know that function can’t
access variables of another function.
Another option is to copy data of x and y into a and b correspondingly. Yes this is the way.
In the following example, observe how we copy data of x and y of main() function into a and b of
add() function. This is called function call by passing values or simply function call by value.
First line is the declaration of function add(). You can see two times int is written in the
parenthesis, informing compiler about two arguments of type int must be passed on call to
the function add().
In main() function, there are two variables x and y. call to scanf function allows input of two
integers and collected in variables x and y.
#9 Functions
C Notes Vol-2 by Saurabh Shukla
www.mysirg.com
In add(x,y), values of x and y are copied to variables a and b of add() function. The value of x
goes into a and value of y goes into b. Here x and y are called actual arguments. a and b are
called formal arguments.
In the definition of add() function, parenthesis is not blank, therefore it is called takes
something. You can see the declaration of two variables in the parenthesis. They are a and b,
known as formal arguments. Do not declare them again in the function’s body; otherwise it
will be a re-declaration error. You cannot declare formal arguments like this
void add(int,a,b) ; this is wrong
You have to use data type for each variable in the formal arguments.
The number of formal arguments decides exactly how many values need to be supplied
during call of function. Here, since we have declared two variables in the parenthesis, we
can pass exactly two int type values during call of add() function.
It would be a blunder if you think about taking values from user using scanf() for variables a
and b, because you have already received values in a and b.
Another way to define a function is takes nothing, returns something. You already know about takes
nothing, so our more focus is towards returns something.
First line is function declaration. Return type is int, which means the function add is going to
return value of type int. Empty parenthesis shows its takes nothing nature.
In the main function, variable s is declared to hold result which you will be getting on call of
function add()
In the line s=add(), function call will be going to replace by the value getting returned from
the function add().
In the add function, as it is of nature takes nothing, you have to take values from user. So
printf and scanf are present in the code.
Once result is calculated and stored in c, it is returned to the calling function.
#9 Functions
C Notes Vol-2 by Saurabh Shukla
www.mysirg.com
Another way to define a function is takes something and returns something. Since you have
understood takes something in the second way and return something in the third way of defining
functions, now you can apply your brain to understand the following example.
References:
Exercise
#9 Functions
C Notes Vol-2 by Saurabh Shukla
www.mysirg.com