SlideShare ist ein Scribd-Unternehmen logo
1 von 2
Downloaden Sie, um offline zu lesen
LAB # 11 HANDOUT
Objectives:
Overloaded Functions:
Overloaded functions help us to have multiple functions with the same name but with different
arguments/parameter list. This allows for better readability of the code: multiple functions having self-
evident names can be defined which take input arguments of different data types. It can also help if we
mistakenly pass wrong/different arguments to the function.
While defining overloaded functions, the thing that needs to be different is the data type of the parameter
list. The return data type may or may not be changed accordingly.
Practice Questions:
1. Write three averaging functions (for 4 numbers) with the same name but with int, float and double data
type of input argument respectively.
2. Write 3 overloaded calculator functions (for int, float and double) that take character as one of the
parameters and depending upon this parameter performs the basic 4 mathematical functions (+, -, *, /).
3. Write two overloaded functions that convert float into int and vice versa. (Hint: If the user enters 5.6, the
function should return 5 and if the user enters 4, the function should return 4.0.)
Pass By Value vs Pass By Reference:
Up until now, we have been passing the arguments to a function by value. What that means is that a copy is
created of the variables passed to the function and all the manipulation is done on that copy. This means that
there is no effect on the original value of the variable.
There is another way of passing the value of a variable to a function. That method is called pass by reference.
The syntax for doing so is as follows:
void my_function (int &my_variable);
The ampersand (&) makes it possible to pass the function by reference. It sends the address of the variable
to the function. As the changes are happening to the original value, therefore there is no need for a return value
hence the use of void.
In the main, there aren’t any changes when calling a function that takes a reference to a variable as an input
argument.
Practice Questions:
Repeat the tasks listed above but this time, pass all the values by reference.
Scope Rules and Variables:
In C++, a scope is defined by { }. Just like nested loops and nested if/else, scopes can exist inside other scopes.
The variables defined in one scope may or may not be visible/accessible inside other scopes. Scope allow us
to have same named variables even in different scopes.
{ //Outer Scope
{ //Inner Scope}
}
{ int x = 0;
{ int y = 1;
cout<<x<<endl<<y;}
}
As soon as we exit a scope, the values of the variables in that scope are destroyed. What this means is that
whenever we return from a function call, the values of the variables declared and initialized in that function
(or temporary variables) aren’t saved anymore.
Global and Static Variables:
Global variables are such variables which are visible to all the scopes in a program. These variables can be
used/updated from anywhere and can be used when there needs to be a single value visible everywhere. Any
variable that is defined and initialized above main ( ) becomes a global variable.
Variables can be made static by using the keyword static. This ensures that the value of the variable is kept
safe even after we have exited the scope. In other words, when we want to have static value between function
calls, we can make a variable static. So, no matter when that particular function is called, the value of the
particular static variable will remain the same.
Practice Questions:
1. Write a function that “remembers” how many times it has been called.
2. Declare two variables both name X. One of these variables should be accessible from anywhere. While
the other one should only be accessible from main.
3. In the above task, display the value of X declared above main in main. (Hint: Use the Unary operator :: ).
4. Re-write an averaging function for 4 numbers that only uses global variables.
5. Re-write the calculator function using global variables.

Weitere ähnliche Inhalte

Was ist angesagt?

Introduction To Programming with Python Lecture 2
Introduction To Programming with Python Lecture 2Introduction To Programming with Python Lecture 2
Introduction To Programming with Python Lecture 2Syed Farjad Zia Zaidi
 
Sorting two numbers
Sorting two numbersSorting two numbers
Sorting two numbersdevil00dante
 
Notes: Verilog Part 5 - Tasks and Functions
Notes: Verilog Part 5 - Tasks and FunctionsNotes: Verilog Part 5 - Tasks and Functions
Notes: Verilog Part 5 - Tasks and FunctionsJay Baxi
 
Compiler: Programming Language= Assignments and statements
Compiler: Programming Language= Assignments and statementsCompiler: Programming Language= Assignments and statements
Compiler: Programming Language= Assignments and statementsGeo Marian
 
Handout # 3 functions c++
Handout # 3   functions c++Handout # 3   functions c++
Handout # 3 functions c++NUST Stuff
 
Functions and tasks in verilog
Functions and tasks in verilogFunctions and tasks in verilog
Functions and tasks in verilogNallapati Anindra
 
Introduction To Programming with Python-1
Introduction To Programming with Python-1Introduction To Programming with Python-1
Introduction To Programming with Python-1Syed Farjad Zia Zaidi
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide shareDevashish Kumar
 
Demystifying Eta Expansion
Demystifying Eta ExpansionDemystifying Eta Expansion
Demystifying Eta ExpansionKnoldus Inc.
 
User Defined Functions in C
User Defined Functions in CUser Defined Functions in C
User Defined Functions in CRAJ KUMAR
 
C programming language working with functions 1
C programming language working with functions 1C programming language working with functions 1
C programming language working with functions 1Jeevan Raj
 

Was ist angesagt? (20)

Books
BooksBooks
Books
 
Functions
FunctionsFunctions
Functions
 
Mule expression
Mule expressionMule expression
Mule expression
 
Introduction To Programming with Python Lecture 2
Introduction To Programming with Python Lecture 2Introduction To Programming with Python Lecture 2
Introduction To Programming with Python Lecture 2
 
Operators in java By cheena
Operators in java By cheenaOperators in java By cheena
Operators in java By cheena
 
FUNCTION CPU
FUNCTION CPUFUNCTION CPU
FUNCTION CPU
 
Function
FunctionFunction
Function
 
ASSIGNMENT STATEMENTS AND
ASSIGNMENT STATEMENTS ANDASSIGNMENT STATEMENTS AND
ASSIGNMENT STATEMENTS AND
 
Sorting two numbers
Sorting two numbersSorting two numbers
Sorting two numbers
 
Notes: Verilog Part 5 - Tasks and Functions
Notes: Verilog Part 5 - Tasks and FunctionsNotes: Verilog Part 5 - Tasks and Functions
Notes: Verilog Part 5 - Tasks and Functions
 
Compiler: Programming Language= Assignments and statements
Compiler: Programming Language= Assignments and statementsCompiler: Programming Language= Assignments and statements
Compiler: Programming Language= Assignments and statements
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Handout # 3 functions c++
Handout # 3   functions c++Handout # 3   functions c++
Handout # 3 functions c++
 
Functions and tasks in verilog
Functions and tasks in verilogFunctions and tasks in verilog
Functions and tasks in verilog
 
Introduction To Programming with Python-1
Introduction To Programming with Python-1Introduction To Programming with Python-1
Introduction To Programming with Python-1
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
 
Demystifying Eta Expansion
Demystifying Eta ExpansionDemystifying Eta Expansion
Demystifying Eta Expansion
 
User Defined Functions in C
User Defined Functions in CUser Defined Functions in C
User Defined Functions in C
 
C programming language working with functions 1
C programming language working with functions 1C programming language working with functions 1
C programming language working with functions 1
 
Function in C++
Function in C++Function in C++
Function in C++
 

Andere mochten auch

Drg 2 geometry
Drg 2 geometryDrg 2 geometry
Drg 2 geometryNUST Stuff
 
Mohammad Mahdi Arab Farsi Resume
Mohammad Mahdi Arab Farsi ResumeMohammad Mahdi Arab Farsi Resume
Mohammad Mahdi Arab Farsi ResumeMohammad Mahdi Arab
 
Волонтёрское движение как форма социализации школьников
Волонтёрское движение как форма социализации школьниковВолонтёрское движение как форма социализации школьников
Волонтёрское движение как форма социализации школьниковАндрей Мирошниченко
 
Assignment # 3 computer system and Programming EC-105
Assignment # 3   computer system and Programming EC-105 Assignment # 3   computer system and Programming EC-105
Assignment # 3 computer system and Programming EC-105 NUST Stuff
 
Assignment # 2 computer system and Programming EC-105
Assignment # 2   computer system and Programming EC-105 Assignment # 2   computer system and Programming EC-105
Assignment # 2 computer system and Programming EC-105 NUST Stuff
 
Виставка дитячих робіт
Виставка дитячих робітВиставка дитячих робіт
Виставка дитячих робітAlexey Musienko
 
Accreditation and licensing
Accreditation and licensingAccreditation and licensing
Accreditation and licensingNUST Stuff
 
Storage container for japan market
Storage container for japan marketStorage container for japan market
Storage container for japan marketleo tan
 
Flat pack japan fukushima-disaster site
Flat pack   japan fukushima-disaster siteFlat pack   japan fukushima-disaster site
Flat pack japan fukushima-disaster siteleo tan
 
Introduction to Programming and QBasic Tutorial
Introduction to Programming and QBasic TutorialIntroduction to Programming and QBasic Tutorial
Introduction to Programming and QBasic Tutorialnhomz
 
Consumption as part of Aggregate Demand
Consumption as part of Aggregate DemandConsumption as part of Aggregate Demand
Consumption as part of Aggregate Demandmattbentley34
 
Handout # 6 pointers and recursion in c++
Handout # 6   pointers and recursion in c++Handout # 6   pointers and recursion in c++
Handout # 6 pointers and recursion in c++NUST Stuff
 
Long run aggregate_supply
Long run aggregate_supplyLong run aggregate_supply
Long run aggregate_supplymattbentley34
 

Andere mochten auch (16)

Drg 4ab mv
Drg 4ab mvDrg 4ab mv
Drg 4ab mv
 
Drg 2 geometry
Drg 2 geometryDrg 2 geometry
Drg 2 geometry
 
2559 project
2559 project 2559 project
2559 project
 
International.doc (2)
International.doc (2)International.doc (2)
International.doc (2)
 
Mohammad Mahdi Arab Farsi Resume
Mohammad Mahdi Arab Farsi ResumeMohammad Mahdi Arab Farsi Resume
Mohammad Mahdi Arab Farsi Resume
 
Волонтёрское движение как форма социализации школьников
Волонтёрское движение как форма социализации школьниковВолонтёрское движение как форма социализации школьников
Волонтёрское движение как форма социализации школьников
 
Assignment # 3 computer system and Programming EC-105
Assignment # 3   computer system and Programming EC-105 Assignment # 3   computer system and Programming EC-105
Assignment # 3 computer system and Programming EC-105
 
Assignment # 2 computer system and Programming EC-105
Assignment # 2   computer system and Programming EC-105 Assignment # 2   computer system and Programming EC-105
Assignment # 2 computer system and Programming EC-105
 
Виставка дитячих робіт
Виставка дитячих робітВиставка дитячих робіт
Виставка дитячих робіт
 
Accreditation and licensing
Accreditation and licensingAccreditation and licensing
Accreditation and licensing
 
Storage container for japan market
Storage container for japan marketStorage container for japan market
Storage container for japan market
 
Flat pack japan fukushima-disaster site
Flat pack   japan fukushima-disaster siteFlat pack   japan fukushima-disaster site
Flat pack japan fukushima-disaster site
 
Introduction to Programming and QBasic Tutorial
Introduction to Programming and QBasic TutorialIntroduction to Programming and QBasic Tutorial
Introduction to Programming and QBasic Tutorial
 
Consumption as part of Aggregate Demand
Consumption as part of Aggregate DemandConsumption as part of Aggregate Demand
Consumption as part of Aggregate Demand
 
Handout # 6 pointers and recursion in c++
Handout # 6   pointers and recursion in c++Handout # 6   pointers and recursion in c++
Handout # 6 pointers and recursion in c++
 
Long run aggregate_supply
Long run aggregate_supplyLong run aggregate_supply
Long run aggregate_supply
 

Ähnlich wie Handout # 4 functions + scopes

04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptxManas40552
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programmingAMAN ANAND
 
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM) FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)Mansi Tyagi
 
1669958779195.pdf
1669958779195.pdf1669958779195.pdf
1669958779195.pdfvenud11
 
Functions and modular programming.pptx
Functions and modular programming.pptxFunctions and modular programming.pptx
Functions and modular programming.pptxzueZ3
 
User defined function in C.pptx
User defined function in C.pptxUser defined function in C.pptx
User defined function in C.pptxRhishav Poudyal
 
Userdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdfUserdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdfDeeptiMalhotra19
 
Amit user defined functions xi (2)
Amit  user defined functions xi (2)Amit  user defined functions xi (2)
Amit user defined functions xi (2)Arpit Meena
 
GUI Programming in JAVA (Using Netbeans) - A Review
GUI Programming in JAVA (Using Netbeans) -  A ReviewGUI Programming in JAVA (Using Netbeans) -  A Review
GUI Programming in JAVA (Using Netbeans) - A ReviewFernando Torres
 
Functions-Computer programming
Functions-Computer programmingFunctions-Computer programming
Functions-Computer programmingnmahi96
 
Functions in Python.pdfnsjiwshkwijjahuwjwjw
Functions in Python.pdfnsjiwshkwijjahuwjwjwFunctions in Python.pdfnsjiwshkwijjahuwjwjw
Functions in Python.pdfnsjiwshkwijjahuwjwjwMayankSinghRawat6
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 FunctionDeepak Singh
 
Arrays &amp; functions in php
Arrays &amp; functions in phpArrays &amp; functions in php
Arrays &amp; functions in phpAshish Chamoli
 
Functions in c language
Functions in c language Functions in c language
Functions in c language tanmaymodi4
 
Functions in c language
Functions in c languageFunctions in c language
Functions in c languageTanmay Modi
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloadingankush_kumar
 

Ähnlich wie Handout # 4 functions + scopes (20)

04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM) FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 
1669958779195.pdf
1669958779195.pdf1669958779195.pdf
1669958779195.pdf
 
Functions and modular programming.pptx
Functions and modular programming.pptxFunctions and modular programming.pptx
Functions and modular programming.pptx
 
User defined function in C.pptx
User defined function in C.pptxUser defined function in C.pptx
User defined function in C.pptx
 
Userdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdfUserdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdf
 
Amit user defined functions xi (2)
Amit  user defined functions xi (2)Amit  user defined functions xi (2)
Amit user defined functions xi (2)
 
GUI Programming in JAVA (Using Netbeans) - A Review
GUI Programming in JAVA (Using Netbeans) -  A ReviewGUI Programming in JAVA (Using Netbeans) -  A Review
GUI Programming in JAVA (Using Netbeans) - A Review
 
Function
FunctionFunction
Function
 
Functions in C++.pdf
Functions in C++.pdfFunctions in C++.pdf
Functions in C++.pdf
 
Functions-Computer programming
Functions-Computer programmingFunctions-Computer programming
Functions-Computer programming
 
Functions in Python.pdfnsjiwshkwijjahuwjwjw
Functions in Python.pdfnsjiwshkwijjahuwjwjwFunctions in Python.pdfnsjiwshkwijjahuwjwjw
Functions in Python.pdfnsjiwshkwijjahuwjwjw
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
 
User Defined Functions
User Defined FunctionsUser Defined Functions
User Defined Functions
 
Arrays &amp; functions in php
Arrays &amp; functions in phpArrays &amp; functions in php
Arrays &amp; functions in php
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Functions in c language
Functions in c languageFunctions in c language
Functions in c language
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloading
 
Functions-.pdf
Functions-.pdfFunctions-.pdf
Functions-.pdf
 

Mehr von NUST Stuff

Drag force on a sphere in yield stress fluid
Drag force on a sphere in yield stress fluidDrag force on a sphere in yield stress fluid
Drag force on a sphere in yield stress fluidNUST Stuff
 
Nums entry test 2017 paper
Nums entry test 2017 paperNums entry test 2017 paper
Nums entry test 2017 paperNUST Stuff
 
Nums entry-test-new-syllabus-mbbsc-bds
Nums entry-test-new-syllabus-mbbsc-bdsNums entry-test-new-syllabus-mbbsc-bds
Nums entry-test-new-syllabus-mbbsc-bdsNUST Stuff
 
MCAT Full length paper 8-student_copy_
MCAT Full length paper  8-student_copy_MCAT Full length paper  8-student_copy_
MCAT Full length paper 8-student_copy_NUST Stuff
 
MCAT Full length paper 7-student_copy_
MCAT Full length paper 7-student_copy_MCAT Full length paper 7-student_copy_
MCAT Full length paper 7-student_copy_NUST Stuff
 
MCAT Full length paper 6-student_copy_
MCAT Full length paper  6-student_copy_MCAT Full length paper  6-student_copy_
MCAT Full length paper 6-student_copy_NUST Stuff
 
MCAT Full length paper 5-student_copy_
MCAT Full length paper 5-student_copy_MCAT Full length paper 5-student_copy_
MCAT Full length paper 5-student_copy_NUST Stuff
 
Mcat (original paper 2014)
Mcat (original paper 2014)Mcat (original paper 2014)
Mcat (original paper 2014)NUST Stuff
 
MCAT Full length paper 4-student_copy
MCAT Full length paper  4-student_copyMCAT Full length paper  4-student_copy
MCAT Full length paper 4-student_copyNUST Stuff
 
mcat (original paper 2013)
mcat (original paper 2013)mcat (original paper 2013)
mcat (original paper 2013)NUST Stuff
 
mcat (original paper 2012)
mcat (original paper 2012)mcat (original paper 2012)
mcat (original paper 2012)NUST Stuff
 
MCAT Full length paper 3 final
MCAT Full length paper 3 finalMCAT Full length paper 3 final
MCAT Full length paper 3 finalNUST Stuff
 
MCAT (original paper 2011)
MCAT (original paper 2011)MCAT (original paper 2011)
MCAT (original paper 2011)NUST Stuff
 
mcat (original paper 2010)
 mcat (original paper 2010) mcat (original paper 2010)
mcat (original paper 2010)NUST Stuff
 
MCAT Full length paper 1 (student copy)
MCAT Full length paper 1 (student copy)MCAT Full length paper 1 (student copy)
MCAT Full length paper 1 (student copy)NUST Stuff
 

Mehr von NUST Stuff (20)

Me211 1
Me211 1Me211 1
Me211 1
 
Lab LCA 1 7
Lab LCA 1 7Lab LCA 1 7
Lab LCA 1 7
 
Me211 2
Me211 2Me211 2
Me211 2
 
Me211 4
Me211 4Me211 4
Me211 4
 
Me211 3
Me211 3Me211 3
Me211 3
 
Drag force on a sphere in yield stress fluid
Drag force on a sphere in yield stress fluidDrag force on a sphere in yield stress fluid
Drag force on a sphere in yield stress fluid
 
Nums entry test 2017 paper
Nums entry test 2017 paperNums entry test 2017 paper
Nums entry test 2017 paper
 
Nums entry-test-new-syllabus-mbbsc-bds
Nums entry-test-new-syllabus-mbbsc-bdsNums entry-test-new-syllabus-mbbsc-bds
Nums entry-test-new-syllabus-mbbsc-bds
 
MCAT Full length paper 8-student_copy_
MCAT Full length paper  8-student_copy_MCAT Full length paper  8-student_copy_
MCAT Full length paper 8-student_copy_
 
MCAT Full length paper 7-student_copy_
MCAT Full length paper 7-student_copy_MCAT Full length paper 7-student_copy_
MCAT Full length paper 7-student_copy_
 
MCAT Full length paper 6-student_copy_
MCAT Full length paper  6-student_copy_MCAT Full length paper  6-student_copy_
MCAT Full length paper 6-student_copy_
 
MCAT Full length paper 5-student_copy_
MCAT Full length paper 5-student_copy_MCAT Full length paper 5-student_copy_
MCAT Full length paper 5-student_copy_
 
Mcat (original paper 2014)
Mcat (original paper 2014)Mcat (original paper 2014)
Mcat (original paper 2014)
 
MCAT Full length paper 4-student_copy
MCAT Full length paper  4-student_copyMCAT Full length paper  4-student_copy
MCAT Full length paper 4-student_copy
 
mcat (original paper 2013)
mcat (original paper 2013)mcat (original paper 2013)
mcat (original paper 2013)
 
mcat (original paper 2012)
mcat (original paper 2012)mcat (original paper 2012)
mcat (original paper 2012)
 
MCAT Full length paper 3 final
MCAT Full length paper 3 finalMCAT Full length paper 3 final
MCAT Full length paper 3 final
 
MCAT (original paper 2011)
MCAT (original paper 2011)MCAT (original paper 2011)
MCAT (original paper 2011)
 
mcat (original paper 2010)
 mcat (original paper 2010) mcat (original paper 2010)
mcat (original paper 2010)
 
MCAT Full length paper 1 (student copy)
MCAT Full length paper 1 (student copy)MCAT Full length paper 1 (student copy)
MCAT Full length paper 1 (student copy)
 

Kürzlich hochgeladen

How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
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
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
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.pptxMaritesTamaniVerdade
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
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
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
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
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 

Kürzlich hochgeladen (20)

How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
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 ...
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
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
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
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
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
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
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 

Handout # 4 functions + scopes

  • 1. LAB # 11 HANDOUT Objectives: Overloaded Functions: Overloaded functions help us to have multiple functions with the same name but with different arguments/parameter list. This allows for better readability of the code: multiple functions having self- evident names can be defined which take input arguments of different data types. It can also help if we mistakenly pass wrong/different arguments to the function. While defining overloaded functions, the thing that needs to be different is the data type of the parameter list. The return data type may or may not be changed accordingly. Practice Questions: 1. Write three averaging functions (for 4 numbers) with the same name but with int, float and double data type of input argument respectively. 2. Write 3 overloaded calculator functions (for int, float and double) that take character as one of the parameters and depending upon this parameter performs the basic 4 mathematical functions (+, -, *, /). 3. Write two overloaded functions that convert float into int and vice versa. (Hint: If the user enters 5.6, the function should return 5 and if the user enters 4, the function should return 4.0.) Pass By Value vs Pass By Reference: Up until now, we have been passing the arguments to a function by value. What that means is that a copy is created of the variables passed to the function and all the manipulation is done on that copy. This means that there is no effect on the original value of the variable. There is another way of passing the value of a variable to a function. That method is called pass by reference. The syntax for doing so is as follows: void my_function (int &my_variable); The ampersand (&) makes it possible to pass the function by reference. It sends the address of the variable to the function. As the changes are happening to the original value, therefore there is no need for a return value hence the use of void. In the main, there aren’t any changes when calling a function that takes a reference to a variable as an input argument. Practice Questions: Repeat the tasks listed above but this time, pass all the values by reference.
  • 2. Scope Rules and Variables: In C++, a scope is defined by { }. Just like nested loops and nested if/else, scopes can exist inside other scopes. The variables defined in one scope may or may not be visible/accessible inside other scopes. Scope allow us to have same named variables even in different scopes. { //Outer Scope { //Inner Scope} } { int x = 0; { int y = 1; cout<<x<<endl<<y;} } As soon as we exit a scope, the values of the variables in that scope are destroyed. What this means is that whenever we return from a function call, the values of the variables declared and initialized in that function (or temporary variables) aren’t saved anymore. Global and Static Variables: Global variables are such variables which are visible to all the scopes in a program. These variables can be used/updated from anywhere and can be used when there needs to be a single value visible everywhere. Any variable that is defined and initialized above main ( ) becomes a global variable. Variables can be made static by using the keyword static. This ensures that the value of the variable is kept safe even after we have exited the scope. In other words, when we want to have static value between function calls, we can make a variable static. So, no matter when that particular function is called, the value of the particular static variable will remain the same. Practice Questions: 1. Write a function that “remembers” how many times it has been called. 2. Declare two variables both name X. One of these variables should be accessible from anywhere. While the other one should only be accessible from main. 3. In the above task, display the value of X declared above main in main. (Hint: Use the Unary operator :: ). 4. Re-write an averaging function for 4 numbers that only uses global variables. 5. Re-write the calculator function using global variables.