This document discusses user-defined functions in C programming. It defines user-defined functions as functions created by the user as opposed to library functions. It covers the necessary elements of user-defined functions including function definition, function call, and function declaration. Function definition includes the function header with name, type, and parameters and the function body. Function calls invoke the function. Function declarations notify the program of functions that will be used. The document provides examples and discusses nesting of functions and recursive functions.
2. User defined functions :
ā¢ C functions can be classified into two categories, namely,
library functions and user-defined functions.
ā¢ The functions which are developed by user at the time of
writing a program are called user defined functions.
ā¢ So, user-defined functions are functions created and
developed by user.
3. Here function01( ) is an user defined function.
main( )
{
=======
=======
function01( );
=======
}
function01( )
{
========
========
}
4. Necessity of user defined functions :
ā¢ When not using user defined functions, for a large program
the tasks of debugging, compiling etc may become difficult in
general.
ā¢ Thatās why user defined functions are extremely necessary
for complex programs.
5. The necessities or advantages are as follows,
01. It facilitates top-down modular programming. In this
programming style, the high level logic of the overall problem
is solved first while the details of each lower-level function are
addressed later.
02. The length of a source program can be reduced by using
functions at appropriate places.
03. It is easy to locate and isolate a faulty function for further
investigations.
04. A function may be used by many other programs. This
means that a C programmer can build on what others have
already done, instead of starting all over again from scratch.
6. Multifunction program :
ā¢ A function is a self-contained block of code that performs a
particular task.
ā¢ Once a function has been designed and packed, it can be
treated as a āblack boxā that takes some data from the main
program and returns a value.
ā¢ Thus a program, which has been written using a number of
functions, is treated as a multifunction program.
7. Elements of user defined functions : In order to make use of a
user-defined function, we need to establish three elements
that are related to functions.
1. Function definition
2. Function call
3. Function declaration
8. ā¢ The function definition is an independent program module
that is specially written to implement to the requirements of
the function.
ā¢ In order to use the function we need to invoke it at a
required place in the program. This is known as the
function call.
ā¢ The program or a function that has called a function is
referred to as the calling function or calling program.
ā¢ The calling program should declare any function that is to
be used later in the program. This is known as the function
declaration.
9. Function Definition : The function definition is an independent program
module that is specially written to implement to the requirements of the
function.
ā¢ A function definition, also known as function implementation shall include the
following elements
1. Function name
2. Function type
3. List of parameters
4. Local variable declarations
5. Function statements
6. A return statement
All the six elements are grouped into two parts; namely,
ā¢ Function header (First three elements)
ā¢ Function body (Second three elements)
10. Function Header
The function header consists of three parts; function type, function name and
list of parameter.
(a) Function Type
The function type specifies the type of value (like float or double) that the
function is expected to return to the calling program. If the return type is not
explicitly specified, C will assume that it is an integer type.
(b) Function name
The function name is any valid C identifier and therefore must follow the same
rules of formation as other variable names in C. The name should be
appropriate to the task performed by the function.
(c) List of Parameter
The parameter list declares the variables that will receive the data sent by the
calling program. They serve as input data to the function to carry out the
specified task.
11. Example :
float mul (float x, float y)
{
ā¦.
}
int sum (int a, int b)
{
ā¦.
}
12. Function body
The function body is enclosed in braces, contains three parts,
in the order given below:
1. Local variable declaration : Local variable declarations are
statements that specify the variables needed by the function.
2. Function Statements : Function statements are statements
that perform the task of the function.
3. Return Statements : A return statement is a statement
that returns the value evaluated by the function to the calling
program. If a function does not return any value, one can omit
the return statement.
13. Function call :
ā¢In order to use functions user need to invoke it at a
required place in the program. This is known as the
function call.
ā¢A function can be called by simply using the function
name followed by a list of actual parameters, if any,
enclosed in parentheses.
14. Example : Here in the main() program the mul(10,5)
function has been called.
main()
{
int y;
y=mul(10,5); /*Function Call*
printf(ā%dnā,y);
}
15. Function declaration :
The program or a function that called a function is referred to as the
calling function or calling program. The calling program should declare any
function that is to be used later in the program. This is known as the
function declaration.
A function declaration consists of four parts. They are,
1. Function type
2. Function name
3. Parameter list
4. Terminating semicolon
They are coded in the following format :
ā¢ function_type function_name(parameter list);
16. 1. The parameter list must be separated by commas.
2. If the function has no formal parameters, the list is
written as void.
3. The return type is optional when the function
returns int type data.
4. When the declared type does not match the types
in the function definition compiler will produce an
error.
17. Parameter list :
The parameter list contains declaration of variables
separated by commas and surrounded by
parentheses.
float quadratic (int a, int b, int c)
{ā¦.
}
18. Prototype :
ā¢The declaration of a function is known as function
prototype. The function prototype is coded in the
following format,
ā¢ Function_type function_name(parameter list);
19. Nesting of functions : In C, each function can contain one or more than
one function in it.
There is no limit as to how deeply functions can be nested. Consider the following example,
= = = = = = = = = = =
main()
{
function1();
}
function1()
{
function2();
}
function2()
{
...............;
}
= = = = = = = = = = =
In the above example, The main() function contains function01(), the function01() contains function02
and so on. This is nesting of functions.
20. Program to find sum of 2 numbers using function
#include<stdio.h>
#include<conio.h>
int sum(int a, int b); //function
declaration
void main()
{
int a,b,c;
clrscr();
printf(āEnter 2 numbers:ā);
scanf(ā%d%dā,&a,&b);
c=sum(a,b); //function call
printf(ānSum=%dā,c);
getch();
//function definition
int sum(int a, int b)
{
int c=0;
c=a+b;
return c;
}
21. Recursion:
ā¢ Recursive function is a function that calls itself.
ā¢ When a function calls another function and that second function
calls the third function then this kind of a function is called nesting
of functions.
ā¢ But a recursive function is the function that calls itself repeatedly.
main()
{
printf(āthis is an example of recursive functionā);
main();
}
ā¢ when this program is executed. The line is printed repeatedly and
indefinitely. We might have to abruptly terminate the execution.
22. Program to display factorial of a number using
recursive function
#include<stdio.h>
#include<conio.h>
Long fact(int n);
Void main()
{
int n;
long int f;
clrscr();
printf(āenter a numberā);
scanf(ā%dā, &n);
f=fact(n);
printf(ānFactorial =%ldā),f);
}
long fact(int n)
{
if (n==0)
return 1;
else
return (n*fact(n-1));
}