C++ Functions
How do I use functions?
refer to it
pass parameters and get results
value, reference, pointer
default arguments
Can I have same variable names in one program?
scoping rules
reference to variables
Why Functions
use other people's code
use your own code multiple times in a similar fashion
What kinds of functions are available?
library functions
rand(), srand(), pow(x,y)
C style header file stdio.h, stdlib.h, C++ style iostream, cstdlib
standard template library (STL)
user created functions
Interaction with the calling program
Some function does not return anything, e.g. print functions. Use void.
Some function return 1 thing, e.g. calculate parallelResistance. Use proper return type.
Sometimes you want more than one things returned from a function, e.g. min/max/average calculation. In that case, you can write multiple routines, or use call by reference.
Function Overloading
What if I want the same function with different types of input? compare two integer, two float, two character, two strings?
function overloading
how does compiler know which one to use?
Do I need to write one version for integer, one for float, one for char, one for string?
function template
What are the standard template library functions?
How does computer execute function calls?
stack operation
push and pop - program counter, parameters, local variables
scope
Default Parameters
What if I want the same function with variable number of input? average of 2, 3, 4 or 5 integers?
#include <iostream>
#include <iomanip>
using namespace std;
int num=46 ; // global variable with initialization
int xyz;
int add10(int); // function declaration, call by value
int add10ref(int &); // call by reference
int add10(float);
void main() {
int num;
// cout << num; // uninitialized variable error
num = 3;
cout << num;
xyz = 8;
cout << xyz << ::xyz;
cout << ::num; // global scope
::num = 123;
cout << add10(num);
cout << num << endl;
cout << add10ref(num);
cout << num << endl; // notice any difference between these two calls?
float f=1.2;
cout << add10(f); // add10 has two versions... function overloading
}
int add10(int i) {
i=i+10;
return i;
}
int add10ref(int &i) {
i = i+77;
return 0;
}
int add10(float i) {
return 22;
}
What to learn from this practice....
scoping of variables, how to declare them and use them
function parameters, return type and function prototype
function overload
add10 has two definitions: one takes int, the other takes float. Same function name is "overloaded" with two maybe different functionalities.
difference between call by value and call by reference
add10(num) and add10ref(num) are called the same way from main. However, num's value is changed by add10ref, but not by add10.
call by pointer (hmm, maybe later)
More Function practice
(Celsius andFahrenheitTemperatures) Implement the following integer functions:
a) Function celsius returns the Celsius equivalent of a Fahrenheit temperature.
b) Function fahrenheit returns the Fahrenheit equivalent of a Celsius temperature.
c) Use these functions to write a program that prints charts showing the Fahrenheit equiv-
alents of all Celsius temperatures from 0 to 1 00 degrees, and the Celsius equivalents of
all Fahrenheit temperatures from 32 to 21 2 degrees. Print the outputs in a neat tabular
format that minimizes the number of lines of output while remaining readable.