SlideShare a Scribd company logo
1 of 21
Lecture 9Lecture 9
Version 1.0Version 1.0
Introduction to FunctionsIntroduction to Functions
Call by ValueCall by Value
2Rushdi Shams, Dept of CSE, KUET, Bangladesh
Introduction to FunctionsIntroduction to Functions
 We have already known that in a C program,We have already known that in a C program,
there must be a main () functionthere must be a main () function
 In C, we used many library functions as well-In C, we used many library functions as well-
printf (), scanf (), clrscr (), or getch ()printf (), scanf (), clrscr (), or getch ()
 printf() prints on the outputprintf() prints on the output
 scanf () takes input from the user and assignsscanf () takes input from the user and assigns
that input to a variable by going to the variable’sthat input to a variable by going to the variable’s
addressaddress
3Rushdi Shams, Dept of CSE, KUET, Bangladesh
Introduction to FunctionsIntroduction to Functions
 clrscr () clears the output bufferclrscr () clears the output buffer
 getch () waits for a key-stroke from the usergetch () waits for a key-stroke from the user
 if you think uniform one thing is common toif you think uniform one thing is common to
say- the function does one particular job. Isn’tsay- the function does one particular job. Isn’t
it?it?
4Rushdi Shams, Dept of CSE, KUET, Bangladesh
Introduction to FunctionsIntroduction to Functions
 In C, user can define functions as well- as manyIn C, user can define functions as well- as many
functions as they wish. These functions arefunctions as they wish. These functions are
called user defined functionscalled user defined functions
5Rushdi Shams, Dept of CSE, KUET, Bangladesh
Example of FunctionExample of Function
6Rushdi Shams, Dept of CSE, KUET, Bangladesh
Another ExampleAnother Example
7Rushdi Shams, Dept of CSE, KUET, Bangladesh
What We have learnt?What We have learnt?
 There is no limit on the number of functionsThere is no limit on the number of functions
that might be present in a C program.that might be present in a C program.
 Each function in a program is called in theEach function in a program is called in the
sequence specified by the function calls.sequence specified by the function calls.
 After each function has done its job, controlAfter each function has done its job, control
returns to the place of calling that function.returns to the place of calling that function.
8Rushdi Shams, Dept of CSE, KUET, Bangladesh
 Well, then, should all the function calls takeWell, then, should all the function calls take
place inside the main () function? No! Functionsplace inside the main () function? No! Functions
defined by the user can call other functions asdefined by the user can call other functions as
well.well.
9Rushdi Shams, Dept of CSE, KUET, Bangladesh
10Rushdi Shams, Dept of CSE, KUET, Bangladesh
We have known a lot!!We have known a lot!!
 C program is a collection of one or more functions.C program is a collection of one or more functions.
 A function gets called when the function name isA function gets called when the function name is
followed by a semicolon.followed by a semicolon.
11Rushdi Shams, Dept of CSE, KUET, Bangladesh
We have known a lot!!We have known a lot!!
 A function is defined when function name is followedA function is defined when function name is followed
by a pair of braces in which one or more statementsby a pair of braces in which one or more statements
may be present.may be present.
12Rushdi Shams, Dept of CSE, KUET, Bangladesh
We have known a lot!!We have known a lot!!
 Any function can be called from any other function.Any function can be called from any other function.
EvenEven main( )main( ) can be called from other functions.can be called from other functions.
13Rushdi Shams, Dept of CSE, KUET, Bangladesh
We have known a lot!!We have known a lot!!
 A function can be called any number of times.A function can be called any number of times.
14Rushdi Shams, Dept of CSE, KUET, Bangladesh
We have known a lot!!We have known a lot!!
 The order in which the functions are defined in aThe order in which the functions are defined in a
program and the order in which they get called needprogram and the order in which they get called need
not necessarily be samenot necessarily be same
15Rushdi Shams, Dept of CSE, KUET, Bangladesh
We have known a lot!!We have known a lot!!
 A function can be called from other function, but aA function can be called from other function, but a
function cannot be defined in another function. Thus,function cannot be defined in another function. Thus,
the following program code would be wrong, sincethe following program code would be wrong, since
argentina( )argentina( ) is being defined inside another function,is being defined inside another function,
main( )main( )..
16Rushdi Shams, Dept of CSE, KUET, Bangladesh
We have known a lot!!We have known a lot!!
 There are basically two types of functions:There are basically two types of functions:
 Library functions Ex.Library functions Ex. printf( )printf( ),, scanf( )scanf( ) etc.etc.
 User-defined functions Ex.User-defined functions Ex. argentina( )argentina( ),,
brazil( )brazil( ) etc.etc.
17Rushdi Shams, Dept of CSE, KUET, Bangladesh
18Rushdi Shams, Dept of CSE, KUET, Bangladesh
Call by ValueCall by Value
 Function Declaration or function prototypingFunction Declaration or function prototyping
 Function Definition or function bodyFunction Definition or function body
 The basic format of function definition is-The basic format of function definition is-
19Rushdi Shams, Dept of CSE, KUET, Bangladesh
Call by ValueCall by Value
 We have seen only void as the return-type in functionsWe have seen only void as the return-type in functions
so far. But it can be of any valid C data type. It specifiesso far. But it can be of any valid C data type. It specifies
what type of data the function will hand over to thewhat type of data the function will hand over to the
callercaller
 Parameter-list is a comma separated list containingParameter-list is a comma separated list containing
declaration of parameters received by the functiondeclaration of parameters received by the function
when it is called. We have also seen only void in thewhen it is called. We have also seen only void in the
parameter-lists so far (it means the function does notparameter-lists so far (it means the function does not
get any value from the caller)get any value from the caller)
20Rushdi Shams, Dept of CSE, KUET, Bangladesh
Call by ValueCall by Value
10,20,30
maximum(10,20,30)
maximum(10,20,30)
21Rushdi Shams, Dept of CSE, KUET, Bangladesh
Call by ValueCall by Value

More Related Content

Viewers also liked

Lec 15. Pointers and Arrays
Lec 15. Pointers and ArraysLec 15. Pointers and Arrays
Lec 15. Pointers and ArraysRushdi Shams
 
Probabilistic logic
Probabilistic logicProbabilistic logic
Probabilistic logicRushdi Shams
 
Lec 02. C Program Structure / C Memory Concept
Lec 02. C Program Structure / C Memory ConceptLec 02. C Program Structure / C Memory Concept
Lec 02. C Program Structure / C Memory ConceptRushdi Shams
 
Lec 23. Files (Part II)
Lec 23. Files (Part II)Lec 23. Files (Part II)
Lec 23. Files (Part II)Rushdi Shams
 
L14 l15 Object Oriented DBMS
L14 l15  Object Oriented DBMSL14 l15  Object Oriented DBMS
L14 l15 Object Oriented DBMSRushdi Shams
 
L4 domain integrity
L4  domain integrityL4  domain integrity
L4 domain integrityRushdi Shams
 
L9 l10 server side programming
L9 l10  server side programmingL9 l10  server side programming
L9 l10 server side programmingRushdi Shams
 

Viewers also liked (8)

Lec 15. Pointers and Arrays
Lec 15. Pointers and ArraysLec 15. Pointers and Arrays
Lec 15. Pointers and Arrays
 
Probabilistic logic
Probabilistic logicProbabilistic logic
Probabilistic logic
 
Lec 02. C Program Structure / C Memory Concept
Lec 02. C Program Structure / C Memory ConceptLec 02. C Program Structure / C Memory Concept
Lec 02. C Program Structure / C Memory Concept
 
Lec 23. Files (Part II)
Lec 23. Files (Part II)Lec 23. Files (Part II)
Lec 23. Files (Part II)
 
L3 defense
L3  defenseL3  defense
L3 defense
 
L14 l15 Object Oriented DBMS
L14 l15  Object Oriented DBMSL14 l15  Object Oriented DBMS
L14 l15 Object Oriented DBMS
 
L4 domain integrity
L4  domain integrityL4  domain integrity
L4 domain integrity
 
L9 l10 server side programming
L9 l10  server side programmingL9 l10  server side programming
L9 l10 server side programming
 

Similar to Lec 09. Introduction to Functions / Call by Values

Preprocessor directives
Preprocessor directivesPreprocessor directives
Preprocessor directivesVikash Dhal
 
Twins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingTwins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingRichardWarburton
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in CHarendra Singh
 
Presentation 2 (1).pdf
Presentation 2 (1).pdfPresentation 2 (1).pdf
Presentation 2 (1).pdfziyadaslanbey
 
Functions in c
Functions in cFunctions in c
Functions in creshmy12
 
Basic information of function in cpu
Basic information of function in cpuBasic information of function in cpu
Basic information of function in cpuDhaval Jalalpara
 
Basic structure of C++ program
Basic structure of C++ programBasic structure of C++ program
Basic structure of C++ programmatiur rahman
 
Data structure scope of variables
Data structure scope of variablesData structure scope of variables
Data structure scope of variablesSaurav Kumar
 
C Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointC Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointJavaTpoint.Com
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorialsakreyi
 
379008-rc217-functionalprogramming
379008-rc217-functionalprogramming379008-rc217-functionalprogramming
379008-rc217-functionalprogrammingLuis Atencio
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And AnswerJagan Mohan Bishoyi
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answerlavparmar007
 

Similar to Lec 09. Introduction to Functions / Call by Values (20)

Preprocessor directives
Preprocessor directivesPreprocessor directives
Preprocessor directives
 
[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++
 
Presentation 2.pptx
Presentation 2.pptxPresentation 2.pptx
Presentation 2.pptx
 
Twins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingTwins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional Programming
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Function in C
Function in CFunction in C
Function in C
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
 
Presentation 2 (1).pdf
Presentation 2 (1).pdfPresentation 2 (1).pdf
Presentation 2 (1).pdf
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Basic information of function in cpu
Basic information of function in cpuBasic information of function in cpu
Basic information of function in cpu
 
Basic structure of C++ program
Basic structure of C++ programBasic structure of C++ program
Basic structure of C++ program
 
Data structure scope of variables
Data structure scope of variablesData structure scope of variables
Data structure scope of variables
 
C Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointC Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpoint
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Unit - 2.ppt
Unit - 2.pptUnit - 2.ppt
Unit - 2.ppt
 
379008-rc217-functionalprogramming
379008-rc217-functionalprogramming379008-rc217-functionalprogramming
379008-rc217-functionalprogramming
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And Answer
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answer
 

More from Rushdi Shams

Research Methodology and Tips on Better Research
Research Methodology and Tips on Better ResearchResearch Methodology and Tips on Better Research
Research Methodology and Tips on Better ResearchRushdi Shams
 
Common evaluation measures in NLP and IR
Common evaluation measures in NLP and IRCommon evaluation measures in NLP and IR
Common evaluation measures in NLP and IRRushdi Shams
 
Machine learning with nlp 101
Machine learning with nlp 101Machine learning with nlp 101
Machine learning with nlp 101Rushdi Shams
 
Semi-supervised classification for natural language processing
Semi-supervised classification for natural language processingSemi-supervised classification for natural language processing
Semi-supervised classification for natural language processingRushdi Shams
 
Natural Language Processing: Parsing
Natural Language Processing: ParsingNatural Language Processing: Parsing
Natural Language Processing: ParsingRushdi Shams
 
Types of machine translation
Types of machine translationTypes of machine translation
Types of machine translationRushdi Shams
 
L1 l2 l3 introduction to machine translation
L1 l2 l3  introduction to machine translationL1 l2 l3  introduction to machine translation
L1 l2 l3 introduction to machine translationRushdi Shams
 
Syntax and semantics
Syntax and semanticsSyntax and semantics
Syntax and semanticsRushdi Shams
 
Propositional logic
Propositional logicPropositional logic
Propositional logicRushdi Shams
 
Knowledge structure
Knowledge structureKnowledge structure
Knowledge structureRushdi Shams
 
Knowledge representation
Knowledge representationKnowledge representation
Knowledge representationRushdi Shams
 
L5 understanding hacking
L5  understanding hackingL5  understanding hacking
L5 understanding hackingRushdi Shams
 
L2 Intrusion Detection System (IDS)
L2  Intrusion Detection System (IDS)L2  Intrusion Detection System (IDS)
L2 Intrusion Detection System (IDS)Rushdi Shams
 
L2 l3 l4 software process models
L2 l3 l4  software process modelsL2 l3 l4  software process models
L2 l3 l4 software process modelsRushdi Shams
 
L1 overview of software engineering
L1  overview of software engineeringL1  overview of software engineering
L1 overview of software engineeringRushdi Shams
 

More from Rushdi Shams (20)

Research Methodology and Tips on Better Research
Research Methodology and Tips on Better ResearchResearch Methodology and Tips on Better Research
Research Methodology and Tips on Better Research
 
Common evaluation measures in NLP and IR
Common evaluation measures in NLP and IRCommon evaluation measures in NLP and IR
Common evaluation measures in NLP and IR
 
Machine learning with nlp 101
Machine learning with nlp 101Machine learning with nlp 101
Machine learning with nlp 101
 
Semi-supervised classification for natural language processing
Semi-supervised classification for natural language processingSemi-supervised classification for natural language processing
Semi-supervised classification for natural language processing
 
Natural Language Processing: Parsing
Natural Language Processing: ParsingNatural Language Processing: Parsing
Natural Language Processing: Parsing
 
Types of machine translation
Types of machine translationTypes of machine translation
Types of machine translation
 
L1 l2 l3 introduction to machine translation
L1 l2 l3  introduction to machine translationL1 l2 l3  introduction to machine translation
L1 l2 l3 introduction to machine translation
 
Syntax and semantics
Syntax and semanticsSyntax and semantics
Syntax and semantics
 
Propositional logic
Propositional logicPropositional logic
Propositional logic
 
L15 fuzzy logic
L15  fuzzy logicL15  fuzzy logic
L15 fuzzy logic
 
Knowledge structure
Knowledge structureKnowledge structure
Knowledge structure
 
Knowledge representation
Knowledge representationKnowledge representation
Knowledge representation
 
First order logic
First order logicFirst order logic
First order logic
 
Belief function
Belief functionBelief function
Belief function
 
L5 understanding hacking
L5  understanding hackingL5  understanding hacking
L5 understanding hacking
 
L4 vpn
L4  vpnL4  vpn
L4 vpn
 
L2 Intrusion Detection System (IDS)
L2  Intrusion Detection System (IDS)L2  Intrusion Detection System (IDS)
L2 Intrusion Detection System (IDS)
 
L1 phishing
L1  phishingL1  phishing
L1 phishing
 
L2 l3 l4 software process models
L2 l3 l4  software process modelsL2 l3 l4  software process models
L2 l3 l4 software process models
 
L1 overview of software engineering
L1  overview of software engineeringL1  overview of software engineering
L1 overview of software engineering
 

Recently uploaded

Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 

Recently uploaded (20)

Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 

Lec 09. Introduction to Functions / Call by Values

  • 1. Lecture 9Lecture 9 Version 1.0Version 1.0 Introduction to FunctionsIntroduction to Functions Call by ValueCall by Value
  • 2. 2Rushdi Shams, Dept of CSE, KUET, Bangladesh Introduction to FunctionsIntroduction to Functions  We have already known that in a C program,We have already known that in a C program, there must be a main () functionthere must be a main () function  In C, we used many library functions as well-In C, we used many library functions as well- printf (), scanf (), clrscr (), or getch ()printf (), scanf (), clrscr (), or getch ()  printf() prints on the outputprintf() prints on the output  scanf () takes input from the user and assignsscanf () takes input from the user and assigns that input to a variable by going to the variable’sthat input to a variable by going to the variable’s addressaddress
  • 3. 3Rushdi Shams, Dept of CSE, KUET, Bangladesh Introduction to FunctionsIntroduction to Functions  clrscr () clears the output bufferclrscr () clears the output buffer  getch () waits for a key-stroke from the usergetch () waits for a key-stroke from the user  if you think uniform one thing is common toif you think uniform one thing is common to say- the function does one particular job. Isn’tsay- the function does one particular job. Isn’t it?it?
  • 4. 4Rushdi Shams, Dept of CSE, KUET, Bangladesh Introduction to FunctionsIntroduction to Functions  In C, user can define functions as well- as manyIn C, user can define functions as well- as many functions as they wish. These functions arefunctions as they wish. These functions are called user defined functionscalled user defined functions
  • 5. 5Rushdi Shams, Dept of CSE, KUET, Bangladesh Example of FunctionExample of Function
  • 6. 6Rushdi Shams, Dept of CSE, KUET, Bangladesh Another ExampleAnother Example
  • 7. 7Rushdi Shams, Dept of CSE, KUET, Bangladesh What We have learnt?What We have learnt?  There is no limit on the number of functionsThere is no limit on the number of functions that might be present in a C program.that might be present in a C program.  Each function in a program is called in theEach function in a program is called in the sequence specified by the function calls.sequence specified by the function calls.  After each function has done its job, controlAfter each function has done its job, control returns to the place of calling that function.returns to the place of calling that function.
  • 8. 8Rushdi Shams, Dept of CSE, KUET, Bangladesh  Well, then, should all the function calls takeWell, then, should all the function calls take place inside the main () function? No! Functionsplace inside the main () function? No! Functions defined by the user can call other functions asdefined by the user can call other functions as well.well.
  • 9. 9Rushdi Shams, Dept of CSE, KUET, Bangladesh
  • 10. 10Rushdi Shams, Dept of CSE, KUET, Bangladesh We have known a lot!!We have known a lot!!  C program is a collection of one or more functions.C program is a collection of one or more functions.  A function gets called when the function name isA function gets called when the function name is followed by a semicolon.followed by a semicolon.
  • 11. 11Rushdi Shams, Dept of CSE, KUET, Bangladesh We have known a lot!!We have known a lot!!  A function is defined when function name is followedA function is defined when function name is followed by a pair of braces in which one or more statementsby a pair of braces in which one or more statements may be present.may be present.
  • 12. 12Rushdi Shams, Dept of CSE, KUET, Bangladesh We have known a lot!!We have known a lot!!  Any function can be called from any other function.Any function can be called from any other function. EvenEven main( )main( ) can be called from other functions.can be called from other functions.
  • 13. 13Rushdi Shams, Dept of CSE, KUET, Bangladesh We have known a lot!!We have known a lot!!  A function can be called any number of times.A function can be called any number of times.
  • 14. 14Rushdi Shams, Dept of CSE, KUET, Bangladesh We have known a lot!!We have known a lot!!  The order in which the functions are defined in aThe order in which the functions are defined in a program and the order in which they get called needprogram and the order in which they get called need not necessarily be samenot necessarily be same
  • 15. 15Rushdi Shams, Dept of CSE, KUET, Bangladesh We have known a lot!!We have known a lot!!  A function can be called from other function, but aA function can be called from other function, but a function cannot be defined in another function. Thus,function cannot be defined in another function. Thus, the following program code would be wrong, sincethe following program code would be wrong, since argentina( )argentina( ) is being defined inside another function,is being defined inside another function, main( )main( )..
  • 16. 16Rushdi Shams, Dept of CSE, KUET, Bangladesh We have known a lot!!We have known a lot!!  There are basically two types of functions:There are basically two types of functions:  Library functions Ex.Library functions Ex. printf( )printf( ),, scanf( )scanf( ) etc.etc.  User-defined functions Ex.User-defined functions Ex. argentina( )argentina( ),, brazil( )brazil( ) etc.etc.
  • 17. 17Rushdi Shams, Dept of CSE, KUET, Bangladesh
  • 18. 18Rushdi Shams, Dept of CSE, KUET, Bangladesh Call by ValueCall by Value  Function Declaration or function prototypingFunction Declaration or function prototyping  Function Definition or function bodyFunction Definition or function body  The basic format of function definition is-The basic format of function definition is-
  • 19. 19Rushdi Shams, Dept of CSE, KUET, Bangladesh Call by ValueCall by Value  We have seen only void as the return-type in functionsWe have seen only void as the return-type in functions so far. But it can be of any valid C data type. It specifiesso far. But it can be of any valid C data type. It specifies what type of data the function will hand over to thewhat type of data the function will hand over to the callercaller  Parameter-list is a comma separated list containingParameter-list is a comma separated list containing declaration of parameters received by the functiondeclaration of parameters received by the function when it is called. We have also seen only void in thewhen it is called. We have also seen only void in the parameter-lists so far (it means the function does notparameter-lists so far (it means the function does not get any value from the caller)get any value from the caller)
  • 20. 20Rushdi Shams, Dept of CSE, KUET, Bangladesh Call by ValueCall by Value 10,20,30 maximum(10,20,30) maximum(10,20,30)
  • 21. 21Rushdi Shams, Dept of CSE, KUET, Bangladesh Call by ValueCall by Value