SlideShare a Scribd company logo
1 of 22
Download to read offline
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

What's hot

Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C ProgrammingShuvongkor Barman
Ā 
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM) FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)Mansi Tyagi
Ā 
Recursive Function
Recursive FunctionRecursive Function
Recursive FunctionHarsh Pathak
Ā 
User_Defined_Functions_ppt_slideshare.
User_Defined_Functions_ppt_slideshare.User_Defined_Functions_ppt_slideshare.
User_Defined_Functions_ppt_slideshare.NabeelaNousheen
Ā 
User Defined Functions in C Language
User Defined Functions   in  C LanguageUser Defined Functions   in  C Language
User Defined Functions in C LanguageInfinity Tech Solutions
Ā 
Functions in C
Functions in CFunctions in C
Functions in CKamal Acharya
Ā 
Functions
FunctionsFunctions
FunctionsOnline
Ā 
cp Module4(1)
cp Module4(1)cp Module4(1)
cp Module4(1)Amarjith C K
Ā 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in Csana shaikh
Ā 
Programming in java basics
Programming in java  basicsProgramming in java  basics
Programming in java basicsLovelitJose
Ā 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and typesimtiazalijoono
Ā 
Prsentation on functions
Prsentation on functionsPrsentation on functions
Prsentation on functionsAlisha Korpal
Ā 
C language for Semester Exams for Engineers
C language for Semester Exams for Engineers C language for Semester Exams for Engineers
C language for Semester Exams for Engineers Appili Vamsi Krishna
Ā 

What's hot (20)

Ch13
Ch13Ch13
Ch13
Ā 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
Ā 
Functions in c
Functions in cFunctions in c
Functions in c
Ā 
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM) FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
Ā 
Recursive Function
Recursive FunctionRecursive Function
Recursive Function
Ā 
User_Defined_Functions_ppt_slideshare.
User_Defined_Functions_ppt_slideshare.User_Defined_Functions_ppt_slideshare.
User_Defined_Functions_ppt_slideshare.
Ā 
Programming in c by pkv
Programming in c by pkvProgramming in c by pkv
Programming in c by pkv
Ā 
User Defined Functions in C Language
User Defined Functions   in  C LanguageUser Defined Functions   in  C Language
User Defined Functions in C Language
Ā 
Functions in C
Functions in CFunctions in C
Functions in C
Ā 
Functions
FunctionsFunctions
Functions
Ā 
Cp module 2
Cp module 2Cp module 2
Cp module 2
Ā 
cp Module4(1)
cp Module4(1)cp Module4(1)
cp Module4(1)
Ā 
Unit 7. Functions
Unit 7. FunctionsUnit 7. Functions
Unit 7. Functions
Ā 
Ch06
Ch06Ch06
Ch06
Ā 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
Ā 
Programming in java basics
Programming in java  basicsProgramming in java  basics
Programming in java basics
Ā 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
Ā 
Prsentation on functions
Prsentation on functionsPrsentation on functions
Prsentation on functions
Ā 
C language for Semester Exams for Engineers
C language for Semester Exams for Engineers C language for Semester Exams for Engineers
C language for Semester Exams for Engineers
Ā 
Unit iii
Unit iiiUnit iii
Unit iii
Ā 

Similar to Functions in c mrs.sowmya jyothi

USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdfUSER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdfSowmyaJyothi3
Ā 
Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptxMehul Desai
Ā 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptxmiki304759
Ā 
Functions-Computer programming
Functions-Computer programmingFunctions-Computer programming
Functions-Computer programmingnmahi96
Ā 
FUNCTIONS IN C.pptx
FUNCTIONS IN C.pptxFUNCTIONS IN C.pptx
FUNCTIONS IN C.pptxSKUP1
Ā 
FUNCTIONS IN C.pptx
FUNCTIONS IN C.pptxFUNCTIONS IN C.pptx
FUNCTIONS IN C.pptxLECO9
Ā 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxKhurramKhan173
Ā 
Functions assignment
Functions assignmentFunctions assignment
Functions assignmentAhmad Kamal
Ā 
user defined functions in c language
user defined functions in c languageuser defined functions in c language
user defined functions in c languageDeepRaval7
Ā 
CHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxCHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxGebruGetachew2
Ā 
358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2sumitbardhan
Ā 
User defined function in C.pptx
User defined function in C.pptxUser defined function in C.pptx
User defined function in C.pptxRhishav Poudyal
Ā 

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

USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdfUSER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
Ā 
arrays.ppt
arrays.pptarrays.ppt
arrays.ppt
Ā 
PSPC-UNIT-4.pdf
PSPC-UNIT-4.pdfPSPC-UNIT-4.pdf
PSPC-UNIT-4.pdf
Ā 
Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
Ā 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
Ā 
Functions-Computer programming
Functions-Computer programmingFunctions-Computer programming
Functions-Computer programming
Ā 
FUNCTIONS IN C.pptx
FUNCTIONS IN C.pptxFUNCTIONS IN C.pptx
FUNCTIONS IN C.pptx
Ā 
FUNCTIONS IN C.pptx
FUNCTIONS IN C.pptxFUNCTIONS IN C.pptx
FUNCTIONS IN C.pptx
Ā 
Lecture 11 - Functions
Lecture 11 - FunctionsLecture 11 - Functions
Lecture 11 - Functions
Ā 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
Ā 
Functions
FunctionsFunctions
Functions
Ā 
Functions assignment
Functions assignmentFunctions assignment
Functions assignment
Ā 
user defined functions in c language
user defined functions in c languageuser defined functions in c language
user defined functions in c language
Ā 
CHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxCHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptx
Ā 
Function C programming
Function C programmingFunction C programming
Function C programming
Ā 
358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2
Ā 
FUNCTION CPU
FUNCTION CPUFUNCTION CPU
FUNCTION CPU
Ā 
User defined function in C.pptx
User defined function in C.pptxUser defined function in C.pptx
User defined function in C.pptx
Ā 
C functions list
C functions listC functions list
C functions list
Ā 
Functions
FunctionsFunctions
Functions
Ā 

More from Sowmya Jyothi

Strings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiSowmya Jyothi
Ā 
Arrays in c unit iii chapter 1 mrs.sowmya jyothi
Arrays in c unit iii chapter 1 mrs.sowmya jyothiArrays in c unit iii chapter 1 mrs.sowmya jyothi
Arrays in c unit iii chapter 1 mrs.sowmya jyothiSowmya Jyothi
Ā 
Bca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothiBca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothiSowmya Jyothi
Ā 
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHIBCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHISowmya Jyothi
Ā 
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHIBCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHISowmya Jyothi
Ā 
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHIBCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHISowmya Jyothi
Ā 
HARDWARE AND PC MAINTENANCE -THE COMPLETE PC-MRS SOWMYA JYOTHI REFERENCE-MIKE...
HARDWARE AND PC MAINTENANCE -THE COMPLETE PC-MRS SOWMYA JYOTHI REFERENCE-MIKE...HARDWARE AND PC MAINTENANCE -THE COMPLETE PC-MRS SOWMYA JYOTHI REFERENCE-MIKE...
HARDWARE AND PC MAINTENANCE -THE COMPLETE PC-MRS SOWMYA JYOTHI REFERENCE-MIKE...Sowmya Jyothi
Ā 
Unit II chapter 4 Loops in C
Unit II chapter 4 Loops in CUnit II chapter 4 Loops in C
Unit II chapter 4 Loops in CSowmya Jyothi
Ā 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CSowmya Jyothi
Ā 
Unit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in cUnit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in cSowmya Jyothi
Ā 
Overview of C Mrs Sowmya Jyothi
Overview of C Mrs Sowmya JyothiOverview of C Mrs Sowmya Jyothi
Overview of C Mrs Sowmya JyothiSowmya Jyothi
Ā 
NETWORK AND DATABASE CONCEPTS UNIT 1 CHAPTER 2 MRS.SOWMYA JYOTHI
NETWORK AND DATABASE CONCEPTS UNIT 1 CHAPTER 2 MRS.SOWMYA JYOTHINETWORK AND DATABASE CONCEPTS UNIT 1 CHAPTER 2 MRS.SOWMYA JYOTHI
NETWORK AND DATABASE CONCEPTS UNIT 1 CHAPTER 2 MRS.SOWMYA JYOTHISowmya Jyothi
Ā 
Introduction to computers MRS. SOWMYA JYOTHI
Introduction to computers MRS. SOWMYA JYOTHIIntroduction to computers MRS. SOWMYA JYOTHI
Introduction to computers MRS. SOWMYA JYOTHISowmya Jyothi
Ā 
Introduction to graphics
Introduction to graphicsIntroduction to graphics
Introduction to graphicsSowmya Jyothi
Ā 
Inter Process Communication PPT
Inter Process Communication PPTInter Process Communication PPT
Inter Process Communication PPTSowmya Jyothi
Ā 
Internal representation of file chapter 4 Sowmya Jyothi
Internal representation of file chapter 4 Sowmya JyothiInternal representation of file chapter 4 Sowmya Jyothi
Internal representation of file chapter 4 Sowmya JyothiSowmya Jyothi
Ā 
Buffer cache unix ppt Mrs.Sowmya Jyothi
Buffer cache unix ppt Mrs.Sowmya JyothiBuffer cache unix ppt Mrs.Sowmya Jyothi
Buffer cache unix ppt Mrs.Sowmya JyothiSowmya Jyothi
Ā 
Introduction to the Kernel Chapter 2 Mrs.Sowmya Jyothi
Introduction to the Kernel  Chapter 2 Mrs.Sowmya JyothiIntroduction to the Kernel  Chapter 2 Mrs.Sowmya Jyothi
Introduction to the Kernel Chapter 2 Mrs.Sowmya JyothiSowmya Jyothi
Ā 
Introduction to Unix operating system Chapter 1-PPT Mrs.Sowmya Jyothi
Introduction to Unix operating system Chapter 1-PPT Mrs.Sowmya JyothiIntroduction to Unix operating system Chapter 1-PPT Mrs.Sowmya Jyothi
Introduction to Unix operating system Chapter 1-PPT Mrs.Sowmya JyothiSowmya Jyothi
Ā 

More from Sowmya Jyothi (19)

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

Recently uploaded

call girls in Kamla Market (DELHI) šŸ” >ą¼’9953330565šŸ” genuine Escort Service šŸ”āœ”ļøāœ”ļø
call girls in Kamla Market (DELHI) šŸ” >ą¼’9953330565šŸ” genuine Escort Service šŸ”āœ”ļøāœ”ļøcall girls in Kamla Market (DELHI) šŸ” >ą¼’9953330565šŸ” genuine Escort Service šŸ”āœ”ļøāœ”ļø
call girls in Kamla Market (DELHI) šŸ” >ą¼’9953330565šŸ” genuine Escort Service šŸ”āœ”ļøāœ”ļø9953056974 Low Rate Call Girls In Saket, Delhi NCR
Ā 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
Ā 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
Ā 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
Ā 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
Ā 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
Ā 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
Ā 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
Ā 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
Ā 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)Dr. Mazin Mohamed alkathiri
Ā 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
Ā 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
Ā 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
Ā 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
Ā 
18-04-UA_REPORT_MEDIALITERAŠ”Y_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAŠ”Y_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAŠ”Y_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAŠ”Y_INDEX-DM_23-1-final-eng.pdfssuser54595a
Ā 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
Ā 

Recently uploaded (20)

call girls in Kamla Market (DELHI) šŸ” >ą¼’9953330565šŸ” genuine Escort Service šŸ”āœ”ļøāœ”ļø
call girls in Kamla Market (DELHI) šŸ” >ą¼’9953330565šŸ” genuine Escort Service šŸ”āœ”ļøāœ”ļøcall girls in Kamla Market (DELHI) šŸ” >ą¼’9953330565šŸ” genuine Escort Service šŸ”āœ”ļøāœ”ļø
call girls in Kamla Market (DELHI) šŸ” >ą¼’9953330565šŸ” genuine Escort Service šŸ”āœ”ļøāœ”ļø
Ā 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
Ā 
Model Call Girl in Bikash Puri Delhi reach out to us at šŸ”9953056974šŸ”
Model Call Girl in Bikash Puri  Delhi reach out to us at šŸ”9953056974šŸ”Model Call Girl in Bikash Puri  Delhi reach out to us at šŸ”9953056974šŸ”
Model Call Girl in Bikash Puri Delhi reach out to us at šŸ”9953056974šŸ”
Ā 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
Ā 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
Ā 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
Ā 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
Ā 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
Ā 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
Ā 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
Ā 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
Ā 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
Ā 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
Ā 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
Ā 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
Ā 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
Ā 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Ā 
18-04-UA_REPORT_MEDIALITERAŠ”Y_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAŠ”Y_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAŠ”Y_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAŠ”Y_INDEX-DM_23-1-final-eng.pdf
Ā 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
Ā 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.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)); }