introduction to programming lecture 6. functions – call by value – call by reference...
DESCRIPTION
FunctionsTRANSCRIPT
![Page 1: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/1.jpg)
Introduction to Programming
Lecture 6
![Page 2: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/2.jpg)
• Functions– Call by value– Call by reference
Today's Lecture Includes
![Page 3: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/3.jpg)
Functions
![Page 4: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/4.jpg)
Laboratory Stool
![Page 5: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/5.jpg)
Constructing a laboratory Stool
![Page 6: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/6.jpg)
Constructing a laboratory Stool
• Task: Making a stool– Subtask:
• Make a seat• Make legs for the stool• Assemble them
![Page 7: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/7.jpg)
• What are functions?• How are they defined ?• How are they declared ?• What values are passed to functions ?• What values do functions return ?
What we will study today …
![Page 8: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/8.jpg)
FunctionFunction name{
Body of the function}
![Page 9: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/9.jpg)
Function
Two types of functions: 1. Functions that return a value2. Functions that do not return a value
![Page 10: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/10.jpg)
return-value-type function-name( argument-list ){
declarations and statements}
Function
![Page 11: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/11.jpg)
return-value-type function-name( argument--type-list) ;
main ( ){
:}
Declaration of Function
![Page 12: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/12.jpg)
Exampleint function-name ( int , int , double ) ;
void main ( ) {
….}
![Page 13: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/13.jpg)
int function-name ( int i , double j ){
…}
Definition of Function
![Page 14: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/14.jpg)
int square ( int ) ;
Return Type of Function
int square ( int i )int square ( int i ){{
return ( i * i ) ;return ( i * i ) ;}}
Declaration
Definition
![Page 15: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/15.jpg)
int x ;x = square ( i ) ;
Function Call
![Page 16: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/16.jpg)
double raiseToPow ( double x , int power ){
double result ;int i ;result = 1.0 ;for ( i = 1 ; i <= power ; i ++ ) // braces first{
result * = x ; // result = result *x}return ( result ) ;
}
Example: Function to calculate integer power ( Xn )
![Page 17: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/17.jpg)
include < iostream.h >void main ( ) {
double x ;int i ;cout << “ Please enter the number “ ;cin >> x ;cout << “ Please enter the integer power that you want this number raised to “ ;cin >> i ;cout << x << “ raise to power “ << i << “is equal to “ << raiseToPow ( x , i ) ;
}
Code to Call the raisetopow Function
![Page 18: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/18.jpg)
Call By Value
![Page 19: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/19.jpg)
Calling function
Called function
![Page 20: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/20.jpg)
Area of the Ring
Outer Circle
Inner Circle
____ Area of Inner CircleArea of Outer Circle = Area of the Ring
![Page 21: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/21.jpg)
double circleArea ( double radius ){ return ( 3.1415926 * radius * radius ) ;}
Example: Function to calculate the area of a circle
![Page 22: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/22.jpg)
main ( ){
:ringArea = ( 3.1415926 * rad1 * rad1 ) – ( 3.1415926 * rad2 * rad2 ) ;
}
Calculating ringArea without using Function
![Page 23: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/23.jpg)
Exercises1. Modify the raise to power function so
that it can handle negative power of x, zero and positive power of x.
2. For the area of ring function put in error checking mechanism.
![Page 24: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/24.jpg)
• We used functions for breaking complex problems into smaller pieces, which is a top-down structured approach.
• Each function should be a small module, self contained and it should solve a well defined problem.
• Variable names and function names should be self explanatory.• Always comment your code
What we have studied so far?
![Page 25: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/25.jpg)
#include <iostream.h>
Header Files
![Page 26: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/26.jpg)
int functionName ( int , int );
Prototype
Return valueAssignment List with data type
![Page 27: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/27.jpg)
double pi = 3.1415926;
Using Header Files
It is better to define this value in a header fileIt is better to define this value in a header file
Then simply by including the header file in the Then simply by including the header file in the program this value is defined and it has a program this value is defined and it has a meaningful namemeaningful name
![Page 28: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/28.jpg)
• #define pi 3.1415926• Name can be used inside a program exactly
like a variable• It cannot be used as a variable
CircleArea = pi * radius * radiusCircleArea = pi * radius * radius
Circumference = 2 * pi * radiusCircumference = 2 * pi * radius
#define
![Page 29: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/29.jpg)
• Identifier is any name user creates in his/her program
• Functions are also identifiers
• Labels are also identifiers
Scope of Identifiers
![Page 30: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/30.jpg)
• Scope means visibility
• A variable declared inside a block has visibility within that block only
• Variables defined within the function has a scope that is function wide
Scope of Identifiers
![Page 31: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/31.jpg)
Examplevoid functionName ( ) {
{int i ; }
…..}
![Page 32: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/32.jpg)
• Do not create variables with same name inside blocks, inside functions or inside bigger blocks
• Try to use separate variable names to avoid confusion
• Reuse of variables is valid
Identifiers Important Points
![Page 33: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/33.jpg)
# include < iostream.h > int i ;
File Scope
Global variable
![Page 34: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/34.jpg)
• Can be used anywhere in program• Can cause logical problems if same variable
name is used in local variable declarations
For good programming
• Try to minimize the use of global variables• Try to use local variables as far as possible
Global Variable
![Page 35: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/35.jpg)
• Global ScopeAnything identified or declared outside of any function is visible to all functions in that file
• Function level scopeDeclaring variables inside a function can be used in the whole function
• Block level scopeVariables or integers declared inside block are used inside block
Visibility of Identifiers
![Page 36: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/36.jpg)
for ( int i = 0 ; i < 10 ; i++ )
• It is block level scope declared in for loop
• When for is finished “ i ” no longer exists
Example: Block Scope
![Page 37: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/37.jpg)
#include < iostream.h >int i ; void f ( void ) ;main ( ){
i = 10 ;cout<< “ within main i = “ << i ;f ( ) ;
}
Example: Global Scope
![Page 38: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/38.jpg)
void f ( void ){
cout<< “ Inside function f , i =“ << i ;i = 20 ;
}
Example: Global Scope
![Page 39: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/39.jpg)
#include <iostream.h >int f ( int ) ;main ( ){
int i = 10 ;cout << “In main i = " << i ;f ( i ) ;cout << " Back in main, i = " << i ;
}
Example: Call by Value
s
![Page 40: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/40.jpg)
int f ( int i ){
cout << "In function f , i = " << i ;i *= 2 ;cout << "In function f , i is now = “ << i ;return i ;
}
Example: Call by Value
![Page 41: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/41.jpg)
double square ( double x ) {
return x * x ;}main ( ){
double number = 123.456 ;cout << “ The square of “ << number << “ is “<< square ( number ) ; cout << “ The current value of “ << number << “is “ << number ;
}
Example : Square of a Number
![Page 42: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/42.jpg)
#include < math.h >double sqrt ( double );
log10 , pow ( xy ) , sin , cos , tan …
Math.h
![Page 43: Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture…](https://reader031.vdocuments.mx/reader031/viewer/2022020305/5a4d1bdf7f8b9ab0599dea0f/html5/thumbnails/43.jpg)
Today we studied
• Functions• Variable Scope