1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006
1
Lecture 16:User-Definded function I
Introduction to Computer Science
Spring 2006
2
3
Predefined Functions To use these functions you need to:
Include the correct header file Know the name of the function Know the number of parameters, if any Know the data type of each parameter Know the data type of the value
computed by the function, called the type of the function
4
Predefined Functions : Value-Returning Functions Because the value returned by a value-
returning function is unique, we must:
Save the value for further calculation
Use the value in some calculation
Print the value
A value-returning function is used in an assignment or in an output statement
5
Use of standard function pow#include <iostream>
using namespace std;int main(){
double u,v;double result;u = 4.2;v = 3.0;
return 0;}
#include <cmath>
result=pow(u, v);
cout << u << " to the power of " << v << " = " << pow(u, v) << endl;
cout << "u = " << u << endl;
u = u + pow(3, 3);
6
User-Defined Functions Void functions: do not have a data
type Value-returning functions: have a
data type
7
#include <iostream>using namespace std;
int main(){ int i, j; int k;
cin>>i>>j;
if (i > j) { k = i; } else { k = j; } cout << "The max is " << k << endl;
return 0;}
#include <iostream>using namespace std;
/* Function Declarations */int FindMax(int n1, int n2);void PrintMax(int someNumber);
int main(){ int i, j; int k; cin>>i>>j;
k = FindMax(i,j); PrintMax(k); // Prints Max Value
return 0;}
/* Function Definitions */int FindMax(int n1, int n2){ if (n1 > n2) { return n1; } else { return n2; }}
/* Function Definitions */void PrintMax(int someNumber){ cout << "The max is " << someNumber << endl;}
Value-returning functions
Void functions
8
Value-Returning Functions Because the value returned by a value-
returning function is unique, we must:
Save the value for further calculation
Use the value in some calculation
Print the value
A value-returning function is used in an assignment or in an output statement
9
#include <iostream>using namespace std;
/* Function Declarations */int FindMax(int n1, int n2);
int main(){ int i, j; int k; cin>>i>>j;
k = FindMax(i,j);
return 0;}
/* Function Definitions */int FindMax(int n1, int n2){ if (n1 > n2) { return n1; } else { return n2; }}
cout << "The max is "<< FindMax(i,j) << endl;k =FindMax(i, j) + 1;
10
Value-Returning Functions: function definition
Properties that form the function definition: Heading:
1. Name of the function
2. Number of parameters
3. Data type of each parameter
4. Function Type (type of the value returned by the function)
Body:
1. Code required to accomplish the task (the body of the function)
The syntax of the function definition is:
functionType functionName(formal parameter list){
statements}
The syntax of the formal parameter list is:
dataType identifier, dataType identifier, ...
11
Value-Returning Functions: function definition
int FindMax(int n1, int n2){ if (n1 > n2) { return n1; } else { return n2; }}
Function Heading
int FindMax(int n1, int n2)
Function Body
Function Type
Function Name
Formal parameter list
12
Value-Returning Functions: call a value-returning function To call a value-returning function:
Use its name, with the actual parameters (if any) in parentheses
The syntax for a function call is:
functionName(actual parameter list)
The syntax for the actual parameter list is:
expression or variable,expression or variable, ...
13
#include <iostream>using namespace std;
/* Function Declarations */int FindMax(int n1, int n2);
int main(){ int i, j; int k; cin>>i>>j;
k = FindMax( i, j ) ;
return 0;}
/* Function Definitions */int FindMax( int n1, int n2 ){ if (n1 > n2) { return n1; } else { return n2; }}
Actual parameter list
Function Name
Call a Value-returning Function
14
Function Prototype Function Prototype: function heading without the body
of the function The syntax is:
functionType functionName(parameter list); It is not necessary to specify the variable name in the
parameter list The data type of each parameter must be specified
Function prototypes appear before any function definition
The compiler translates these first
The compiler can then correctly translate a function call
15
#include <iostream>using namespace std;
/* Function Declarations */int FindMax(int n1, int n2);
int main(){ int i, j; int k; cin>>i>>j;
k = FindMax( i, j ) ;
return 0;}
/* Function Definitions */int FindMax( int n1, int n2 ){ if (n1 > n2) { return n1; } else { return n2; }}
Function Prototype
16
Value-Returning Functions (continued)
Formal Parameter: variable declared in the heading
Actual Parameter: variable or expression listed in a call to a function
There is a one-to-one correspondence between actual and formal parameters
17
#include <iostream>using namespace std;
/* Function Declarations */int FindMax(int n1, int n2);
int main(){ int i, j; int k; cin>>i>>j;
k = FindMax( i, j ) ;
return 0;}
/* Function Definitions */int FindMax( int n1, int n2 ){ if (n1 > n2) { return n1; } else { return n2; }}
Actual parameter list
Formal parameter list
18
Functions The formal parameter list can be empty If the formal parameter list is empty
Parentheses are still needed Function heading of the value-returning
function takes either of the following forms: functionType functionName() functionType functionName(void)
In a function call the actual parameter is empty
A call to a value-returning function with an empty formal parameter list is: functionName()
19
Value-Returning Functions :The return Statement
Once the function computes the value, the function returns the value via the return statement
The syntax of the return statement is:return expression or variable;
When a return statement executes Function immediately terminates Control goes back to the caller
When a return statement executes in the function main, the program terminates
20
Flow of Execution Execution always begins at
The first statement in the function main no matter where main is placed in the program
Other functions are executed only when they are called
A function call statement results in
Transfer of control to the first statement in the body of the called function
After the last statement of the called function is executed
Control is passed back to the point immediately following the function call
A value-returning function returns a value
After executing the function
The value that the function returns replaces the function call statement
21
#include <iostream>using namespace std;
/* Function Declarations */int FindMax(int n1, int n2);
int main(){ int i, j; int k; cin>>i>>j;
k = FindMax( i, j ) ;
return 0;}
/* Function Definitions */int FindMax( int n1, int n2 ){ if (n1 > n2) { return n1; } else { return n2; }}
22
End of lecture 16
Thank you!