Professional Documents
Culture Documents
1
Functions
• A function is a block of instructions that is
executed when it is called from some other
point of the program.
• Advantages
– Reusability
– Data Abstraction
– Modularity
2
Predefined Functions
Libraries
3
Predefined Functions
Libraries
5
A Function call
//Computes the size of a dog house that can be purchased
//given the user’s budget.
#include <stdio.h>
#include <math.h>
int main( )
{
const double COST_PER_SQ_FT = 10.50;
double budget, area, length_side;
printf( "Enter the amount budgeted for your dog house $“);
scanf( “%f”, &budget);
area = budget/COST_PER_SQ_FT;
length_side = sqrt(area); // the function call
6
A Function call
return 0;
}
7
Programmer Defined Functions
function prototypes
• A function prototype tells you all the information you need to
call the function. A prototype of a function (or its definition)
must appear in your code prior to any call to the function.
• Syntax: Don’t forget the semicolon
– Type_of_returned_value Function_Name(Parameter_list);
– Place prototype comment here.
– Parameter_list is a comma separated list of parameter
definitions:
type_1 param_1, type_2 param_2, …. type_N param_N
• Example:
double total_weight(int number, double weight_of_one);
// Returns total weight of number of items that
// each weigh weight_of_one
8
Programmer Defined Functions
#include <stdio.h>
double total_cost(int number_par, double price_par);
//Computes the total cost, including 5% sales tax,
//on number_par items at a cost of price_par each.
int main( )
{
double price, bill;
int number;
printf( "Enter the number of items purchased: “);
scanf( “%d”, &number);
printf( "Enter the price per item $“);
scanf(“%d”, &price);
bill = total_cost(number, price); The function call
printf(“%d items at $ %d each.\n“, number, price);
printf( "Final bill, including tax, is $%d\n“, bill);
9
return 0;
3.3 Programmer Defined Functions
Display 3.3 A function Definition (Slide 2 of 2)
10
Programmer Defined Functions
Call-by-value Parameters
Consider the function call:
bill = total_cost(number, price);
11
Programmer Defined Functions
Alternate form for Function Prototypes
• The parameter names are not required:
double total_cost(int number, double price);
• It is permissible to write:
double total_cost(int, double );
12
PITFALL
Arguments in the wrong order
• When a function is called, C++ substitutes the first argument
given in the call for the first parameter in the definition, the
second argument for the second parameter, and so on.
• There is no check for reasonableness. The only things checked
are: i) that there is agreement of argument type with parameter
type and ii) that the number of arguments agrees with the
number of parameters.
• If you do not put correct arguments in call in the correct order,
C++ will happily assign the “wrong” arguments to the “right”
parameters.
13
Programmer Defined Functions
Summary of Syntax for a Function that Returns a Value.
Function Prototype:
Type_Returned Function_Name(Parameter_List);
Prototype Comment
function header
Function Definitions
Type_Returned Function_Name(Parameter_List)
{
Declaration_1
Declaration_2
... Must include one or
Declaration_Last; more return statements.
body Executable_1;
Executable_2;
...
Executable_Last
}
14
Functions with no return value
General format
void function_name ([parameters])
{
<function_body>
}
Declaring Void Functions
• Similar to functions returning a value
• Return type specified as "void"
• Example:
– Function declaration/prototype:
void showResults( double fDegrees,
double cDegrees);
• Return-type is "void"
• Nothing is returned
Declaring Void Functions
• Function definition:
void showResults(double fDegrees,
double cDegrees)
{
statements;
statements;
}
• Notice: no return statement
Calling Void Functions
• Same as calling predefined void functions
• From some other function, like main():
– showResults(degreesF, degreesC);
– showResults(32.5, 0.3);
• Notice no assignment, since no
value returned
• Actual arguments (degreesF, degreesC)
– Passed to function
– Function is called to "do it’s job" with the
data passed in
Returning more than one value
#include <stdio.h>
void prevnext (int x, int &prev, int &next)
{ prev = x-1;
next = x + 1;}
int main {int x = 100,y,z;
prevnext(x,y,z);
printf(”previous %d next %d”,y, z);
return 0;} 20
Default Values in arguments
#include<stdio.h>
int divide(int a, int b=2) {
int r; r=a/b; return(r);}
int main(){
printf(“%d\n”,divide(2));
printf((“%d\n”, divide(20,4));
return 0; }
21
Overloading Function Names
• C++ distinguishes two functions by examining the
function name and the argument list for number
and type of arguments.
• The function that is chosen is the function with
the same number of parameters as the number of
arguments and and that matches the types of the
parameter list sufficiently well.
• This means you do not have to generate names for
functions that have very much the same task, but
have different types.
22
Overloading Function Names
double ave(double n1, double n2) Both these functions have the
{ same name, but have
parameter
return ((n1 + n2)/2.0); lists that are have different
} numbers of parameters.
24
Overloading Function Names
Automatic Type Conversion
25
Overloading Function Names
return 0;
}
28
Function Template
• Is a single, complete function that serves as
a model for a family of functions
• Generic, flexible
• Data type of the arguments of the function
follow the data type of the actual value that
is passed unto it.
29
Function Template
#include <stdio.h>
template<class T>
void showabs(T number)
{if (number<0)
number = -number;
printf("the absolute value of the number is"
<<number<<endl;}
int main ()
{ int num1 = -4;
float num2 = -4.23;
double num3= -4.23456;
showabs(num1);
showabs(num2);
showabs(num3); return 0; }
30
Reference
• Slides by David B Teague, Western
Carolina University,