Functions

Passing Data By Value

File: "func1.cpp"

#include <iostream>using namespace std;void changeme( int x1 ){ x1 = 100 ;}int main(){ int i1 = 10 ; changeme( i1 ) ; cout << "i1:" << i1 << endl ; return(1) ;}

$ ./a.exe

Let us see how the above function "changeme" works. We call the function "changeme" and pass the "i1" but we don't pass the variable and instead pass in the value which is 10. This value is passed to the variable "x1" in the function "changeme". We are in the function "changeme" and the variable "x1" is defined at this point and set to 10. The function "changeme" ends and the variable "x1" is out of scope. When we come back to the main function nothing has happened to the variable "i1" and the value of i1 is still 10 as shown in the output. There is a way to pass in a reference to the original variable and that is called pass by reference.

i1:10

File: "func2.cpp"

#include <iostream> using namespace std; // Function prototype. void divide(double, double); int main() { double num1, num2; cout << "Enter two numbers and I will divide the first\n"; cout << "number by the second number: "; cin >> num1 >> num2; divide(num1, num2); return 0; } void divide(double arg1, double arg2) { if (arg2 == 0.0) { cout << "Sorry, I cannot divide by zero.\n"; return; } cout << "The quotient is " << (arg1 / arg2) << endl; }

We have see how the main function returns an "int" and we do "return 0" to end the function. A function can have "void" to signify that the function does not return anything. We can still use a "return" statement to come out of the function as shown in the above program. Notice a function declaration does not need to have the parameter names defined in the function.

Exercises

1) Change the "func2.cpp" by adding a multiply function and the main calling that instead of the divide function. Place the "multiply" function at the bottom of the file and the declaration at the top.

2) Rewrite the prime problem and the power to use functions instead.

Default Arguments

A function can be defined so that the parameters take on default values.

Ex: "def1.cpp"

#include <iostream>using namespace std;void function1( int x1 , int x2= 5 , int x3=6 ){ cout << x1 << " " << x2 << " " << x3 << endl ;}int main(){ function1( 4 ) ; function1( 4 , 15 ) ; function1( 4 , 15 , 7) ; return(1) ;}

With default arguments we do not have to give all the values for the arguments when we call the function. in the example above we have defined the last 2 parameters x1 and x2 to take on the values 5 and 6 .

If we call

function1( 4 )

the parameter x1 will take on the value 4 and the parameter x2 takes on the value 5 and the parameter. If we provide a default value for one parameter then we need to provide values for the parameters to the right. We cannot have something like:

File: "def2.cpp"

#include <iostream>using namespace std;void function1( int x1 , int x2= 5 , int x3 ){ cout << x1 << " " << x2 << " " << x3 << endl ;}int main(){ function1( 4 ) ; function1( 4 , 15 ) ; function1( 4 , 15 , 7) ; return(1) ;}

$ g++ def2.cpp

def2.cpp:5:42: error: default argument missing for parameter 3 of ‘void function1(int, int, int)’

5 | void function1( int x1 , int x2= 5 , int x3 )

If the function has a declaration that we are using then we need to place the default arguments in the declaration and not in the definition.

File: "def3.cpp"

#include <iostream>using namespace std;void function1( int x1 , int x2= 5 , int x3=10 );int main(){ function1( 4 ) ; function1( 4 , 15 ) ; function1( 4 , 15 , 7) ; return(1) ;}void function1( int x1 , int x2 , int x3 ){ cout << x1 << " " << x2 << " " << x3 << endl ;}

Exercise

What does the below program print ?

#include <iostream>using namespace std;void function1( int x1=4 , int x2= 5 , int x3=6 ){ cout << x1 + x2 + x3 << endl ;}int main(){ function1( ) ; function1( 2 ) ; function1( 3 , 4 ) ; function1( 3 , 4 , 5) ; return(1) ;}

Static variables in functions.

File: "static1.cpp"

#include <iostream> using namespace std;void function1() { int localVar = 20 ; cout << "localVar:" << localVar << endl ; localVar++ ; }void function2() { static int staticVar = 20 ; cout << "staticVar:" << staticVar << endl ; staticVar++ ; }int main() { function1() ; function1() ; function1() ; function2() ; function2() ; function2() ; return 0; }

Output:

$ ./a.exe

localVar:20

localVar:20

localVar:20

staticVar:20

staticVar:21

staticVar:22

Overloading functions

We can have functions with the same name as long as the signature is different.

File: "over1.cpp"

// This program uses overloaded functions. #include <iostream> #include <iomanip> using namespace std; // Function prototypes int square(int); double square(double); int main() { int userInt; double userFloat; // Get an int and a double. cout << fixed << showpoint << setprecision(2); cout << "Enter an integer and a floating-point value: "; cin >> userInt >> userFloat; // Display their squares. cout << "Here are their squares: "; cout << square(userInt) << " and " << square(userFloat); return 0; } int square(int number) { return number * number; } //*************************************************************** // Definition of overloaded function square. * // This function uses a double parameter, number. It returns * // the square of number as a double. * //*************************************************************** double square(double number) { return number * number; }

Signature difference

The signatures of 2 functions have to be different in the parameters and not just in the return values.

int square(int number)

{

return number * number;

}

double square(int number)

{

return number * number;

}

The above code will not compile because, the functions only differ in the return value.