Ch02 Functions
Ch02 Functions
November 2024
C++ comes with libraries of predefined functions that you can use in
your programs.
The value the function starts out with is called its argument.
The value it computes is called the value returned.
Functions may have more than one argument, but no function has
more than one value returned.
theRoot = sqrt(9.0);
Example:
The value returned by sqrt is of type double.
bonus = sqrt(sales)/10;
//sales and bonus are variables that would normally be of type double.
cout << "The side of a square with area " << area << " is " << sqrt(area);
SYNTAX
Function Name(Argument List)
For Example:
double totalCost(int numberPar, double pricePar);
For Example:
1 int square(int); //This is a function signature / prototype / declarati
2
Example
//The following function computes and returns the area of
double area(double radius){
const double PI = 3.14;
return (PI * radius * radius);
}
The return statement without any value is typically used to exit the
function early
C++ does not allow nested functions
The definition of one function cannot be included in the body of
another function
A function definition must agree in return type and parameter list
with its prototype
The body of the function is always enclosed in braces, even when it
consists of only one statement
Example
return (subtotal + subtotal * TAX_RATE);
When return statement is executed, the value of the expression is returned as the value of
the function call.
For Example:
1 void printNumber(int num) {
2 cout << "number is " << num << endl;
3 }
4 int main() {
5 printNumber(4); // number is 4
6 return 0;
7 }
For Example:
1 void printLarger(int x, int y) {
2 if (x > y)
3 {
4 cout << "The larger number is " << x << endl;
5 return ;
6 }
7 cout << "The larger number is " << y << endl;
8 }
9
10 int main() {
11 printLarger(35, 29); // outputs: The larger number is 35
12 return 0;
13 }
1 # include <iostream>
2 using namespace std;
3 double Celsius_to_Fahr(double); //Function Prototype
4 int main()
5 {
6 double temp,result;
7 cout<<"Enter the temperature"<<endl;
8 cin>>temp;
9 result= Celsius_to_Fahr(temp);
10 cout<<"The corresponding Fahrenheit is "<<result<<endl;
11 }
12 double Celsius_to_Fahr(double Celsius)
13 {
14 double temp; // Declare variable
15 temp = (9.0/5.0)*Celsius + 32; // Convert
16 return temp;
17 }
Variables declared within the body of the function are local variables
When the function returns, the underlinelocal variables are no longer
available.
Variables declared within a block are scoped to that block - Local to
that block
▶ they can be accessed only within that block
▶ ”go out of existence” when that block ends.
1 # include <iostream>
2 using namespace std;
3 void myFunction(); // prototype
4 int x = 5, y = 7; // global variables
5 int main()
6 {
7 cout << "x from main: " << x << "\n";
8 cout << "y from main: " << y << "\n\n";
9 myFunction();
10 cout << "Back from myFunction!\n\n";
11 cout << "x from main: " << x << "\n";
12 cout << "y from main: " << y << "\n";
13 return 0;
14 }
15
16 void myFunction()
17 {
18 int y = 10;
19 cout << "x from myFunction: " << x << "\n";
20 cout << "y from myFunction: " << y << "\n\n";
21 }
1 # include <iostream>
2 using namespace std;
3 void myFunction(); // prototype
4 int x = 5, y = 7; // global variables Output
5 int main()
6 {
7 cout << "x from main: " << x << "\n";
8 cout << "y from main: " << y << "\n\n";
9 myFunction();
10 cout << "Back from myFunction!\n\n";
11 cout << "x from main: " << x << "\n";
12 cout << "y from main: " << y << "\n";
13 return 0;
14 }
15
16 void myFunction()
17 {
18 int y = 10;
19 cout << "x from myFunction: " << x << "\n";
20 cout << "y from myFunction: " << y << "\n\n";
21 }
In a function call where the function has more than one default
parameter and a value to a default parameter is not specified, you
have to omit all of the arguments to its right
Default values can be constants, global variables, or function calls
The caller has the option of specifying a value other than the default
for any default parameter
You cannot assign a constant value as a default value to a reference
parameter
1 # include <iostream>
2 using namespace std;
3 int AreaCube(int length, int width = 25, int height = 1);
4
5 int main()
6 {
7 int length = 100;
8 int width = 50;
9 int height = 2;
10 int area;
11 area = AreaCube(length, width, height);
12 cout << "First area equals: " << area << "\n";
13 area = AreaCube(length, width);
14 cout << "Second time area equals: " << area << "\n";
15 area = AreaCube(length);
16 cout << "Third time area equals: " << area << "\n";
17 return 0;
18 }
19
20 int AreaCube(int length, int width, int height)
21 {
22 return (length * width * height);
23 }
Kibret Z, Mayet G., Belayneh M. Chapter 01 - Functions in C++ November 2024 32 / 45
Example: Functions with Default Parameters
1 # include <iostream>
2 using namespace std;
3 int AreaCube(int length, int width = 25, int height = 1);
4
5 int main()
6 {
7 int length = 100;
8 int width = 50;
9 int height = 2;
10 int area;
11 area = AreaCube(length, width, height);
12 cout << "First area equals: " << area << "\n";
13 area = AreaCube(length, width);
14 cout << "Second time area equals: " << area << "\n";
15 area = AreaCube(length);
16 cout << "Third time area equals: " << area << "\n";
17 return 0;
18 }
19
20 int AreaCube(int length, int width, int height)
21 {
22 return (length * width * height);
23 }
Kibret Z, Mayet G., Belayneh M. Chapter 01 - Functions in C++ November 2024 33 / 45
Parameter Passing
1 # include <iostream>
2 using namespace std;
3 void swap(int x, int y);
4
5 int main()
6 {
7 int x = 5, y = 10;
8
9 cout << "Main. Before swap, x: " << x << " y: " <<y<< "\n";
10 swap(x,y);
11 cout << "Main. After swap, x: " << x << " y: " <<y << "\n";
12 return 0;
13 }
14
15 void swap (int x, int y)
16 {
17 cout << "Swap. Before swap, x: " << x << " y: " <<y<< "\n";
18 int temp = x;
19 x = y;
20 y = temp;
21 cout << "Swap. After swap, x: " << x << " y: " << y << "\n";
22 }
# include <iostream>
using namespace std;
void swap(int x, int y);
int main()
{
int x = 5, y = 10;
cout << "Main. Before swap, x: " << x << " y: " <<y<< "\n";
swap(x,y);
cout << "Main. After swap, x: " << x << " y: " <<y << "\n";
return 0;
}
1 # include <iostream>
2 using namespace std;
3 void swap(int &x, int &y);
4
5 int main()
6 {
7 int x = 250, y = 750;
8
9 cout << "Main. Before swap, x: " << x << " y: " <<y<< "\n";
10 swap(x,y);
11 cout << "Main. After swap, x: " << x << " y: " <<y << "\n";
12 return 0;
13 }
14
15 void swap (int &i, int &j)
16 {
17 cout << "Swap. Before swap, i: " << i << " j: " <<j<< "\n";
18 int temp = i;
19 i = j;
20 j = temp;
21 cout << "Swap. After swap, i: " << i << " j: " << j << "\n";
22 }
int main()
{
int x = 250, y = 750;
cout << "Main. Before swap, x: " << x << " y: " <<y<< "\n";
swap(x,y);
cout << "Main. After swap, x: " << x << " y: " <<y << "\n";
return 0;
}
1 # include <iostream>
2 using namespace std;
3 unsigned long factorial(unsigned long); //prototype
4 int main()
5 {
6 int num;
7 cout<<"Enter a positive integer:";
8 cin>>num;
9 cout<<"Factorial of "<<num<<" is "<<factorial(num);
10 return 0;
11 }
12
13 //Function definition for factorial
14 unsigned long factorial(unsigned long n)
15 {
16 unsigned long fact = 1;
17 For( int i = 1; i <= n; i++)
18 fact*=i;
19 }
1 # include <iostream>
2 using namespace std;
3 unsigned long factorial(unsigned long); //prototype
4 int main()
5 {
6 int num;
7 cout<<"Enter a positive integer:";
8 cin>>num;
9 cout<<"Factorial of "<<num<<" is "<<factorial(num);
10 return 0;
11 }
12
13 //Function definition for factorial
14 unsigned long factorial(unsigned long n)
15 {
16 if ( n <= 1) //the base case
17 return 1;
18 else
19 return n * factorial (n - 1);
20
21 }
1 # include <iostream>
2 using namespace std;
3 double average(double, double); //prototype
4 //Returns the average of the two numbers
5
6 double average(double, double, double); //prototype
7 //Returns the average of the three numbers
8 int main()
9 {
10 cout<<"The average of 2.0, 2.5, and 3.0 is "<< ave(2.0, 2.5, 3.0) <<endl;
11
12 cout<<"The average of 4.5 and 5.5 is "<< ave(4.5, 5.5) <<endl;
13 return 0;
14 }
15 double average(double x, double y)
16 {
17 return (x + y) / 2.0;
18 }
19 double average(double a, double b, double c)
20 {
21 return (a + b + c) / 3.0;
22 }