CS503 Lec7
CS503 Lec7
Functions (Cont.)
Void functions (do not have a data type)
- You can place void functions either before or after the function main (like
value-returning functions).
If you put it after the function main, place its prototype before the function main.
- Because void functions do not have a data type, they are not called in an
expression.
Thus, to call a void function, use the function name together with the actual
parameters (if any) in a stand-alone statement.
void Function Definition
void is a reserved word.
The formal parameter list may be empty but the empty parentheses are still needed.
Summary
// demonstrates function definition preceding function calls
#include <iostream>
using namespace std; //there’s no function declaration as its definition is written before the main function
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
EX2:
#include <iostream>
using namespace std;
They enable functions to manipulate different data each time they are called.
In the case of a value parameter, the value of the actual parameter is copied
into the memory cell of its corresponding formal parameter.
When a function is called, the value of the actual parameter is copied into
the corresponding formal parameter.
If the formal parameter is a value parameter, then after copying the value
of the actual parameter, there is no connection between the formal
parameter and actual parameter; that is, the formal parameter has its
own copy of the data.
Therefore, during program execution, the formal parameter manipulates
the data stored in its own memory space.
By writing & after the dataType in the formal parameter list of a function,
the variable following that dataType becomes a reference parameter.
//EX1: A program illustrates how a reference parameter works.
#include <iostream>
using namespace std;
void funcRefParam (int& num);
int main() {
int number = 6;
funcRefParam(number);
cout << "After calling the function, funcRefParam, number = " << number << endl;
//int a=number +100; cout<<"its value in an expression +100 is "<<a<<endl; //115
return 0; }
void funcRefParam (int& num) {
cout<<"In the function funcRefParam, before changing, num = " <<num << endl;
num = 15;
cout << "In the function funcRefParam, after changing, num = " << num << endl; }
Sample Run:
In the function funcRefParam, before changing, num = 6
In the function funcRefParam, after changing, num = 15
After calling the function funcRefParam, number = 15
EX2: Program takes a course score and determines its grade. 3 functions: main, getScore, printGrade
#include <iostream>
using namespace std;
void getScore (int& score);
void printGrade (int cScore);
int main(){ int courseScore;
getScore (courseScore);
printGrade (courseScore);
return 0;}
void getScore (int& score) { cout<<"Enter course score: "; cin>>score;
cout << "\n The course score is" << score << endl;}
void printGrade (int cScore) {
cout << "Your grade for the course is ";
if (cScore >= 90) cout << "A." << endl;
else if (cScore >= 80) cout << "B." << endl;
else if (cScore >= 70) cout << "C." << endl;
else if (cScore >= 60) cout << "D." << endl;
else cout << "F." << endl; }
EX2 (Cont.)
The formal parameter score of the function getScore is a reference parameter, the
address (that is, the memory location of the variable courseScore) passes to
score. Thus, both score and courseScore refer to the same memory location
(which is courseScore)
#include <iostream>
using namespace std;
void addFirst (int& first, int& second);
void doubleFirst (int one, int two);
void squareFirst (int& ref, int val);
int main() {
int num = 5;
cout << "Inside main: num = " << num<< endl;
addFirst (num, num);
cout << "Inside main after addFirst:"<< " num = " << num << endl;
A static variable
A variable for which the memory remains allocated as long as the program
executes.
The syntax for declaring a static variable is:
EX:
static int x; //declares x to be a static variable of type int.
EX:
Sample run:
#include <iostream>
using namespace std;
void test();
int main() {
int count;
for (count = 1; count <= 5; count++) test() ;
return 0; }
void test()
{
static int x = 0;
int y = 10;
x = x + 2;
y = y + 1;
cout << "Inside the function test, x = " << x << " and y = "<< y << endl;
EX (Cont.)
Memory for the variable y is allocated every time the function test is called
and deallocated when the function exits. Thus, every time the function
test is called, it prints the same value for y.
The subsequent التاليةcalls of the function test use the current value of x.
Questions Samples:
1. Consider the following program:
#include <iostream>
using namespace std;
void func1();
void func2();
int main(){ int num;
cout << "Enter 1 or 2: "; cin >> num;
cout << " \n Take ";
if (num == 1) func1();
else if (num == 2) func2();
else cout<<"Invalid input. You must enter1or2 \n"; return 0; }
3. Write the definition of a void function that takes as input two decimal
numbers. If the first number is nonzero, it outputs second number
divided by the first number; otherwise, it outputs a message
indicating that the second number cannot be divided by the first no.
cause the first number is 0