SlideShare une entreprise Scribd logo
1  sur  20
Session – 18 Functions
Session Outcomes
• Why to use functions?
• Elements of user defined functions
• Types of functions
• Categories of functions
Why to Use Functions?
• All of the programs that we have studied so far have
consisted of a single function, main().
• If the project is large, and requires you to write code
with more number of lines, it would be difficult for
you to write all alone
• It is also difficult to find errors
Solution: Modular Programming
Breaking a program up into smaller, manageable unit is called
modular programming. These smaller units are called functions
or methods or subroutines ….
Examples of functions
main ()
{
int a;
a = sum(3,4);
printf(“%d”,a);
}
int sum (int x, int y)
{
int ans=x+y;
return (ans);
}
main ()
3, 4
sum(int x, int y)
3
x
4
y
7
ans
7
monitor
7
a
7
Elements of User defined functions
Elements of User defined functions are
• Function Prototype
• Function Call
• Actual parameters/arguments
• Formal parameters
• Function Definition
• Function Body
A function is a block of code that performs a particular task
when called.
Elements of User defined functions
main ()
{
int a;
a = sum(3,4);
printf(“%d”,a);
}
int sum(int,int); Function Prototype
Function Call
3,4 are actual
parameters
int sum (int x, int y)
{
int ans=x+y;
return (ans);
}
Function Definition
x and y are formal
parameters
Function Prototype
• It specify the type of value that is to be return
from the function and that is to be passed to the
function.
• It is defined in the beginning before the function
call is made.
• Syntax:
return-type name-of-function(list of arguments);
Example
int sum(int, int);
Actual and Formal Parameters
• Actual parameters are written within parenthesis at
the time of function call.
• Formal parameters are written within parenthesis at
the time of function definition.
Function Call
• A function can be called by specifying name and list
of parameters enclosed in parenthesis and separated
by comma.
• If there is no parameter empty parenthesis are placed
after function name. Ex: sum();
• If function return a value, function call is written as
assignment statement as:
Example:
a=sum(x,y);
Function Definition and Function Body
• It is the independent program module.
• It is written to specify the particular task that is to be
performed by the function.
• The first line of the function is called function
declaration and rest line inside { } is called function
body
Return Type
• Return statement will take you out of function
• It is the last statement of the function that return
certain values.
• Function returns a result to the calling statement. The
data type of the value that function returns must be
specified, otherwise void must be specified.
• Function in C can return only one value
Syntax:
 return-type (variable-name or constant);
Memory Allocation
int sum(int,int);
main ()
{
int a;
Stack Memory
main()
a
a = sum(3,4);
sum( 3, 4)
3
x
int sum (int x, int y)
{
int ans=x+y;
4
y
7
ans
return (ans);
}
7
a
printf(“%d”,a);
}
Memory Allocation
• The function data area is an area of memory that is
allocated each time the function is called, and is lost
when the function terminates.
• This data area holds the function's formal parameters,
and local variables created within.
• Variables created inside a function are local to that
function, and can be used only inside that function.
They are not directly accessible from main.
Some More Advantages of Functions
• When the same code need to be repeated in more than
one place in a given program
• Then we can put that code in a function and can call
the function as many times as we want in different
places in program where required.
• Why?
– it happens some times that you fix an error in one place
and forget to fix it in others.
– Encapsulating commonly used operations/ code into
functions also promotes the reuse of code from one
program to another.
Types of Functions
1. User Defined Functions –written by the programmer
2. Library (Built In) Functions
• They are written in the header files by the C developers.
• To use them appropriate header files should be included.
Header Files Functions Defined
stdio.h printf(), scanf(), getchar(), putchar(),
gets(), puts(), fopen(), fclose()
conio.h clrscr(), getch()
ctype.h toupper(), tolower(), isalpha()
math.h pow(), sqrt(), cos(), log()
stdlib.h rand(), exit()
string.h strlen(), strcpy(), strupr()
Categories of Functions
1. Function without arguments and with return type
2. Function with arguments without return type
3. Function without arguments and with return type
4. Function with arguments and with return type
Function without argument and
without return type
Function with argument and
without return type
Function without argument with
return type
Function with argument and with
return type
Write a program to take radius as an argument to function
area() and calculate area of circle and return its value.
#include<stdio.h>
#include<math.h>
double calArea(double r)
{
return M_PI*r*r;
}
main()
{
double r;
printf("enter radius");
scanf("%lf",&r);
double area=calArea(r);
printf("Area = %lf",area);
}

Contenu connexe

Tendances

Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C ProgrammingShuvongkor Barman
 
Recursive Function
Recursive FunctionRecursive Function
Recursive FunctionHarsh Pathak
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in CSyed Mustafa
 
VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1YOGESH SINGH
 
Functions in C - Programming
Functions in C - Programming Functions in C - Programming
Functions in C - Programming GaurangVishnoi
 
Function in c programming
Function in c programmingFunction in c programming
Function in c programmingSayeemAhmed8
 
Data Structure with C
Data Structure with CData Structure with C
Data Structure with CSyed Mustafa
 
Function in C Programming
Function in C ProgrammingFunction in C Programming
Function in C ProgrammingAnil Pokhrel
 
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
 
Programming in c (importance of c)
Programming in c (importance of c)Programming in c (importance of c)
Programming in c (importance of c)ViswanathanS21
 

Tendances (20)

Function in C Language
Function in C Language Function in C Language
Function in C Language
 
Python algorithm
Python algorithmPython algorithm
Python algorithm
 
C FUNCTIONS
C FUNCTIONSC FUNCTIONS
C FUNCTIONS
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Function & Recursion
Function & RecursionFunction & Recursion
Function & Recursion
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
 
Recursive Function
Recursive FunctionRecursive Function
Recursive Function
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in C
 
Python recursion
Python recursionPython recursion
Python recursion
 
VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1
 
User Defined Functions
User Defined FunctionsUser Defined Functions
User Defined Functions
 
Functions in C - Programming
Functions in C - Programming Functions in C - Programming
Functions in C - Programming
 
Function in c programming
Function in c programmingFunction in c programming
Function in c programming
 
Functions
FunctionsFunctions
Functions
 
Data Structure with C
Data Structure with CData Structure with C
Data Structure with C
 
Function in C Programming
Function in C ProgrammingFunction in C Programming
Function in C Programming
 
Lesson 3.2 data types for memory location
Lesson 3.2 data types for memory locationLesson 3.2 data types for memory location
Lesson 3.2 data types for memory location
 
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
 
Programming in c (importance of c)
Programming in c (importance of c)Programming in c (importance of c)
Programming in c (importance of c)
 
Basics of cpp
Basics of cppBasics of cpp
Basics of cpp
 

Similaire à Functions

5. Functions in C.pdf
5. Functions in C.pdf5. Functions in C.pdf
5. Functions in C.pdfsantosh147365
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxKhurramKhan173
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptxmiki304759
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptxAshwini Raut
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxsangeeta borde
 
U19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).pptU19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).pptManivannan837728
 
Functions in c mrs.sowmya jyothi
Functions in c mrs.sowmya jyothiFunctions in c mrs.sowmya jyothi
Functions in c mrs.sowmya jyothiSowmya Jyothi
 
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
 
ch-3 funtions - 1 class 12.pdf
ch-3 funtions - 1 class 12.pdfch-3 funtions - 1 class 12.pdf
ch-3 funtions - 1 class 12.pdfzafar578075
 
Basic information of function in cpu
Basic information of function in cpuBasic information of function in cpu
Basic information of function in cpuDhaval Jalalpara
 

Similaire à Functions (20)

arrays.ppt
arrays.pptarrays.ppt
arrays.ppt
 
5. Functions in C.pdf
5. Functions in C.pdf5. Functions in C.pdf
5. Functions in C.pdf
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
 
Unit 7. Functions
Unit 7. FunctionsUnit 7. Functions
Unit 7. Functions
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
 
Functions
FunctionsFunctions
Functions
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptx
 
U19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).pptU19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).ppt
 
Ch4 functions
Ch4 functionsCh4 functions
Ch4 functions
 
3. functions modules_programs (1)
3. functions modules_programs (1)3. functions modules_programs (1)
3. functions modules_programs (1)
 
Functions in c mrs.sowmya jyothi
Functions in c mrs.sowmya jyothiFunctions in c mrs.sowmya jyothi
Functions in c mrs.sowmya jyothi
 
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
 
1.6 Function.pdf
1.6 Function.pdf1.6 Function.pdf
1.6 Function.pdf
 
ch-3 funtions - 1 class 12.pdf
ch-3 funtions - 1 class 12.pdfch-3 funtions - 1 class 12.pdf
ch-3 funtions - 1 class 12.pdf
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
 
Functions
Functions Functions
Functions
 
Basic information of function in cpu
Basic information of function in cpuBasic information of function in cpu
Basic information of function in cpu
 
Unit iii
Unit iiiUnit iii
Unit iii
 

Plus de Lakshmi Sarvani Videla (20)

Data Science Using Python
Data Science Using PythonData Science Using Python
Data Science Using Python
 
Programs on multithreading
Programs on multithreadingPrograms on multithreading
Programs on multithreading
 
Menu Driven programs in Java
Menu Driven programs in JavaMenu Driven programs in Java
Menu Driven programs in Java
 
Recursion in C
Recursion in CRecursion in C
Recursion in C
 
Simple questions on structures concept
Simple questions on structures conceptSimple questions on structures concept
Simple questions on structures concept
 
Errors incompetitiveprogramming
Errors incompetitiveprogrammingErrors incompetitiveprogramming
Errors incompetitiveprogramming
 
Relational Operators in C
Relational Operators in CRelational Operators in C
Relational Operators in C
 
Recursive functions in C
Recursive functions in CRecursive functions in C
Recursive functions in C
 
Function Pointer in C
Function Pointer in CFunction Pointer in C
Function Pointer in C
 
Java sessionnotes
Java sessionnotesJava sessionnotes
Java sessionnotes
 
Singlelinked list
Singlelinked listSinglelinked list
Singlelinked list
 
Graphs
GraphsGraphs
Graphs
 
B trees
B treesB trees
B trees
 
Functions in python3
Functions in python3Functions in python3
Functions in python3
 
Dictionary
DictionaryDictionary
Dictionary
 
Sets
SetsSets
Sets
 
Lists
ListsLists
Lists
 
DataStructures notes
DataStructures notesDataStructures notes
DataStructures notes
 
Solutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresSolutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structures
 
C programs
C programsC programs
C programs
 

Dernier

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024SynarionITSolutions
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Principled Technologies
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 

Dernier (20)

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 

Functions

  • 1. Session – 18 Functions Session Outcomes • Why to use functions? • Elements of user defined functions • Types of functions • Categories of functions
  • 2. Why to Use Functions? • All of the programs that we have studied so far have consisted of a single function, main(). • If the project is large, and requires you to write code with more number of lines, it would be difficult for you to write all alone • It is also difficult to find errors Solution: Modular Programming Breaking a program up into smaller, manageable unit is called modular programming. These smaller units are called functions or methods or subroutines ….
  • 3. Examples of functions main () { int a; a = sum(3,4); printf(“%d”,a); } int sum (int x, int y) { int ans=x+y; return (ans); } main () 3, 4 sum(int x, int y) 3 x 4 y 7 ans 7 monitor 7 a 7
  • 4. Elements of User defined functions Elements of User defined functions are • Function Prototype • Function Call • Actual parameters/arguments • Formal parameters • Function Definition • Function Body A function is a block of code that performs a particular task when called.
  • 5. Elements of User defined functions main () { int a; a = sum(3,4); printf(“%d”,a); } int sum(int,int); Function Prototype Function Call 3,4 are actual parameters int sum (int x, int y) { int ans=x+y; return (ans); } Function Definition x and y are formal parameters
  • 6. Function Prototype • It specify the type of value that is to be return from the function and that is to be passed to the function. • It is defined in the beginning before the function call is made. • Syntax: return-type name-of-function(list of arguments); Example int sum(int, int);
  • 7. Actual and Formal Parameters • Actual parameters are written within parenthesis at the time of function call. • Formal parameters are written within parenthesis at the time of function definition.
  • 8. Function Call • A function can be called by specifying name and list of parameters enclosed in parenthesis and separated by comma. • If there is no parameter empty parenthesis are placed after function name. Ex: sum(); • If function return a value, function call is written as assignment statement as: Example: a=sum(x,y);
  • 9. Function Definition and Function Body • It is the independent program module. • It is written to specify the particular task that is to be performed by the function. • The first line of the function is called function declaration and rest line inside { } is called function body
  • 10. Return Type • Return statement will take you out of function • It is the last statement of the function that return certain values. • Function returns a result to the calling statement. The data type of the value that function returns must be specified, otherwise void must be specified. • Function in C can return only one value Syntax:  return-type (variable-name or constant);
  • 11. Memory Allocation int sum(int,int); main () { int a; Stack Memory main() a a = sum(3,4); sum( 3, 4) 3 x int sum (int x, int y) { int ans=x+y; 4 y 7 ans return (ans); } 7 a printf(“%d”,a); }
  • 12. Memory Allocation • The function data area is an area of memory that is allocated each time the function is called, and is lost when the function terminates. • This data area holds the function's formal parameters, and local variables created within. • Variables created inside a function are local to that function, and can be used only inside that function. They are not directly accessible from main.
  • 13. Some More Advantages of Functions • When the same code need to be repeated in more than one place in a given program • Then we can put that code in a function and can call the function as many times as we want in different places in program where required. • Why? – it happens some times that you fix an error in one place and forget to fix it in others. – Encapsulating commonly used operations/ code into functions also promotes the reuse of code from one program to another.
  • 14. Types of Functions 1. User Defined Functions –written by the programmer 2. Library (Built In) Functions • They are written in the header files by the C developers. • To use them appropriate header files should be included. Header Files Functions Defined stdio.h printf(), scanf(), getchar(), putchar(), gets(), puts(), fopen(), fclose() conio.h clrscr(), getch() ctype.h toupper(), tolower(), isalpha() math.h pow(), sqrt(), cos(), log() stdlib.h rand(), exit() string.h strlen(), strcpy(), strupr()
  • 15. Categories of Functions 1. Function without arguments and with return type 2. Function with arguments without return type 3. Function without arguments and with return type 4. Function with arguments and with return type
  • 16. Function without argument and without return type
  • 17. Function with argument and without return type
  • 18. Function without argument with return type
  • 19. Function with argument and with return type
  • 20. Write a program to take radius as an argument to function area() and calculate area of circle and return its value. #include<stdio.h> #include<math.h> double calArea(double r) { return M_PI*r*r; } main() { double r; printf("enter radius"); scanf("%lf",&r); double area=calArea(r); printf("Area = %lf",area); }