UNIT IV
CHAPTER 1 :
USER DEFINED FUNCTIONS
MRS.SOWMYA JYOTHI
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.
Here function01( ) is an user defined function.
main( )
{
=======
=======
function01( );
=======
}
function01( )
{
========
========
}
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.
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.
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.
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
• 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.
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)
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.
Example :
float mul (float x, float y)
{
….
}
int sum (int a, int b)
{
….
}
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.
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.
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);
}
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);
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.
Parameter list :
The parameter list contains declaration of variables
separated by commas and surrounded by
parentheses.
float quadratic (int a, int b, int c)
{….
}
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);
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.
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;
}
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.
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));
}

Functions in c mrs.sowmya jyothi

  • 1.
    UNIT IV CHAPTER 1: USER DEFINED FUNCTIONS MRS.SOWMYA JYOTHI
  • 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 userdefined 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 oradvantages 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 userdefined 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 functiondefinition 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 functionheader 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 functionbody 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 : •Inorder 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 : Herein 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 : Theprogram 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 parameterlist 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 : Theparameter list contains declaration of variables separated by commas and surrounded by parentheses. float quadratic (int a, int b, int c) {…. }
  • 18.
    Prototype : •The declarationof 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 findsum 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 functionis 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 displayfactorial 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)); }