Professional Documents
Culture Documents
Week 3
Week 3
Week 3
Jeanine Ingber 1
Functions
• Pre-defined
– standard libraries
• User defined
Jeanine Ingber 2
Pre-defined Functions - Example
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double angle;
cout << “input angle in radians: “;
cin >> angle;
cout << “\nthe sine of the angle is “ << sin(angle) << endl;
return 0;
}//end main
Jeanine Ingber 3
Self Test-what is the output?
cast functions - return new data type, no affect on argument
#include <iostream>
using namespace std;
int main()
{
int x=9, y=2;
cout << x/y << endl;
cout << double(x)/double(y) << endl;
cout << double(x/y) << endl;
cout << double(x)/y << endl;
return 0;
}//end main Jeanine Ingber 4
Programmer Defined Functions
Terminology
• Function Prototype
– describes how a function is called
• Function Call
• Function Arguments
– used in the function call
• Function Definition
– function header
– function body
• Formal Parameters
– used in function definition
• Formal parameters must agree with arguments in order, number
and data type, but not in name.
Jeanine Ingber 5
Programmer Defined Functions
• Defined to
– return a single value to the calling function
(parameters are pass by value)
– perform a task (parameters are pass by value)
– change the value of variables in the calling
function (parameters are pass by reference)
Jeanine Ingber 6
Value Returning Functions
• Function returns a single value to the
calling program
• Function header declares the type of value
to be returned
• A return statement is required in the body
of the function
• parameters should be pass by value
Jeanine Ingber 7
Example - factorial function
/*function definition
n! = n*(n-1)*(n-2)*…*1, 0! Is 1 by definition
- fact returns n!
assumes n is non-negative integer */
Jeanine Ingber 11
void Functions
• A void function may be called to
• perform a particular task (clear the screen)
• modify data
• perform input and output
• A void function does not return a value to the calling
program
• return statement is optional
• if a return statement is used, it must not return a value
• parameters my be any combination of pass by value or pass
by reference
Jeanine Ingber 12
Example of void function
//output formatted date
//function definition
void print_date(int mo, int day, int year) //function
header
{
cout << mo << ‘/’ << day << ‘/’ << year << endl;
return; //return is optional
}//end print_date
Jeanine Ingber 13
Parameter Passing - pass by
value
• Pass by value
– the default in C++ (except when passing arrays)
as arguments to functions
– formal parameter receives the value of the
argument
– changes to the formal parameter do not affect
the argument
Jeanine Ingber 14
Parameter Passing - pass by reference
• Pass by reference
– append an & to the parameter data type
• void get_date(int& day, int& mo, int& year)
– formal parameter receives the address of the
argument
– any changes to the formal parameter directly
change the value of the argument
Jeanine Ingber 15
Example - pass by reference
//Function swap interchanges the values of two variables
//function definition
void swap(double& x, double& y) //function header
{
double temp; //local variable temp
temp = x;
x=y;
y=temp;
return; //optional return statement
}//end swap
Jeanine Ingber 16
Example - pass by reference
#include <iostream>
using namespace std;
void swap(double&, double&); //function prototype
int main()
{
double x=5, y=10;
swap(x,y); //function call, x and y are arguments
cout >> “x = “ << x << ‘,’ << “y= “ << y << endl;
return 0;
}//end main
void swap(double& n1, double& n2)
{
.
.
.
Jeanine Ingber 17