Previous Lecture Lecture 4 Next Lecture

Lecture 4, Tue 07/10

Functions, print vs. return, Memory Stack

Functions

Function Signatures

Example of Overloading Functions

void printAreaOfSquare(int area); // OK, first declaration of this function
double printAreaOfSquare(int area); // ERROR, only return type differs
void printAreaOfSquare(double area); // OK, parameters are different
void printAreaOfSquare(int length, double width); // OK
void printAreaOfSquare(int width, double length); // ERROR, same parameter types

Function Declarations

In C++, a function declaration must occur BEFORE they are used. * We can write the method after it is used, we just need to declare it.

Example: Simple Function Definition

#include <iostream>

using namespace std;

int areaOfSquare(int length); // declaration

int main() {
	int result = areaOfSquare(20); // call
	cout << result << endl;
	return 0;
}

int areaOfSquare(int length) { // definition
	return length * length;
}
// OK. Function declaration happens before it was used

-----------
#include <iostream>

using namespace std;

int main() {
	int result = areaOfSquare(20); // call
	cout << result << endl;
	return 0;
}

int areaOfSquare(int length) { // definition
	return length * length;
}
// ERROR! use of undeclared identifier 'areaOfSquare'

Return vs. Print

Example of printing, but not returning

void printAreaOfSquare(int area) {
	cout << “Area of Square: “ << area << endl;
	return; // return not necessary, will exit function when reached.
}

Example: Matching function calls to overloaded functions

void f(int x) { cout << "f(int x)" << endl; }
void f(double x) { cout << "f(double x)" << endl; }

void g(int x) { cout << "g(int x)" << endl; }

void h(double x) { cout << "h(double x)" << endl; }

int main() {
	f(5); 		// f(int x) 
 	f(5.0);		// f(double x)
 	g(10.1);	// g(int x)
 	h(10);		// h(double x)
}

Memory Stack

Example

#include <iostream>

using namespace std;

int doubleValue(int x) {
	return 2 * x;
}

int quadrupleValue(int x) {
	return double(x) + double(x); 
}

int main() {
	int result = quadrupleValue(4);
	cout << result << endl;
	return 0;
}
|                       |
|-----------------------|
| int main()            |
|_______________________|
|                       |
|-----------------------|
| int quadrupleValue(4) |
|-----------------------|
| int main()            |
|_______________________|
|                       |
|-----------------------|
| int double(4)         |
|-----------------------|
| int quadrupleValue(4) |
|-----------------------|
| int main()            |
|_______________________|
|                       |
|-----------------------|
| int quadrupleValue(4) |
|-----------------------|
| int main()            |
|_______________________|
|                       |
|-----------------------|
| int double(4)         |
|-----------------------|
| int quadrupleValue(4) |
|-----------------------|
| int main()            |
|_______________________|
|                       |
|-----------------------|
| int quadrupleValue(4) |
|-----------------------|
| int main()            |
|_______________________|
|                       |
|-----------------------|
| int main()            |
|_______________________|