1BCA 4 Unit-6 Function - 06
1BCA 4 Unit-6 Function - 06
1BCA 4 Unit-6 Function - 06
Introduction:-
We can divide up our code into separate functions. How we divide up our code
among different functions is up to us, but logically the division is such that each
function performs a specific task.
A function declaration tells the compiler about a function's name, return type,
and parameters. A function definition provides the actual body of the function.
The C standard library provides numerous built-in functions that our program can
call. For example, strcat() to concatenate two strings, memcpy() to copy one
memory location to another location, and many more functions.
A function can also be referred as a method or a sub-routine or a procedure, etc.
Defining a Function:-
For Example:-
Given below is the source code for a function called max(). This function takes two
parameters num1 and num2 and returns the maximum value between the two −
/* function returning the max between two numbers */
int max(int num1, int num2)
{
/* local variable declaration */
int result;
return result;
}
Function Declarations:-
A function declaration tells the compiler about a function name and how to call the
function. The actual body of the function can be defined separately.
A function declaration has the following parts:-
return_type function_name( parameter list );
For the above defined function max(), the function declaration is as follows:-
int max(int num1, int num2);
Parameter names are not important in function declaration only their type is required, so
the following is also a valid declaration:-
int max(int, int);
Function declaration is required when we define a function in one source file and we call
that function in another file. In such case, we should declare the function at the top of the
file calling the function.
While creating a C function, we give a definition of what the function has to do. To use
a function, we will have to call that function to perform the defined task.
When a program calls a function, the program control is transferred to the called
function. A called function performs a defined task and when its return statement is
executed or when its function-ending closing brace is reached, it returns the program
control back to the main program.
To call a function, we simply need to pass the required parameters along with the
function name, and if the function returns a value, then we can store the returned value.
For example :-
#include <stdio.h>
/* function declaration */
int max(int num1, int num2);
int main ()
{
/* local variable definition */
int a = 100;
int b = 200;
int ret;
If a function is to use arguments, it must declare variables that accept the values of the
arguments. These variables are called the formal parameters of the function.
Formal parameters behave like other local variables inside the function and are created
upon entry into the function and destroyed upon exit.
While calling a function, there are two ways in which arguments can be passed to a
function:-
1. Call by value (sending the values of the arguments) :-
Instead of passing the value of a variable, we can pass the location number (also
called address) of the variable to a function, this method is called ‘call by
reference’.
In this method, the addresses of an actual arguments in the calling function are
copied into the formal arguments of the called function. Inside the function, the
address is used to access the actual argument used in the call. This means that
changes made to the parameter affect the argument.
For Example:-
#include <stdio.h>
void swap( int *, int * );
void main()
{
Library Function:-
As you know, there are two types of functions in C. They are: library functions and
user defined functions.
Library functions are inbuilt functions which are available in common place called
C library. Where as, User defined functions are the functions which are written by
us for our own requirement.
Normal header files are saved as “file_name.h” in which all library functions are
available. These header files contain source code and this source code is added in
main C program file where we add this header file using “#include <file_name.h>”
command.
We can add user defined function to the library. It makes sense in doing so as the
functions that are to be added to the library are first compiled and then added. When
we use these functions (by calling them), we save on their compilation time as they
are available in the library in the compiled form.
Let us now see how to add user defined functions to the library. Different compilers
provide different utilities to add/delete/modify functions in the standard library. For
example C compilers provide a utility called ‘tlib.exe’ (Turbo Librarian). Let us use
this utility to add a function factorial() to the library.
Given below are the steps to do so:-
a) Write the function definition of factorial( ), say ‘fact.c’
{
int i, f = 1;
for ( i = 1; I <= num; i++ )
f = f * i;
return ( f );
}
b) Compile the ‘fact.c’ file using Alt F9. A new file called ‘fact.obj’ will get
created.
c) Add the function to the library ‘maths.lib’ by issuing the command
c:\>tlib maths.lib + c:\fact.obj
d) Declare the prototype of the factorial( ) function in the header file, say
‘fact.h’. This file should be included while calling the function.
e) To use the function present inside the library, we will create a program as:-
#include “c:\fact.h”
#include <stdio.h>
int main( )
{
int f;
f = factorial ( 5 );
printf( “%d”\n”, f);
return 0;
}
R.B.Singh, HOD, Deptt. of BCA, A.S.College, Bikramganj Page 6
f) Compile and execute the program using Ctrl F9.