SlideShare une entreprise Scribd logo
1  sur  17
Lecture 10Lecture 10
Version 1.0Version 1.0
More on FunctionsMore on Functions
Scope RulesScope Rules
Math Library FunctionsMath Library Functions
2Rushdi Shams, Dept of CSE, KUET, Bangladesh
More Knowledge on FunctionsMore Knowledge on Functions
Actual Arguments
Formal Arguments
3Rushdi Shams, Dept of CSE, KUET, Bangladesh
More Knowledge on FunctionsMore Knowledge on Functions
 Instead of using different variable namesInstead of using different variable names xx,, yy
andand zz, we could have used the same variable, we could have used the same variable
namesnames aa,, bb andand cc
 compiler would still treat them as differentcompiler would still treat them as different
variables since they are in different functionsvariables since they are in different functions
4Rushdi Shams, Dept of CSE, KUET, Bangladesh
More Knowledge on FunctionsMore Knowledge on Functions
 There is no restriction on the number ofThere is no restriction on the number of returnreturn
statements that may be present in a functionstatements that may be present in a function
 returnreturn statement need not always be present at the endstatement need not always be present at the end
of the called functionof the called function
5Rushdi Shams, Dept of CSE, KUET, Bangladesh
More Knowledge on FunctionsMore Knowledge on Functions
 All the following are validAll the following are valid returnreturn statementsstatements
 In the last statement a garbage value is returned to theIn the last statement a garbage value is returned to the
calling function since we are not returning any specificcalling function since we are not returning any specific
valuevalue
 Note that in this case the parentheses afterNote that in this case the parentheses after returnreturn areare
droppeddropped
6Rushdi Shams, Dept of CSE, KUET, Bangladesh
More Knowledge on FunctionsMore Knowledge on Functions
 A function can return only one value at a timeA function can return only one value at a time
 the following statements are invalidthe following statements are invalid
7Rushdi Shams, Dept of CSE, KUET, Bangladesh
More Knowledge on FunctionsMore Knowledge on Functions
 If the value of a formal argument is changed in theIf the value of a formal argument is changed in the
called function, the corresponding change does not takecalled function, the corresponding change does not take
place in the calling functionplace in the calling function
8Rushdi Shams, Dept of CSE, KUET, Bangladesh
More Knowledge on FunctionsMore Knowledge on Functions
9Rushdi Shams, Dept of CSE, KUET, Bangladesh
Calling ConventionsCalling Conventions
 Calling convention indicates the order inCalling convention indicates the order in
which arguments are passed to a functionwhich arguments are passed to a function
when a function call is encounteredwhen a function call is encountered
 There are two possibilities here:There are two possibilities here:
1.1. Arguments might be passed from left to right.Arguments might be passed from left to right.
2.2. Arguments might be passed from right to left.Arguments might be passed from right to left.
 C language follows the second orderC language follows the second order
10Rushdi Shams, Dept of CSE, KUET, Bangladesh
Calling ConventionsCalling Conventions
 In this call it doesn’t matter whether the argumentsIn this call it doesn’t matter whether the arguments
are passed from left to right or from right to leftare passed from left to right or from right to left
 However, in some function call the order of passingHowever, in some function call the order of passing
arguments becomes an important considerationarguments becomes an important consideration
11Rushdi Shams, Dept of CSE, KUET, Bangladesh
Calling ConventionsCalling Conventions
12Rushdi Shams, Dept of CSE, KUET, Bangladesh
Scope RulesScope Rules
 The scope of an identifier is the portion of theThe scope of an identifier is the portion of the
program in which an identifier is referencedprogram in which an identifier is referenced
 When we declare a local variable in a block, itWhen we declare a local variable in a block, it
can be referenced only in that block or in blockscan be referenced only in that block or in blocks
nested within that blocknested within that block
13Rushdi Shams, Dept of CSE, KUET, Bangladesh
14Rushdi Shams, Dept of CSE, KUET, Bangladesh
Math Library FunctionsMath Library Functions
 Math library functions allow the programmer toMath library functions allow the programmer to
perform certain common mathematical calculationsperform certain common mathematical calculations
 A programmer desiring to calculate and print the squareA programmer desiring to calculate and print the square
root of 900.0 can write-root of 900.0 can write-
15Rushdi Shams, Dept of CSE, KUET, Bangladesh
Math Library FunctionsMath Library Functions
 When the statement is executed, sqrt() is called toWhen the statement is executed, sqrt() is called to
calculate the square root of the number contained incalculate the square root of the number contained in
the parenthesis (900.0)the parenthesis (900.0)
 The number 900.0 is the argument of sqrt() functionThe number 900.0 is the argument of sqrt() function
 The statement will print 30.00The statement will print 30.00
 sqrt() function takes an argument of type double andsqrt() function takes an argument of type double and
returns a result of type doublereturns a result of type double
 All math library functions return the data type doubleAll math library functions return the data type double
16Rushdi Shams, Dept of CSE, KUET, Bangladesh
Math Library FunctionsMath Library Functions
 If c1=13.0, d=3.0 and f=4.0, then the line would printIf c1=13.0, d=3.0 and f=4.0, then the line would print
5.005.00
17Rushdi Shams, Dept of CSE, KUET, Bangladesh
Other Useful Math FunctionsOther Useful Math Functions

Contenu connexe

Similaire à Lec 10. Functions (Part II)

Lec 09. Introduction to Functions / Call by Values
Lec 09. Introduction to Functions / Call by ValuesLec 09. Introduction to Functions / Call by Values
Lec 09. Introduction to Functions / Call by Values
Rushdi Shams
 
Lec 07. Do-While Loop / Switch
Lec 07. Do-While Loop / SwitchLec 07. Do-While Loop / Switch
Lec 07. Do-While Loop / Switch
Rushdi Shams
 
379008-rc217-functionalprogramming
379008-rc217-functionalprogramming379008-rc217-functionalprogramming
379008-rc217-functionalprogramming
Luis Atencio
 
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
Rushdi Shams
 
Data structure scope of variables
Data structure scope of variablesData structure scope of variables
Data structure scope of variables
Saurav Kumar
 

Similaire à Lec 10. Functions (Part II) (20)

Lec 09. Introduction to Functions / Call by Values
Lec 09. Introduction to Functions / Call by ValuesLec 09. Introduction to Functions / Call by Values
Lec 09. Introduction to Functions / Call by Values
 
Lec 07. Do-While Loop / Switch
Lec 07. Do-While Loop / SwitchLec 07. Do-While Loop / Switch
Lec 07. Do-While Loop / Switch
 
Vb script
Vb scriptVb script
Vb script
 
379008-rc217-functionalprogramming
379008-rc217-functionalprogramming379008-rc217-functionalprogramming
379008-rc217-functionalprogramming
 
WORKING WITH FUNCTIONS
WORKING WITH FUNCTIONSWORKING WITH FUNCTIONS
WORKING WITH FUNCTIONS
 
Computer science notes of functions chapter
Computer science notes of functions chapterComputer science notes of functions chapter
Computer science notes of functions chapter
 
Preprocessor directives
Preprocessor directivesPreprocessor directives
Preprocessor directives
 
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
 
Android Application Development - Level 3
Android Application Development - Level 3Android Application Development - Level 3
Android Application Development - Level 3
 
QTP VB Script Trainings
QTP VB Script TrainingsQTP VB Script Trainings
QTP VB Script Trainings
 
Data services-functions
Data services-functionsData services-functions
Data services-functions
 
Informatica interview questions by H2kInfosys
Informatica interview questions by H2kInfosysInformatica interview questions by H2kInfosys
Informatica interview questions by H2kInfosys
 
Cocoa heads 09112017
Cocoa heads 09112017Cocoa heads 09112017
Cocoa heads 09112017
 
System Verilog Functional Coverage
System Verilog Functional CoverageSystem Verilog Functional Coverage
System Verilog Functional Coverage
 
solidity programming solidity programming
solidity programming solidity programmingsolidity programming solidity programming
solidity programming solidity programming
 
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
 
PythonStudyMaterialSTudyMaterial.pdf
PythonStudyMaterialSTudyMaterial.pdfPythonStudyMaterialSTudyMaterial.pdf
PythonStudyMaterialSTudyMaterial.pdf
 
Library management system
Library management systemLibrary management system
Library management system
 
Data structure scope of variables
Data structure scope of variablesData structure scope of variables
Data structure scope of variables
 

Plus de Rushdi Shams

Types of machine translation
Types of machine translationTypes of machine translation
Types of machine translation
Rushdi 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 translation
Rushdi Shams
 
Syntax and semantics
Syntax and semanticsSyntax and semantics
Syntax and semantics
Rushdi Shams
 
Propositional logic
Propositional logicPropositional logic
Propositional logic
Rushdi Shams
 
Probabilistic logic
Probabilistic logicProbabilistic logic
Probabilistic logic
Rushdi Shams
 
Knowledge structure
Knowledge structureKnowledge structure
Knowledge structure
Rushdi Shams
 
Knowledge representation
Knowledge representationKnowledge representation
Knowledge representation
Rushdi Shams
 
L5 understanding hacking
L5  understanding hackingL5  understanding hacking
L5 understanding hacking
Rushdi Shams
 
L2 Intrusion Detection System (IDS)
L2  Intrusion Detection System (IDS)L2  Intrusion Detection System (IDS)
L2 Intrusion Detection System (IDS)
Rushdi Shams
 

Plus de 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
 
Probabilistic logic
Probabilistic logicProbabilistic logic
Probabilistic 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
 
L3 defense
L3  defenseL3  defense
L3 defense
 
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
 

Dernier

Dernier (20)

Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 

Lec 10. Functions (Part II)

  • 1. Lecture 10Lecture 10 Version 1.0Version 1.0 More on FunctionsMore on Functions Scope RulesScope Rules Math Library FunctionsMath Library Functions
  • 2. 2Rushdi Shams, Dept of CSE, KUET, Bangladesh More Knowledge on FunctionsMore Knowledge on Functions Actual Arguments Formal Arguments
  • 3. 3Rushdi Shams, Dept of CSE, KUET, Bangladesh More Knowledge on FunctionsMore Knowledge on Functions  Instead of using different variable namesInstead of using different variable names xx,, yy andand zz, we could have used the same variable, we could have used the same variable namesnames aa,, bb andand cc  compiler would still treat them as differentcompiler would still treat them as different variables since they are in different functionsvariables since they are in different functions
  • 4. 4Rushdi Shams, Dept of CSE, KUET, Bangladesh More Knowledge on FunctionsMore Knowledge on Functions  There is no restriction on the number ofThere is no restriction on the number of returnreturn statements that may be present in a functionstatements that may be present in a function  returnreturn statement need not always be present at the endstatement need not always be present at the end of the called functionof the called function
  • 5. 5Rushdi Shams, Dept of CSE, KUET, Bangladesh More Knowledge on FunctionsMore Knowledge on Functions  All the following are validAll the following are valid returnreturn statementsstatements  In the last statement a garbage value is returned to theIn the last statement a garbage value is returned to the calling function since we are not returning any specificcalling function since we are not returning any specific valuevalue  Note that in this case the parentheses afterNote that in this case the parentheses after returnreturn areare droppeddropped
  • 6. 6Rushdi Shams, Dept of CSE, KUET, Bangladesh More Knowledge on FunctionsMore Knowledge on Functions  A function can return only one value at a timeA function can return only one value at a time  the following statements are invalidthe following statements are invalid
  • 7. 7Rushdi Shams, Dept of CSE, KUET, Bangladesh More Knowledge on FunctionsMore Knowledge on Functions  If the value of a formal argument is changed in theIf the value of a formal argument is changed in the called function, the corresponding change does not takecalled function, the corresponding change does not take place in the calling functionplace in the calling function
  • 8. 8Rushdi Shams, Dept of CSE, KUET, Bangladesh More Knowledge on FunctionsMore Knowledge on Functions
  • 9. 9Rushdi Shams, Dept of CSE, KUET, Bangladesh Calling ConventionsCalling Conventions  Calling convention indicates the order inCalling convention indicates the order in which arguments are passed to a functionwhich arguments are passed to a function when a function call is encounteredwhen a function call is encountered  There are two possibilities here:There are two possibilities here: 1.1. Arguments might be passed from left to right.Arguments might be passed from left to right. 2.2. Arguments might be passed from right to left.Arguments might be passed from right to left.  C language follows the second orderC language follows the second order
  • 10. 10Rushdi Shams, Dept of CSE, KUET, Bangladesh Calling ConventionsCalling Conventions  In this call it doesn’t matter whether the argumentsIn this call it doesn’t matter whether the arguments are passed from left to right or from right to leftare passed from left to right or from right to left  However, in some function call the order of passingHowever, in some function call the order of passing arguments becomes an important considerationarguments becomes an important consideration
  • 11. 11Rushdi Shams, Dept of CSE, KUET, Bangladesh Calling ConventionsCalling Conventions
  • 12. 12Rushdi Shams, Dept of CSE, KUET, Bangladesh Scope RulesScope Rules  The scope of an identifier is the portion of theThe scope of an identifier is the portion of the program in which an identifier is referencedprogram in which an identifier is referenced  When we declare a local variable in a block, itWhen we declare a local variable in a block, it can be referenced only in that block or in blockscan be referenced only in that block or in blocks nested within that blocknested within that block
  • 13. 13Rushdi Shams, Dept of CSE, KUET, Bangladesh
  • 14. 14Rushdi Shams, Dept of CSE, KUET, Bangladesh Math Library FunctionsMath Library Functions  Math library functions allow the programmer toMath library functions allow the programmer to perform certain common mathematical calculationsperform certain common mathematical calculations  A programmer desiring to calculate and print the squareA programmer desiring to calculate and print the square root of 900.0 can write-root of 900.0 can write-
  • 15. 15Rushdi Shams, Dept of CSE, KUET, Bangladesh Math Library FunctionsMath Library Functions  When the statement is executed, sqrt() is called toWhen the statement is executed, sqrt() is called to calculate the square root of the number contained incalculate the square root of the number contained in the parenthesis (900.0)the parenthesis (900.0)  The number 900.0 is the argument of sqrt() functionThe number 900.0 is the argument of sqrt() function  The statement will print 30.00The statement will print 30.00  sqrt() function takes an argument of type double andsqrt() function takes an argument of type double and returns a result of type doublereturns a result of type double  All math library functions return the data type doubleAll math library functions return the data type double
  • 16. 16Rushdi Shams, Dept of CSE, KUET, Bangladesh Math Library FunctionsMath Library Functions  If c1=13.0, d=3.0 and f=4.0, then the line would printIf c1=13.0, d=3.0 and f=4.0, then the line would print 5.005.00
  • 17. 17Rushdi Shams, Dept of CSE, KUET, Bangladesh Other Useful Math FunctionsOther Useful Math Functions