04 - Functions
04 - Functions
Programming Technique I
(SCSJ1013)
Modular Programming
x=4 sqrt(x) 2
Math Functions
Function Purpose
sin Sine
cos Cosine
tan Tangent
sqrt Square root
log Natural (e) log
abs Absolute value (takes and returns an int)
More Mathematical Functions
• Functions:
tolower: if char argument is uppercase letter, return
lowercase equivalent; otherwise, return input unchanged
char ch1 = 'H';
char ch2 = 'e';
char ch3 = '!';
cout << tolower(ch1); // displays 'h'
cout << tolower(ch2); // displays 'e'
cout << tolower(ch3); // displays '!'
Example – Character Case
Conversion
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char input[15];
cout<<"Enter a name ";
cin>>input;
for(int i=0;input[i] != '\0';i++)
input[i]=toupper(input[i]);
cout<<"The name in upper case is:" << input;
return 0;
}
Character Manipulating
Functions
Exercise 02
H i t h e r e ! \0
Review of the Internal Storage of
C-Strings
• Array of chars can be used to define storage for
string:
const int SIZE = 20;
char city[SIZE];
• Leave room for NULL at end
• Can enter a value using cin or >>
– Input is whitespace-terminated
– No check to see if enough space
• For input containing whitespace, and to control
amount of input, use cin.getline()
C-Strings Manipulation Functions
• Functions:
– strlen(str): returns length of C-string str
• Functions:
– strcpy(str1, str2): copies str2 to str1
• Functions:
– strstr(str1, str2): finds the first occurrence of str2 in
str1. Returns a pointer to match, or NULL if no match.
char river[] = "Wabash";
char word[] = "aba";
cout << strstr(state, word);
// displays "abash"
Comparing C-Strings
Main Program
Function
definition
Function call
Function Definition
• Definition includes:
– return type: data type of the value that function returns to
the part of the program that calls it
– name: name of the function. Function names follow same
rules as variables
– parameter list: variables containing values passed to the
function
– body: statements that perform the function’s task,
enclosed in {}
Function Definition (cont.)
Note: The line that reads int main() is the function header.
Function Return Type
Start
displayMessage
End
The Pseudo Code
• Start • displayMessage:
• Print “Hello from main” – Print “Hello from the function
displayMessage”
• call displayMessage
• Print “Back in function main
again”
• End
The Structure Chart
main_program
displayMessage
Calling a Function - Example
Function
definition
Function call
Flow of Control in Program 6-1
User-Defined Functions:
Example 2
01 #include <iostream>
02 #include <cmath>
03 using namespace std;
04
05 float distance(float x, float y)
06 { float dist;
07 dist = sqrt(x * x + y * y);
08 return dist;
09 }
10
11 void main()
12 {
13 float x,y,dist;
14 cout << "Testing function distance(x,y)" << endl;
15 cout << "Enter values for x and y: ";
16 cin >> x >> y;
17 dist = distance(x,y);
18 cout << "Distance of (" << x << ',' << y << ") from origin is
" << dist << endl << "Tested" << endl;
19 }
Calling Functions
• main can call any number of functions
• Functions can call other functions
• Compiler must know the following about a function before it
is called:
– name
– return type
– number of parameters
– data type of each parameter
In-Class Exercise
Main Program
first() second()
Prototype Notes
main_program
printHi
User-Defined Functions: Functions with
Parameter and No Return Values (2)
01 #include <iostream>
02 using namespace std;
03
04 void displayValue(int);
05
06 void main()
07 {
08 cout<<"Passing number 5 to displayValue\n";
09 displayValue(5);
10 cout<<"Back in main\n";
11 }
12
13 void displayValue(int n)
14 {
15 cout<<"The value is " << n << endl;
16 }
Other Parameter Terminology
val num
5 5
argument in parameter in
calling function evenOrOdd function
double x;
x = pow(2.0, 10.0);
Returning a Value from a Function
Return Type
Program 6-11(Continued)
Return to
called
function
Returning a Value From a Function
Returning a Value From a Function
main_program
value1
total
value2
sum
Returning a Value From a Function
#include <iostream>
using namespace std;
int j = 8;
int main()
{
int i=0;
cout<<"i: "<<i<<endl;
cout<<"j: "<<j<<endl;
system("pause");
return 0;
}
• Identify global variables, local variables and static local variables in
the following program. What is the output?
#include <iostream>
using namespace std;
int j = 40;
void p()
{ int i=5;
static int j=5;
i++;
j++;
cout<<"i: "<<i<<endl;
cout<<"j: "<<j<<endl;
}
int main()
{ p();
p();
return 0;}
Using Reference Variables as
Parameters
• A mechanism that allows a function to work with the
original argument from the function call, not a copy of
the argument
• Allows the function to modify values stored in the calling
environment
• Provides a way for the function to ‘return’ more than one
value
Passing by Reference
(Program Continues)
Default Arguments – Example
Program 6-23 (Continued)
Default Arguments
a) testDefaultParam(6);
b) testDefaultParam(3,4);
c) testDefaultParam(3,4.5);
d) testDefaultParam(3,4, 5.5);
e) testDefaultParam(3.4);
In-Class Exercise
• Write a function prototype and function header for a function
called compute. The function should have 3 parameters: an int,
a double and a long. The int parameter should have a default
argument of 5, and the long parameter should have a default
argument of 65536. The double parameter should have no default
arguments. The parameters no necessarily in the order.
The overloaded
functions have different
parameter lists
Passing a double
• Example:
exit(0);