A More Advanced Look at Functions

​Modular Programming

Modular programming: breaking a program up into smaller, manageable functions or modules

Function: a collection of statements to perform a task

Motivation for modular programming:

  • Improves maintainability of programs

  • Simplifies the process of writing programs

Static Local Variables

  • Local variables only exist while the function is executing. When the function terminates, the contents of local variables are lost.

  • static local variables retain their contents between function calls.

  • static local variables are defined and initialized only the first time the function is executed. 0 is the default initialization value

Using a Static Variable

Default Arguments

A Default argument is an argument that is passed automatically to a parameter if the argument is missing on the function call.

  • Must be a constant declared in prototype:

    • void evenOrOdd(int = 0);

  • Can be declared in header if no prototype

  • Multi-parameter functions may have default arguments for some or all of them:

    • int getSum(int, int=0, int=0);

Default Arguments Example

Default Arguments

  • If not all parameters to a function have default values, the defaultless ones are declared first in the parameter list:

int getSum(int, int=0, int=0);// OK
int getSum(int, int=0, int); // NO
  • When an argument is omitted from a function call, all arguments after it must also be omitted:

sum = getSum(num1, num2); // OK
sum = getSum(num1, , num3); // NO

Overloading Functions

Recall

Declaration:

return_type name (formal arguments);
int sum(const int[] vals);

Note, the declaration does NOT need names for the formal arguments.

int sum(const int[]); // acceptable in declaration

Why?

• Compiler only needs this information to find the function definition that corresponds to it.

  • Overloaded functions have the same name but different parameter lists

  • Can be used to create functions that perform the same task but take different parameter types or different number of parameters

  • Compiler will determine which version of function to call by argument and parameter lists

Funcrion Signature

  • Unique combination of

    • Name and parameter types and parameter order.

    • Frequently equated with the declaration.

      • However, it is possible to have different declarations that are considered the same signature.

int mean(int a, int b);
int mean(int first, int last);
double mean(int, int);

Compiler

  • The compiler tries to match function calls to a signature that matches a declaration it has already seen.

    • Note: The compiler starts at the top of the file containing main() and works its way down.

    • This is why functions must be declared above where they are called in a program.

    • #include essentially puts declarations at the top of the file.

Function Overloading Examples

//Using these overloaded functions,
void getDimensions(int); // 1
void getDimensions(int, int); // 2
void getDimensions(int, double); // 3
void getDimensions(double, double);// 4


//the compiler will use them as follows:
int length, width;
double base, height;
getDimensions(length); // 1
getDimensions(length, width); // 2
getDimensions(length, height); // 3
getDimensions(height, base); // 4
Function Overloading Example

Overloading

  • Essentially creating multiple functions with

    • The same name

    • Different parameter configurations

      • Number of parameters

      • Types of parameters

      • Order of parameter types

  • Only overload if the functions do very similar things!

Overloading Guidelines

Last updated

Was this helpful?