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

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
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
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
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
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
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
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 

Recently uploaded (20)

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
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...
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
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
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
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
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 

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