What We Will Cover
Continuations
Homework Questions?
Famous Students
^ top
8.1: More About Functions
Learner Outcomes
At the end of the lesson the student will be able to:
- Describe local vs. global variables
- Write global constants
- Write
void functions
|
^ top
8.1.1: Review of Functions
Defining Functions
- To define a function, we use the following syntax:
returnType functionName(parameter1, ..., parametern) {
statements
}
- Where:
- returnType: the data type of the value returned
- functionName: the name you make up for the function
- parameterx: the input values, if any
- statements: the list of statements to execute when the function is called
- As an example, we wrote the function
add() which is called from main() :
int add(int a, int b) {
int sum = a + b;
return sum;
}
int main() {
//... other code omitted
int total = add(num1, num2);
//... other code omitted
}
Parameters
- In the parenthesis of the function are the parameters
- Parameters are the inputs to a function
- When we define a function, we want it to be reusable
- To make a function more reusable, we avoid hard-wiring important values
- Instead, we pass the key values by defining parameters
- When we call the function, we supply an argument for each parameter as shown below
Passing Arguments to Function Parameters
Returning Values
- The first word in the function signature is the return type
int add(int a, int b)
- The return type specifies the type of data the function outputs
- When we want the function to return a value we write a return statement
return sum;
- The returned value gets substituted for the function call in the calling code
- The flow of a function call is shown below
Function Call Flow
Check Yourself
What is output by the following program? (Do not run the code -- work it out by hand)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#include <iostream>
using namespace std;
int mystery(int param) {
cout << "param=" << param << endl;
param = param * 2;
return param;
}
int main() {
int num = 2;
cout << "At first, num=" << num << endl;
int result = mystery(num);
cout << "After calling, num=" << num << endl;
cout << "And result=" << result << endl;
return 0;
}
|
-
At first, num=2
param=2
After calling, num=4
And result=4
-
At first, num=2
param=4
After calling, num=4
And result=4
-
At first, num=2
param=2
After calling, num=2
And result=4
- None of these
^ top
8.1.2: Variable Scope and Global Constants
Program Using Local Variables and Global Constants
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#include <iostream>
using namespace std;
const double PI = 3.14159265;
double circleArea(double radius) {
double area = PI * radius * radius;
return area;
}
int main() {
cout << "Enter the radius of a circle: ";
double radius = 0.0;
cin >> radius;
double area = circleArea(radius);
cout << "The circle area is " << area << endl;
return 0;
}
|
Local Variables
Global Variables and Constants
Programming Style: No Global Variables
- Global variables are variables that are defined outside functions
- They are declared just like a global constant but without the
const keyword
- Unlike global constants, global variables are considered poor programming practice
- A global variable can be modified anywhere in your program, so it is hard to trace where it changes
- Global variables make programs more difficult to understand and maintain
- Do not use global variables in your programs for this course
- Instead, use function parameters and return statements to transfer data from one function to another
Group Activity
This program illustrates some of the hazards of using global variables. What is the output of the following code snippet if it was compiled and executed?
int x, y, z;
int fun(int a, int b) {
int x;
x = a + 2;
a = a * 3;
b = x + a;
return b;
}
int main( ) {
x = 1;
int y = 2;
z = 3;
y = fun(y, x);
cout << x << ' ' << y << ' ' << z << endl;
return 0;
}
|