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

More Related Content

What's hot (20)

PPTX
Functions in C
Kamal Acharya
 
PPTX
Oop c++class(final).ppt
Alok Kumar
 
PPTX
Functions in c language
tanmaymodi4
 
PPTX
Union in C programming
Kamal Acharya
 
PPT
C++ Pointers And References
verisan
 
PPSX
Type conversion
Frijo Francis
 
PPTX
C programming - String
Achyut Devkota
 
PPTX
Enums in c
Vijayananda Ratnam Ch
 
PPTX
Function in C program
Nurul Zakiah Zamri Tan
 
PPT
structure and union
student
 
PPTX
Inline function in C++
Learn By Watch
 
PPTX
Function in C Programming
Anil Pokhrel
 
PPT
Introduction to Basic C programming 01
Wingston
 
PPT
File handling-c
CGC Technical campus,Mohali
 
PPTX
data types in C programming
Harshita Yadav
 
PPTX
Presentation on Function in C Programming
Shuvongkor Barman
 
PDF
Function in C
Dr. Abhineet Anand
 
PPTX
Variables in C and C++ Language
Way2itech
 
PPTX
Union in c language
tanmaymodi4
 
Functions in C
Kamal Acharya
 
Oop c++class(final).ppt
Alok Kumar
 
Functions in c language
tanmaymodi4
 
Union in C programming
Kamal Acharya
 
C++ Pointers And References
verisan
 
Type conversion
Frijo Francis
 
C programming - String
Achyut Devkota
 
Function in C program
Nurul Zakiah Zamri Tan
 
structure and union
student
 
Inline function in C++
Learn By Watch
 
Function in C Programming
Anil Pokhrel
 
Introduction to Basic C programming 01
Wingston
 
data types in C programming
Harshita Yadav
 
Presentation on Function in C Programming
Shuvongkor Barman
 
Function in C
Dr. Abhineet Anand
 
Variables in C and C++ Language
Way2itech
 
Union in c language
tanmaymodi4
 

Similar to Functions in c language (20)

PDF
Functions
Pragnavi Erva
 
PPTX
unit_2 (1).pptx
JVenkateshGoud
 
PPTX
Detailed concept of function in c programming
anjanasharma77573
 
PPTX
Presentation 2.pptx
ziyadaslanbey
 
PPTX
Functions IN CPROGRAMMING OF ENGINEERING.pptx
vanshhans21102005
 
PDF
Presentation 2 (1).pdf
ziyadaslanbey
 
PPTX
Functions and structure in programming c
dalalbhargavi19
 
PDF
function in in thi pdf you will learn what is fu...
kushwahashivam413
 
PPTX
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
vekariyakashyap
 
PPTX
FUNCTIONS IN C.pptx
SKUP1
 
PPTX
FUNCTIONS IN C.pptx
LECO9
 
PPTX
Functions in C.pptx
Ashwini Raut
 
PPTX
Programming in C FUNCTION Basic concepts.pptx
vengaimarbhan1
 
PPTX
Function in c
Raj Tandukar
 
PDF
VIT351 Software Development VI Unit1
YOGESH SINGH
 
PPTX
FUNCTIONengineeringtechnologyslidesh.pptx
ricknova674
 
PPTX
unit_2.pptx
Venkatesh Goud
 
PDF
Unit 3 (1)
Sowri Rajan
 
PDF
functionsinc-130108032745-phpapp01.pdf
mounikanarra3
 
PPT
c-Functions power point presentation on c functions
10300PEDDIKISHOR
 
Functions
Pragnavi Erva
 
unit_2 (1).pptx
JVenkateshGoud
 
Detailed concept of function in c programming
anjanasharma77573
 
Presentation 2.pptx
ziyadaslanbey
 
Functions IN CPROGRAMMING OF ENGINEERING.pptx
vanshhans21102005
 
Presentation 2 (1).pdf
ziyadaslanbey
 
Functions and structure in programming c
dalalbhargavi19
 
function in in thi pdf you will learn what is fu...
kushwahashivam413
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
vekariyakashyap
 
FUNCTIONS IN C.pptx
SKUP1
 
FUNCTIONS IN C.pptx
LECO9
 
Functions in C.pptx
Ashwini Raut
 
Programming in C FUNCTION Basic concepts.pptx
vengaimarbhan1
 
Function in c
Raj Tandukar
 
VIT351 Software Development VI Unit1
YOGESH SINGH
 
FUNCTIONengineeringtechnologyslidesh.pptx
ricknova674
 
unit_2.pptx
Venkatesh Goud
 
Unit 3 (1)
Sowri Rajan
 
functionsinc-130108032745-phpapp01.pdf
mounikanarra3
 
c-Functions power point presentation on c functions
10300PEDDIKISHOR
 
Ad

More from Tanmay Modi (12)

PPTX
Preprocessor directives in c laguage
Tanmay Modi
 
PPTX
Pointers in c language
Tanmay Modi
 
PPTX
Loops in c language
Tanmay Modi
 
PPTX
Generations of computers
Tanmay Modi
 
PPTX
Storage classes in c language
Tanmay Modi
 
PPTX
Union in c language
Tanmay Modi
 
PPTX
Structures in c language
Tanmay Modi
 
PPTX
Operators inc c language
Tanmay Modi
 
PPTX
Dynamic memory allocation in c language
Tanmay Modi
 
PPTX
Decision statements in c laguage
Tanmay Modi
 
PPTX
Arrays in c v1 09102017
Tanmay Modi
 
PPTX
Cryptocurrency
Tanmay Modi
 
Preprocessor directives in c laguage
Tanmay Modi
 
Pointers in c language
Tanmay Modi
 
Loops in c language
Tanmay Modi
 
Generations of computers
Tanmay Modi
 
Storage classes in c language
Tanmay Modi
 
Union in c language
Tanmay Modi
 
Structures in c language
Tanmay Modi
 
Operators inc c language
Tanmay Modi
 
Dynamic memory allocation in c language
Tanmay Modi
 
Decision statements in c laguage
Tanmay Modi
 
Arrays in c v1 09102017
Tanmay Modi
 
Cryptocurrency
Tanmay Modi
 
Ad

Recently uploaded (20)

PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PDF
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
community health nursing question paper 2.pdf
Prince kumar
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
community health nursing question paper 2.pdf
Prince kumar
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 

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