SlideShare a Scribd company logo
Functions in C
Definition
 A function is a block of code that
performs a particular task.
 Some situations when we need to write
a particular block of code for more than
once in our program: may lead to bugs
and irritation for the programmer.
 C language provides an approach in
which you need to declare and define a
group of statements once and that can
be called and used whenever required.
 Saves both time and space.
Example:
 Suppose, you have to check 3
numbers (781, 883 and 531) whether
it is prime number or not.
 Without using function, you need to
write the prime number logic 3 times.
So, there is repetition of code.
 But if you use functions, you need to
write the logic only once and you can
reuse it several times.
Advantage of functions in C
1) Code Reusability
 By creating functions in C, you can
call it many times.
 So we don't need to write the same
code again and again.
2) Code optimization
 It makes the code optimized, we don't
need to write much code.
Types of Functions
Types of Functions
There are two types of functions in C
programming:
 Library Functions: are the functions
which are declared in the C header files
such as scanf(), printf(), gets(), puts(),
ceil(), floor() etc. You just need to include
appropriate header files to use these
functions.
 User-defined functions: are the
functions which are created by the C
programmer, so that he/she can use it
many times. It reduces complexity of a
big program and optimizes the code.
Definition of a function
 Syntax:
 Return Type − A function may return a value.
The return_type is the data type of the value the
function returns. Some functions perform the
desired operations without returning a value. In
this case, the return_type is the keyword void.
 Function Name − This is the actual name of the
function. The function name and the parameter
list together constitute the function signature.
 Parameters − A parameter is like a placeholder.
When a function is invoked, you pass a value to
the parameter. This value is referred to as actual
parameter or argument. The parameter list refers
to the type, order, and number of the parameters
of a function. Parameters are optional; that is, a
function may contain no parameters.
 Function Body − The function body contains a
collection of statements that define what the
function does.
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;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
Return Value
 A C function may or may not return a
value from the function. If you don't
have to return any value from the
function, use void for the return type.
 Example without return value:
 If you want to return any value from
the function, you need to use any
data type such as int, long, char etc.
The return type depends on the
value to be returned from the
function.
 Example with return value:
 In the above example, we have to
return 10 as a value, so the return
 If you want to return floating-point
value (e.g. 10.2, 3.1, 54.5 etc), you
need to use float as the return type of
the method.
Parameters in C Function
 A c function may have 0 or more
parameters. You can have any type of
parameter in C program such as int,
float, char etc. The parameters are
also known as formal arguments.
 Example of a function that has 0
parameter:
 Example of a function that has 1
parameter:
 Example of a function that has 2
parameters:
Calling a function in C
 While creating a C function, you give a definition
of what the function has to do. To use a function,
you 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, you simply need to pass the
required parameters along with the function
name, and if the function returns a value, then
you can store the returned value.
 Syntax:
1) variable: The variable is not mandatory. If
function return type is void, you must not provide
the variable because void functions doesn't
return any value.
2) function_name: The function_name is name of
the function to be called.
3) arguments: You need to provide arguments
while calling the C function. It is also known
as actual arguments.
 Example to call a function:
Example of C function with no
return statement
Example of C function with return
statement
Call by value and call by
reference in C
 There are two ways to pass value or data to
function in C language: call by
value and call by reference.
 Original value is not modified in call by
value but it is modified in call by reference.
Call by value in C
 In call by value, original value is not
modified.
 In call by value, value being passed to
the function is locally stored by the
function parameter in stack memory
location. If you change the value of
function parameter, it is changed for
the current function only. It will not
change the value of variable inside the
caller method such as main().
Example
Call by reference in C
 In call by reference, original value is
modified because we pass reference
(address).
 Here, address of the value is passed
in the function, so actual and formal
arguments shares the same address
space. Hence, value changed inside
the function, is reflected inside as well
as outside the function.
Example:
Difference between call by value
and call by reference in c
Recursion in C
 When function is called within the
same function, it is known
as recursion in C. The function which
calls the same function, is known
as recursive function.
 Syntax:
Rules for Recursive function
 Only the user-defined function can be
involved in recursion.Library functions cannot
be involved in recursion because their source
code cannot be viewed.
 A recursive function saves return address
with the intention to return at proper location
when return to a calling function is made.
 To stop the recursive function,it is necessary
to base the recursion on test condition,and
proper terminating statement such as exit() or
return must be written using the if()
statement.
 The user-defined function main() can be
invoked recursively.
Advantages of Recursion
 Although most problems can be
solveed without recursion,but in some
situations,it is must to use
recursion.For eg, a program to display
a list of all files of the system cannot
be solved without recursion.
 Using recursion,the length of a
program can be reduced.
Disadvantages of Recursion
 Requires extra storage space.For
every recursive call,separate memory
is allocated to variables with the same
name.
 If the programmer forgets to specify
the exit condition in the recursive
function,the program will execute out
of memory.
 Not efficient in execution speed and
time.
Example
#include<stdio.h>
#include<conio.h>
int factorial (int n)
{
if (n == 1)
return 1; /*Terminating condition*/
return (n * factorial (n -1));
}
void main(){
int fact=0;
clrscr();
fact=factorial(5);
printf("n factorial of 5 is %d",fact);
getch();
}
Ad

More Related Content

What's hot (20)

Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
International Institute of Information Technology (I²IT)
 
5bit field
5bit field5bit field
5bit field
Frijo Francis
 
String Library Functions
String Library FunctionsString Library Functions
String Library Functions
Nayan Sharma
 
Storage class in c
Storage class in cStorage class in c
Storage class in c
kash95
 
Functions in C
Functions in CFunctions in C
Functions in C
Kamal Acharya
 
constants, variables and datatypes in C
constants, variables and datatypes in Cconstants, variables and datatypes in C
constants, variables and datatypes in C
Sahithi Naraparaju
 
Function in c
Function in cFunction in c
Function in c
savitamhaske
 
Storage class in C Language
Storage class in C LanguageStorage class in C Language
Storage class in C Language
Nitesh Kumar Pandey
 
Data types in C language
Data types in C languageData types in C language
Data types in C language
kashyap399
 
Function in c program
Function in c programFunction in c program
Function in c program
umesh patil
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
janani thirupathi
 
Fundamental of C Programming (Data Types)
Fundamental of C Programming (Data Types)Fundamental of C Programming (Data Types)
Fundamental of C Programming (Data Types)
jimmy majumder
 
Arrays in c
Arrays in cArrays in c
Arrays in c
Jeeva Nanthini
 
Intoduction to structure
Intoduction to structureIntoduction to structure
Intoduction to structure
Utsav276
 
Arrays & Strings
Arrays & StringsArrays & Strings
Arrays & Strings
Munazza-Mah-Jabeen
 
Types of function call
Types of function callTypes of function call
Types of function call
ArijitDhali
 
Java applet
Java appletJava applet
Java applet
Rohan Gajre
 
Operator overloading C++
Operator overloading C++Operator overloading C++
Operator overloading C++
Lahiru Dilshan
 
Multiple inheritance in java3 (1).pptx
Multiple inheritance in java3 (1).pptxMultiple inheritance in java3 (1).pptx
Multiple inheritance in java3 (1).pptx
RkGupta83
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
ThamizhselviKrishnam
 

Similar to Functions in c language (20)

Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
vekariyakashyap
 
Function C programming
Function C programmingFunction C programming
Function C programming
Appili Vamsi Krishna
 
Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
Mehul Desai
 
Ch4 functions
Ch4 functionsCh4 functions
Ch4 functions
Hattori Sidek
 
4. function
4. function4. function
4. function
Shankar Gangaju
 
All chapters C++ - Copy.pdfyttttttttttttttttttttttttttttt
All chapters C++ - Copy.pdfytttttttttttttttttttttttttttttAll chapters C++ - Copy.pdfyttttttttttttttttttttttttttttt
All chapters C++ - Copy.pdfyttttttttttttttttttttttttttttt
jacobdiriba
 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
SangeetaBorde3
 
Modular Programming in C
Modular Programming in CModular Programming in C
Modular Programming in C
bhawna kol
 
1.6 Function.pdf
1.6 Function.pdf1.6 Function.pdf
1.6 Function.pdf
NirmalaShinde3
 
Presentation 2.pptx
Presentation 2.pptxPresentation 2.pptx
Presentation 2.pptx
ziyadaslanbey
 
arrays.ppt
arrays.pptarrays.ppt
arrays.ppt
Bharath904863
 
Lecture_5_-_Functions_in_C_Detailed.pptx
Lecture_5_-_Functions_in_C_Detailed.pptxLecture_5_-_Functions_in_C_Detailed.pptx
Lecture_5_-_Functions_in_C_Detailed.pptx
Salim Shadman Ankur
 
Detailed concept of function in c programming
Detailed concept of function  in c programmingDetailed concept of function  in c programming
Detailed concept of function in c programming
anjanasharma77573
 
Functions
Functions Functions
Functions
Praneeth960856
 
FUNCTION CPU
FUNCTION CPUFUNCTION CPU
FUNCTION CPU
Krushal Kakadia
 
unit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdfunit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdf
JAVVAJI VENKATA RAO
 
PSPC-UNIT-4.pdf
PSPC-UNIT-4.pdfPSPC-UNIT-4.pdf
PSPC-UNIT-4.pdf
ArshiniGubbala3
 
C language 3
C language 3C language 3
C language 3
Arafat Bin Reza
 
Functions
FunctionsFunctions
Functions
Pragnavi Erva
 
programlama fonksiyonlar c++ hjhjghjv jg
programlama fonksiyonlar c++ hjhjghjv jgprogramlama fonksiyonlar c++ hjhjghjv jg
programlama fonksiyonlar c++ hjhjghjv jg
uleAmet
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
vekariyakashyap
 
All chapters C++ - Copy.pdfyttttttttttttttttttttttttttttt
All chapters C++ - Copy.pdfytttttttttttttttttttttttttttttAll chapters C++ - Copy.pdfyttttttttttttttttttttttttttttt
All chapters C++ - Copy.pdfyttttttttttttttttttttttttttttt
jacobdiriba
 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
SangeetaBorde3
 
Modular Programming in C
Modular Programming in CModular Programming in C
Modular Programming in C
bhawna kol
 
Lecture_5_-_Functions_in_C_Detailed.pptx
Lecture_5_-_Functions_in_C_Detailed.pptxLecture_5_-_Functions_in_C_Detailed.pptx
Lecture_5_-_Functions_in_C_Detailed.pptx
Salim Shadman Ankur
 
Detailed concept of function in c programming
Detailed concept of function  in c programmingDetailed concept of function  in c programming
Detailed concept of function in c programming
anjanasharma77573
 
unit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdfunit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdf
JAVVAJI VENKATA RAO
 
programlama fonksiyonlar c++ hjhjghjv jg
programlama fonksiyonlar c++ hjhjghjv jgprogramlama fonksiyonlar c++ hjhjghjv jg
programlama fonksiyonlar c++ hjhjghjv jg
uleAmet
 
Ad

More from Tanmay Modi (12)

Preprocessor directives in c laguage
Preprocessor directives in c laguagePreprocessor directives in c laguage
Preprocessor directives in c laguage
Tanmay Modi
 
Pointers in c language
Pointers in c languagePointers in c language
Pointers in c language
Tanmay Modi
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
Tanmay Modi
 
Generations of computers
Generations of computersGenerations of computers
Generations of computers
Tanmay Modi
 
Storage classes in c language
Storage classes in c languageStorage classes in c language
Storage classes in c language
Tanmay Modi
 
Union in c language
Union in c languageUnion in c language
Union in c language
Tanmay Modi
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
Tanmay Modi
 
Operators inc c language
Operators inc c languageOperators inc c language
Operators inc c language
Tanmay Modi
 
Dynamic memory allocation in c language
Dynamic memory allocation in c languageDynamic memory allocation in c language
Dynamic memory allocation in c language
Tanmay Modi
 
Decision statements in c laguage
Decision statements in c laguageDecision statements in c laguage
Decision statements in c laguage
Tanmay Modi
 
Arrays in c v1 09102017
Arrays in c v1 09102017Arrays in c v1 09102017
Arrays in c v1 09102017
Tanmay Modi
 
Cryptocurrency
Cryptocurrency Cryptocurrency
Cryptocurrency
Tanmay Modi
 
Preprocessor directives in c laguage
Preprocessor directives in c laguagePreprocessor directives in c laguage
Preprocessor directives in c laguage
Tanmay Modi
 
Pointers in c language
Pointers in c languagePointers in c language
Pointers in c language
Tanmay Modi
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
Tanmay Modi
 
Generations of computers
Generations of computersGenerations of computers
Generations of computers
Tanmay Modi
 
Storage classes in c language
Storage classes in c languageStorage classes in c language
Storage classes in c language
Tanmay Modi
 
Union in c language
Union in c languageUnion in c language
Union in c language
Tanmay Modi
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
Tanmay Modi
 
Operators inc c language
Operators inc c languageOperators inc c language
Operators inc c language
Tanmay Modi
 
Dynamic memory allocation in c language
Dynamic memory allocation in c languageDynamic memory allocation in c language
Dynamic memory allocation in c language
Tanmay Modi
 
Decision statements in c laguage
Decision statements in c laguageDecision statements in c laguage
Decision statements in c laguage
Tanmay Modi
 
Arrays in c v1 09102017
Arrays in c v1 09102017Arrays in c v1 09102017
Arrays in c v1 09102017
Tanmay Modi
 
Ad

Recently uploaded (20)

Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
Ajanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of HistoryAjanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of History
Virag Sontakke
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
E-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26ASE-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26AS
Abinash Palangdar
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
Overview Well-Being and Creative Careers
Overview Well-Being and Creative CareersOverview Well-Being and Creative Careers
Overview Well-Being and Creative Careers
University of Amsterdam
 
Cultivation Practice of Turmeric in Nepal.pptx
Cultivation Practice of Turmeric in Nepal.pptxCultivation Practice of Turmeric in Nepal.pptx
Cultivation Practice of Turmeric in Nepal.pptx
UmeshTimilsina1
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Association for Project Management
 
The History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptxThe History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
parmarjuli1412
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
Rock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian HistoryRock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian History
Virag Sontakke
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
Ajanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of HistoryAjanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of History
Virag Sontakke
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
E-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26ASE-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26AS
Abinash Palangdar
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
Overview Well-Being and Creative Careers
Overview Well-Being and Creative CareersOverview Well-Being and Creative Careers
Overview Well-Being and Creative Careers
University of Amsterdam
 
Cultivation Practice of Turmeric in Nepal.pptx
Cultivation Practice of Turmeric in Nepal.pptxCultivation Practice of Turmeric in Nepal.pptx
Cultivation Practice of Turmeric in Nepal.pptx
UmeshTimilsina1
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Association for Project Management
 
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
parmarjuli1412
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
Rock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian HistoryRock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian History
Virag Sontakke
 

Functions in c language

  • 2. Definition  A function is a block of code that performs a particular task.  Some situations when we need to write a particular block of code for more than once in our program: may lead to bugs and irritation for the programmer.  C language provides an approach in which you need to declare and define a group of statements once and that can be called and used whenever required.  Saves both time and space.
  • 3. Example:  Suppose, you have to check 3 numbers (781, 883 and 531) whether it is prime number or not.  Without using function, you need to write the prime number logic 3 times. So, there is repetition of code.  But if you use functions, you need to write the logic only once and you can reuse it several times.
  • 4. Advantage of functions in C 1) Code Reusability  By creating functions in C, you can call it many times.  So we don't need to write the same code again and again. 2) Code optimization  It makes the code optimized, we don't need to write much code.
  • 6. Types of Functions There are two types of functions in C programming:  Library Functions: are the functions which are declared in the C header files such as scanf(), printf(), gets(), puts(), ceil(), floor() etc. You just need to include appropriate header files to use these functions.  User-defined functions: are the functions which are created by the C programmer, so that he/she can use it many times. It reduces complexity of a big program and optimizes the code.
  • 7. Definition of a function  Syntax:
  • 8.  Return Type − A function may return a value. The return_type is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return_type is the keyword void.  Function Name − This is the actual name of the function. The function name and the parameter list together constitute the function signature.  Parameters − A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters.  Function Body − The function body contains a collection of statements that define what the function does.
  • 9. 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; if (num1 > num2) result = num1; else result = num2; return result; }
  • 10. Return Value  A C function may or may not return a value from the function. If you don't have to return any value from the function, use void for the return type.  Example without return value:
  • 11.  If you want to return any value from the function, you need to use any data type such as int, long, char etc. The return type depends on the value to be returned from the function.  Example with return value:  In the above example, we have to return 10 as a value, so the return
  • 12.  If you want to return floating-point value (e.g. 10.2, 3.1, 54.5 etc), you need to use float as the return type of the method.
  • 13. Parameters in C Function  A c function may have 0 or more parameters. You can have any type of parameter in C program such as int, float, char etc. The parameters are also known as formal arguments.  Example of a function that has 0 parameter:
  • 14.  Example of a function that has 1 parameter:  Example of a function that has 2 parameters:
  • 15. Calling a function in C  While creating a C function, you give a definition of what the function has to do. To use a function, you 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, you simply need to pass the required parameters along with the function name, and if the function returns a value, then you can store the returned value.
  • 16.  Syntax: 1) variable: The variable is not mandatory. If function return type is void, you must not provide the variable because void functions doesn't return any value. 2) function_name: The function_name is name of the function to be called. 3) arguments: You need to provide arguments while calling the C function. It is also known as actual arguments.
  • 17.  Example to call a function:
  • 18. Example of C function with no return statement
  • 19. Example of C function with return statement
  • 20. Call by value and call by reference in C  There are two ways to pass value or data to function in C language: call by value and call by reference.  Original value is not modified in call by value but it is modified in call by reference.
  • 21. Call by value in C  In call by value, original value is not modified.  In call by value, value being passed to the function is locally stored by the function parameter in stack memory location. If you change the value of function parameter, it is changed for the current function only. It will not change the value of variable inside the caller method such as main().
  • 23. Call by reference in C  In call by reference, original value is modified because we pass reference (address).  Here, address of the value is passed in the function, so actual and formal arguments shares the same address space. Hence, value changed inside the function, is reflected inside as well as outside the function.
  • 25. Difference between call by value and call by reference in c
  • 26. Recursion in C  When function is called within the same function, it is known as recursion in C. The function which calls the same function, is known as recursive function.  Syntax:
  • 27. Rules for Recursive function  Only the user-defined function can be involved in recursion.Library functions cannot be involved in recursion because their source code cannot be viewed.  A recursive function saves return address with the intention to return at proper location when return to a calling function is made.  To stop the recursive function,it is necessary to base the recursion on test condition,and proper terminating statement such as exit() or return must be written using the if() statement.  The user-defined function main() can be invoked recursively.
  • 28. Advantages of Recursion  Although most problems can be solveed without recursion,but in some situations,it is must to use recursion.For eg, a program to display a list of all files of the system cannot be solved without recursion.  Using recursion,the length of a program can be reduced.
  • 29. Disadvantages of Recursion  Requires extra storage space.For every recursive call,separate memory is allocated to variables with the same name.  If the programmer forgets to specify the exit condition in the recursive function,the program will execute out of memory.  Not efficient in execution speed and time.
  • 30. Example #include<stdio.h> #include<conio.h> int factorial (int n) { if (n == 1) return 1; /*Terminating condition*/ return (n * factorial (n -1)); } void main(){ int fact=0; clrscr(); fact=factorial(5); printf("n factorial of 5 is %d",fact); getch(); }
  翻译: