130 likes | 247 Views
Functions. Declarations and Definitions Function Declarations Function Arguments Function Return Types Overloaded Functions Function Templates. File A.C #include "A.h" int day_of_year=31; double f(double x) { return 1.0/x; }. Declarations and Definitions. Trans. Unit A.C
E N D
Functions • Declarations and Definitions • Function Declarations • Function Arguments • Function Return Types • Overloaded Functions • Function Templates
File A.C #include "A.h" int day_of_year=31; double f(double x) { return 1.0/x; } Declarations and Definitions • Trans. Unit A.C • extern int day_of_year; • extern double f(double); • int day_of_year=31; • double f(double x) { • return 1.0/x; • } • File A.h • extern int day_of_year; • extern double f(double); • Trans. Unit useA.C • extern int day_of_year; • extern double f(double); • ... • day_of_year++; • double one_third = f(3); • ... • File useA.C • #include "A.h" • day_of_year++; • double one_third = f(3);
Function Declarations • linkage(optional) identifier type(return type, argument types) • extern doublesin(double x); • extern complexsin(complex x); • extern floatsin(double x); //Compiler Error • static doubleexp_random(double mu) { ... } • inline doublesqr(double x) {return x*x;} When declaring a name with external linkage, omit the extern keyword in definitions and use the extern keyword in declarations.
Function Arguments • Anything that occurs during the execution of a function other than the mapping of the input to the output is called side-effect. Pure side-effects are best provided in the context of classes. Avoid pure side-effects in global functions.
float 12.1 float 4.0 float 12.1 float 4.0 float 12.1 float 12.1 float 4.0 float x float b float a float x float x float b float a : : : : : : : int 4 int 4 int 4 • float s(float a, float b) { • a = b; • return a*b; • } • float x = 12.1; • s(x,4); conversion
float 12.1 float 4.0 float 12.1 float 4.0 float 4.0 float x float b float x float b float x : : : : : int 4 int 4 int 4 float a : float a : • float s(float& a, float b) { • a = b; • return a*b; • } • float x = 12.1; • s(x,4); conversion
float 12.1 float 4.0 float 12.1 float 4.0 float 4.0 float x float x float x : : : const float b const float b : : int 4 int 4 int 4 • float s(float& a, const float& b) { • a = b; • return a*b; • } • float x = 12.1; • s(x,4); conversion float a : float a :
Signal input-modification side-effects by consistent use of type modifiers in the declaration of the forma l arguments. Assume that an argument passed by (non-const) reference is modified. Pass small objects by value and large objects by const reference, if the arguments will not be modified. Declare C++ built-in array arguments const, unless the function is intended to alter the array elements. extern double g(const float y[]); extern double g(const float* y);
#include <math.h> • extern double log_of(double x, double base = M_E); // M_E in math.h • log_of(2.); • log_of(2.,10.); • extern double f(int x = 3, double y); // Compile Error • extern void doNothing(); Supply default arguments with the function's declaration in the header file, not with the function's definition in the implementation file.
extern void display_greeting(); • float f() { • ... • return 1; • } • float& second_element(float* a){ • return a[1]; • } • float x[] = {1, 1}; • second_element(x) = 10; • cout << x[1] << endl; • double& g() { • double x; • ... • return x; • } Use return by reference to provide access to a selected portion of an object or of a function argument passed by reference. Do not return (non-static) local variables or constants by reference.
Overloaded Functions • Exact Match or Match with Trivial Conversions • extern int abs(int); • extern double abs(double); • extern double abs(complex); • int i = -3; • int iabs = abs(i); // Calls abs(int) • double d = 3.2; • double dabs = abs(d); // Calls abs(double) • complex c(-1, 2); • double cabs = abs(c); // Calls abs(complex) • extern int abs(int&); • extern double abs(double&); • extern double abs(complex&);
Match with Promotions • extern double exp(int y); • extern double exp(double y); • Match with Standard Conversions • extern double exp(double y); • Match with User-Defined Conversions Used simply, function overloading avoids needless repetition of function definitions for similar types of arguments. But overused or used in a way that depends on subtleties of the C++ rules, overloading is confusing.
Function Templates • template<class EltType> • extern void axpy(EltType alpha, const Vector<EltType>& x, • Vector<EltType>& y); • void axpy(float alpha, const Vector<float>& x, Vector<float>& y); • void axpy(double alpha, const Vector<double>& x, Vector<double>& y); • void axpy(complex alpha, const Vector<complex>& x, Vector<complex>& y); • void axpy(float alpha, const Vector<double>& x, Vector<double>& y); • If an exact match of a nontemplate function is found, the matching function is called. • Otherwise, if a function template that matches (with only trivial conversions) is found, it is used. • Otherwise, if a function is found using ordinary argument matching process, it is used.