Functions in C
Functions in C
Contents
Chapter 11: Functions
Introduction using functions, Function definition,
function declaration, function call, return statement, passing parameters
functions, scope of variables, storage classes, recursive functions
Chapter 12: Arrays
Declaration of arrays, Accessing elements of an array,
storing values in array, Operations on array,
Passing arrays to functions, 2D array, operations on 2D array,
2D arrays to functions, multidimensional arrays, application of arrays
Chapter 11:Functions
Introduction:
C enables programmers to break up a program into segments commonly known
as functions
Every function in the program is supposed to perform a well-defined task. Therefore, the
code of one function is completely insulated from the other functions.
Another point is that it is not only the main() function that can call other functions.
A function can call any other function.
Why are functions needed?
The reasons for segmenting a program into functions
• Dividing a program into separate well-defined functions facilitates each function to be written
and tested separately. This simplifies the process of getting the total program to work. Figure
11.3 shows that the main() function calls other functions for dividing the entire code into smaller
sections (or functions). This approach is referred to as the top-down approach.
• Understanding, coding, and testing multiple separate functions is far easier than doing it for
one big function.
• If a big program has to be developed without the use of any function other than the main()
function, then there will be countless lines in the main() function and maintaining this program
will be very difficult. A large program size is a serious issue in micro-computers where memory
space is limited.
• All the libraries in C contain a set of functions that the programmers are free to use in their
programs.
.When a big program is broken into comparatively smaller functions, then different programmers
working on that project can divide the workload by writing different functions.
• Like C libraries, programmers can also write their functions and use them at different points
in the main program or in any other program that needs its functionalities.
Why are functions needed?
Using Functions
While using functions we will be using the following terminologies:
A function f that uses another function g is known as the calling function and
g is known as the called function.
The inputs that a function takes are known as arguments/ parameters.
When a called function returns some result back to the calling function, it is
said to return that result.
The calling function may or may not pass parameters to the called function. If
the called function accepts arguments, the calling function will pass
parameters, else it will not do so.
Function declaration is a declaration statement that identifies a function with
its name, a list of arguments that it accepts, and the type of data it returns.
Function definition consists of a function header that identifies the function,
followed by the body of the function containing the executable code for that
function.
Function declaration/Function prototype
Before using a function, the compiler must know about the number of
parameters and the type of parameters that
1.the function expects to receive
2.the data type of the value that it will return to the calling function.
The general format for declaring a function that accepts some arguments
and returns some value as a result can be given as
return_data_type function_name(data_type variable1, data_type
variable2,...);
A function should have a meaningful name that must specify the task that
the function will perform. The function name is used to call it for
execution in a program.
Function declaration/Function prototype
Every function must have a different name that indicates the particular job
that the function does.
return_data_type specifies the data type of the value that will be returned to
the calling function
data_type variable1, data_type variable2, ...is a list of variables of specified
data types. These variables are passed from the calling function to the called
function.
They are also known as arguments or parameters that the called function
accepts to perform its task. Table 11.1 shows examples of valid function
declarations in C.
Function declaration/Function prototype
Things to remember about function declaration:
• After the declaration of every function, there should be a semicolon: If the semicolon is
missing, the compiler will generate an error message.
The function declaration is global. Therefore, the declared function can be called from any
point in the program.
• Use of argument names in the function declaration statement is optional. So both
declaration statements are the particular job that the function does valid in C.
int func(int, char, float);
int func (int num, char ch, float fnum);
• A function cannot be declared within the body of another function.
• A function having void as its return type cannot return any value.
• A function having void as its parameter list cannot accept any value. So the function
declared as
void print(void);
Or
void print();
does not accept any input/arguments from the calling function.
Function declaration/Function prototype
•If the function declaration does not specify any return type, then
by default, the function returns an integer value. Therefore, when a
function is declared as
sum(int a, int b);
Then the function sum accepts two integer values from the calling
function and in turn returns an integer value to the caller.
Some compilers make it compulsory to declare the function before
its usage while other compilers make it optional. However, it is a
good practice to always declare the function before its use as it
allows error checking on the arguments in the function call.
Function definition
When a function is defined, space is allocated for that function in the memory.
When the function declaration is present before the function call, the compiler can
check if the correct number and type of arguments are used in the function call and
the returned value, if any, is being used reasonably.
Points to Remember While Calling
Functions
The following points are to be kept in mind while calling functions:
Function name and the number and type of arguments in
the function call must be same as given in function declaration and function
header of the function definition.
If parameter passed is less than what is specified to accept, then unmatched
argument will be initialized to some garbage value
Parameter list must be separated with commas
Arguments may be passed in the form of expressions to the called function. In
such cases, arguments are first evaluated and converted to the type of formal
parameter and then the body of the function gets executed.
The parameter list must be separated with commas.
Return statement
The return statement terminates the execution of the called function and
returns control to the calling function. When the return statement is
encountered, the program execution resumes in the calling function at the
point immediately
when the return statement is encountered. A return statement may or may not
return a value o the calling function. The syntax of return statement co be
given as
return ‹expression›;
Here expression is placed in between angular bracket because specifying an
expression is optional. The value or expression, if present, is returned to the
calling function.
However, in case expression is omitted, the return value of the function is
undefined.
Return Statement
The expression, if present, is converted to the type returned by the function. A
function that has void as it return type cannot return any value to the calling
function.
So in a function that has been declared with return type void, a return
statement containing an expression generates a warning and the expression is
not evaluated.
For functions that have no return statement, the control automatically returns
to the calling function after the last statement of the called function is
executed. In other words an implicit return takes place upon execution of the
last statement of the called function, and control automatically returns to the
calling function
Passing parameters functions
When a function is called, the calling function may have to pass some
values to the called function.
There are two ways in which arguments or parameters can be passed
to the called function. They include:
• Call by value in which values of variables are passed by the
calling function to the called function.