Functions in c language

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();
}
1 von 30

Recomendados

Function C programming von
Function C programmingFunction C programming
Function C programmingAppili Vamsi Krishna
11.6K views29 Folien
FUNCTIONS IN c++ PPT von
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT03062679929
25.3K views24 Folien
Function in C program von
Function in C programFunction in C program
Function in C programNurul Zakiah Zamri Tan
70.4K views23 Folien
Functions in c von
Functions in cFunctions in c
Functions in csunila tharagaturi
1.5K views42 Folien
Datatype in c++ unit 3 -topic 2 von
Datatype in c++ unit 3 -topic 2Datatype in c++ unit 3 -topic 2
Datatype in c++ unit 3 -topic 2MOHIT TOMAR
2.4K views19 Folien
File handling in c von
File handling in cFile handling in c
File handling in cDavid Livingston J
28.3K views34 Folien

Más contenido relacionado

Was ist angesagt?

Data types in C von
Data types in CData types in C
Data types in CTarun Sharma
33.8K views12 Folien
Programming in c Arrays von
Programming in c ArraysProgramming in c Arrays
Programming in c Arraysjanani thirupathi
23.6K views31 Folien
Variables in C Programming von
Variables in C ProgrammingVariables in C Programming
Variables in C Programmingprogramming9
9K views7 Folien
Functions in C von
Functions in CFunctions in C
Functions in CKamal Acharya
25.1K views22 Folien
Presentation on Function in C Programming von
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C ProgrammingShuvongkor Barman
14.3K views17 Folien
Pointers von
PointersPointers
PointersProf. Dr. K. Adisesha
388 views23 Folien

Was ist angesagt?(20)

Variables in C Programming von programming9
Variables in C ProgrammingVariables in C Programming
Variables in C Programming
programming99K views
Presentation on Function in C Programming von Shuvongkor Barman
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
Shuvongkor Barman14.3K views
08 c++ Operator Overloading.ppt von Tareq Hasan
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
Tareq Hasan16.7K views
Constants in C Programming von programming9
Constants in C ProgrammingConstants in C Programming
Constants in C Programming
programming96.3K views
Java Data Types von Spotle.ai
Java Data TypesJava Data Types
Java Data Types
Spotle.ai10.8K views
Class and object in C++ von rprajat007
Class and object in C++Class and object in C++
Class and object in C++
rprajat00720.7K views
Conditional and control statement von narmadhakin
Conditional and control statementConditional and control statement
Conditional and control statement
narmadhakin18.1K views
What is identifier c programming von Rumman Ansari
What is identifier c programmingWhat is identifier c programming
What is identifier c programming
Rumman Ansari9.8K views
Data types in C language von kashyap399
Data types in C languageData types in C language
Data types in C language
kashyap3991.2K views
Constructor and Types of Constructors von Dhrumil Panchal
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
Dhrumil Panchal2.7K views

Similar a Functions in c language

Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx von
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).pptxvekariyakashyap
12 views27 Folien
Unit-III.pptx von
Unit-III.pptxUnit-III.pptx
Unit-III.pptxMehul Desai
3 views61 Folien
Ch4 functions von
Ch4 functionsCh4 functions
Ch4 functionsHattori Sidek
1.8K views41 Folien
4. function von
4. function4. function
4. functionShankar Gangaju
511 views10 Folien
CH.4FUNCTIONS IN C_FYBSC(CS).pptx von
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxSangeetaBorde3
29 views31 Folien
Modular Programming in C von
Modular Programming in CModular Programming in C
Modular Programming in Cbhawna kol
749 views31 Folien

Similar a Functions in c language (20)

Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx von vekariyakashyap
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
vekariyakashyap12 views
CH.4FUNCTIONS IN C_FYBSC(CS).pptx von SangeetaBorde3
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
SangeetaBorde329 views
Modular Programming in C von bhawna kol
Modular Programming in CModular Programming in C
Modular Programming in C
bhawna kol749 views
Functions-Computer programming von nmahi96
Functions-Computer programmingFunctions-Computer programming
Functions-Computer programming
nmahi9680 views
Functions in C++ von home
Functions in C++Functions in C++
Functions in C++
home 3.1K views
USER DEFINED FUNCTIONS IN C.pdf von BoomBoomers
USER DEFINED FUNCTIONS IN C.pdfUSER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdf
BoomBoomers102 views

Más de tanmaymodi4

Cryptocurrency von
CryptocurrencyCryptocurrency
Cryptocurrencytanmaymodi4
1.3K views17 Folien
Pointers in c v5 12102017 1 von
Pointers in c v5 12102017 1Pointers in c v5 12102017 1
Pointers in c v5 12102017 1tanmaymodi4
666 views32 Folien
Operators and expressions in c language von
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c languagetanmaymodi4
2.2K views46 Folien
Loops in c language von
Loops in c languageLoops in c language
Loops in c languagetanmaymodi4
867 views23 Folien
Dynamic memory allocation in c language von
Dynamic memory allocation in c languageDynamic memory allocation in c language
Dynamic memory allocation in c languagetanmaymodi4
252 views11 Folien
Arrays in c language von
Arrays in c languageArrays in c language
Arrays in c languagetanmaymodi4
601 views18 Folien

Más de tanmaymodi4(11)

Pointers in c v5 12102017 1 von tanmaymodi4
Pointers in c v5 12102017 1Pointers in c v5 12102017 1
Pointers in c v5 12102017 1
tanmaymodi4666 views
Operators and expressions in c language von tanmaymodi4
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
tanmaymodi42.2K views
Loops in c language von tanmaymodi4
Loops in c languageLoops in c language
Loops in c language
tanmaymodi4867 views
Dynamic memory allocation in c language von tanmaymodi4
Dynamic memory allocation in c languageDynamic memory allocation in c language
Dynamic memory allocation in c language
tanmaymodi4252 views
Arrays in c language von tanmaymodi4
Arrays in c languageArrays in c language
Arrays in c language
tanmaymodi4601 views
Decision statements in c language von tanmaymodi4
Decision statements in c languageDecision statements in c language
Decision statements in c language
tanmaymodi4315 views
Preprocessor directives in c language von tanmaymodi4
Preprocessor directives in c languagePreprocessor directives in c language
Preprocessor directives in c language
tanmaymodi46.8K views
Storage classes in c language von tanmaymodi4
Storage classes in c languageStorage classes in c language
Storage classes in c language
tanmaymodi41K views
Structures in c language von tanmaymodi4
Structures in c languageStructures in c language
Structures in c language
tanmaymodi44.1K views
Union in c language von tanmaymodi4
Union  in c languageUnion  in c language
Union in c language
tanmaymodi41.8K views

Último

Class 10 English notes 23-24.pptx von
Class 10 English notes 23-24.pptxClass 10 English notes 23-24.pptx
Class 10 English notes 23-24.pptxTARIQ KHAN
95 views53 Folien
2022 CAPE Merit List 2023 von
2022 CAPE Merit List 2023 2022 CAPE Merit List 2023
2022 CAPE Merit List 2023 Caribbean Examinations Council
3.9K views76 Folien
ANATOMY AND PHYSIOLOGY UNIT 1 { PART-1} von
ANATOMY AND PHYSIOLOGY UNIT 1 { PART-1}ANATOMY AND PHYSIOLOGY UNIT 1 { PART-1}
ANATOMY AND PHYSIOLOGY UNIT 1 { PART-1}DR .PALLAVI PATHANIA
234 views195 Folien
Student Voice von
Student Voice Student Voice
Student Voice Pooky Knightsmith
148 views33 Folien
SIMPLE PRESENT TENSE_new.pptx von
SIMPLE PRESENT TENSE_new.pptxSIMPLE PRESENT TENSE_new.pptx
SIMPLE PRESENT TENSE_new.pptxnisrinamadani2
173 views15 Folien
Lecture: Open Innovation von
Lecture: Open InnovationLecture: Open Innovation
Lecture: Open InnovationMichal Hron
95 views56 Folien

Último(20)

Class 10 English notes 23-24.pptx von TARIQ KHAN
Class 10 English notes 23-24.pptxClass 10 English notes 23-24.pptx
Class 10 English notes 23-24.pptx
TARIQ KHAN95 views
Lecture: Open Innovation von Michal Hron
Lecture: Open InnovationLecture: Open Innovation
Lecture: Open Innovation
Michal Hron95 views
Are we onboard yet University of Sussex.pptx von Jisc
Are we onboard yet University of Sussex.pptxAre we onboard yet University of Sussex.pptx
Are we onboard yet University of Sussex.pptx
Jisc71 views
OEB 2023 Co-learning To Speed Up AI Implementation in Courses.pptx von Inge de Waard
OEB 2023 Co-learning To Speed Up AI Implementation in Courses.pptxOEB 2023 Co-learning To Speed Up AI Implementation in Courses.pptx
OEB 2023 Co-learning To Speed Up AI Implementation in Courses.pptx
Inge de Waard165 views
Scope of Biochemistry.pptx von shoba shoba
Scope of Biochemistry.pptxScope of Biochemistry.pptx
Scope of Biochemistry.pptx
shoba shoba121 views
Community-led Open Access Publishing webinar.pptx von Jisc
Community-led Open Access Publishing webinar.pptxCommunity-led Open Access Publishing webinar.pptx
Community-led Open Access Publishing webinar.pptx
Jisc69 views
AI Tools for Business and Startups von Svetlin Nakov
AI Tools for Business and StartupsAI Tools for Business and Startups
AI Tools for Business and Startups
Svetlin Nakov89 views
Classification of crude drugs.pptx von GayatriPatra14
Classification of crude drugs.pptxClassification of crude drugs.pptx
Classification of crude drugs.pptx
GayatriPatra1465 views
ISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks Effectively von PECB
ISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks EffectivelyISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks Effectively
ISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks Effectively
PECB 457 views
The basics - information, data, technology and systems.pdf von JonathanCovena1
The basics - information, data, technology and systems.pdfThe basics - information, data, technology and systems.pdf
The basics - information, data, technology and systems.pdf
JonathanCovena177 views
Narration lesson plan.docx von TARIQ KHAN
Narration lesson plan.docxNarration lesson plan.docx
Narration lesson plan.docx
TARIQ KHAN99 views
11.28.23 Social Capital and Social Exclusion.pptx von mary850239
11.28.23 Social Capital and Social Exclusion.pptx11.28.23 Social Capital and Social Exclusion.pptx
11.28.23 Social Capital and Social Exclusion.pptx
mary850239112 views

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