SlideShare a Scribd company logo
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));
}

More Related Content

PPTX
Stacks IN DATA STRUCTURES
DOC
Functions
PDF
BCA DATA STRUCTURES ALGORITHMS AND PRELIMINARIES MRS SOWMYA JYOTHI
PPTX
Fda unit 1 lec 1
PPT
User defined functions in C programmig
PPT
Ch4 functions
PPTX
Functions in c language
PPTX
User defined functions
Stacks IN DATA STRUCTURES
Functions
BCA DATA STRUCTURES ALGORITHMS AND PRELIMINARIES MRS SOWMYA JYOTHI
Fda unit 1 lec 1
User defined functions in C programmig
Ch4 functions
Functions in c language
User defined functions

What's hot (20)

PPTX
Presentation on Function in C Programming
PPTX
Functions in c
PDF
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
PPTX
Recursive Function
PDF
User_Defined_Functions_ppt_slideshare.
PDF
Programming in c by pkv
PDF
User Defined Functions in C Language
PPTX
Functions in C
PPT
Functions
PDF
Cp module 2
PDF
cp Module4(1)
PPTX
Unit 7. Functions
PPTX
Control Structures in C
PPTX
Programming in java basics
PDF
Programming Fundamentals Functions in C and types
PPT
Prsentation on functions
PDF
C language for Semester Exams for Engineers
PDF
Unit iii
Presentation on Function in C Programming
Functions in c
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
Recursive Function
User_Defined_Functions_ppt_slideshare.
Programming in c by pkv
User Defined Functions in C Language
Functions in C
Functions
Cp module 2
cp Module4(1)
Unit 7. Functions
Control Structures in C
Programming in java basics
Programming Fundamentals Functions in C and types
Prsentation on functions
C language for Semester Exams for Engineers
Unit iii
Ad

Similar to Functions in c mrs.sowmya jyothi (20)

PDF
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
PPTX
User Defined Functionscccccccccccccccccccccccccc.pptx
PPT
arrays.ppt
PPTX
Functions and structure in programming c
PPTX
The function contains the set of programming statements enclosed by {}  when ...
PDF
PSPC-UNIT-4.pdf
PPTX
Unit-III.pptx
PPTX
Chapter One Function.pptx
PDF
Functions-Computer programming
PPTX
FUNCTIONS IN C.pptx
PPTX
FUNCTIONS IN C.pptx
PPT
Lecture 11 - Functions
PPTX
Lecture 1_Functions in C.pptx
PDF
All chapters C++ - Copy.pdfyttttttttttttttttttttttttttttt
PPTX
DOCX
Functions assignment
PPTX
FUNCTIONengineeringtechnologyslidesh.pptx
PPTX
FUNCTION.pptxfkrdutytrtttrrtttttttttttttt
PDF
Programming in C Functions PPT Presentation.pdf
PPTX
user defined functions in c language
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
User Defined Functionscccccccccccccccccccccccccc.pptx
arrays.ppt
Functions and structure in programming c
The function contains the set of programming statements enclosed by {}  when ...
PSPC-UNIT-4.pdf
Unit-III.pptx
Chapter One Function.pptx
Functions-Computer programming
FUNCTIONS IN C.pptx
FUNCTIONS IN C.pptx
Lecture 11 - Functions
Lecture 1_Functions in C.pptx
All chapters C++ - Copy.pdfyttttttttttttttttttttttttttttt
Functions assignment
FUNCTIONengineeringtechnologyslidesh.pptx
FUNCTION.pptxfkrdutytrtttrrtttttttttttttt
Programming in C Functions PPT Presentation.pdf
user defined functions in c language
Ad

More from Sowmya Jyothi (19)

PDF
Strings in c mrs.sowmya jyothi
PDF
Arrays in c unit iii chapter 1 mrs.sowmya jyothi
PDF
Bca data structures linked list mrs.sowmya jyothi
PDF
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
PDF
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
PDF
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
PDF
HARDWARE AND PC MAINTENANCE -THE COMPLETE PC-MRS SOWMYA JYOTHI REFERENCE-MIKE...
PDF
Unit II chapter 4 Loops in C
PDF
Unit ii chapter 2 Decision making and Branching in C
PDF
Unit ii chapter 1 operator and expressions in c
PPTX
Overview of C Mrs Sowmya Jyothi
PPT
NETWORK AND DATABASE CONCEPTS UNIT 1 CHAPTER 2 MRS.SOWMYA JYOTHI
PPT
Introduction to computers MRS. SOWMYA JYOTHI
PPT
Introduction to graphics
PDF
Inter Process Communication PPT
PDF
Internal representation of file chapter 4 Sowmya Jyothi
PDF
Buffer cache unix ppt Mrs.Sowmya Jyothi
PDF
Introduction to the Kernel Chapter 2 Mrs.Sowmya Jyothi
PPT
Introduction to Unix operating system Chapter 1-PPT Mrs.Sowmya Jyothi
Strings in c mrs.sowmya jyothi
Arrays in c unit iii chapter 1 mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothi
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
HARDWARE AND PC MAINTENANCE -THE COMPLETE PC-MRS SOWMYA JYOTHI REFERENCE-MIKE...
Unit II chapter 4 Loops in C
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 1 operator and expressions in c
Overview of C Mrs Sowmya Jyothi
NETWORK AND DATABASE CONCEPTS UNIT 1 CHAPTER 2 MRS.SOWMYA JYOTHI
Introduction to computers MRS. SOWMYA JYOTHI
Introduction to graphics
Inter Process Communication PPT
Internal representation of file chapter 4 Sowmya Jyothi
Buffer cache unix ppt Mrs.Sowmya Jyothi
Introduction to the Kernel Chapter 2 Mrs.Sowmya Jyothi
Introduction to Unix operating system Chapter 1-PPT Mrs.Sowmya Jyothi

Recently uploaded (20)

PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
Classroom Observation Tools for Teachers
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Computing-Curriculum for Schools in Ghana
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Sports Quiz easy sports quiz sports quiz
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Lesson notes of climatology university.
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
TR - Agricultural Crops Production NC III.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Classroom Observation Tools for Teachers
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Insiders guide to clinical Medicine.pdf
Renaissance Architecture: A Journey from Faith to Humanism
Computing-Curriculum for Schools in Ghana
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
FourierSeries-QuestionsWithAnswers(Part-A).pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
Sports Quiz easy sports quiz sports quiz
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
VCE English Exam - Section C Student Revision Booklet
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Lesson notes of climatology university.
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf

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 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)); }