Functions

Function Challenge Pointers as Function Arguments

C++ FUNCTIONS

Terminology

Function: user defined operation
Argument: function operand
Return type: type of function
Function prototype: declaration of a function; consists of return type, name, and argument list (with types)
Function signature: argument list

Format

<c++function> :: = <ftype><funcname>(<arglist>){<body>}
<ftype> :: = | void | <type>
Notes: 1) No specified function type defaults to int. Function with ftype void corresponds to Pascal procedure
<funcname> ::= <id>
<arglist> ::= | <onearg> | <onearg>,<arg_list>
<onearg> ::= <type> <id> | <type> &<id>

Placement in a program

<c++prog> ::= <sysdirectives> <decdefs> <functions> main () {<body>}| <sysdirectives> <functions> <decdefs> main () {<body>}| <sysdirectives> <functionprototypes> <decdefs> main () {<body>}<functions>

Function may not be nested in another function
Function may be defined and declared at the same time or declared at one point and defined later
- Declaration: header line (prototype) followed by semicolon
- Definiton: full function with header and code
Function must be declared BEFORE it can be used

Returning a value

Reference argument
- Ampersand precedes argument name
- Analogous to Pascal var parameter
- Example

void findsum (int a, int b, int &sum ) {
  // Return sum of a and b in sum
  sum = a + b;
}
Function return type
- Desired value is return value of function
int sum (int a, int b) {
  // Return sum of a and b as function value
  int S;
  S = a + b;
  return sum;
}

or more briefly

int sum (int a, int b) {
  // Return sum of a and b as function value
  return a + b;
}
- NOTE: Return type may be reference. See discussion below in "Scope".

Invoking a function

Format
<fname> (<args>, &<refarg>, <args>);
<args> ::= | <arg> | <arg>, <args>
<arg> ::= <id>
<refarg> ::= <id>
Examples
  1. Value returned in argument <refarg>

    Example using first example above:

    findsum (i1, i2, isum);
    cout << isum << endl;

  2. Value is return value for function

    Example using second example above:

    cout << "sum = " << sum (i1, i2) << endl;

Function overloading

C++ functions in same program may have same name
Signatures must be different so system can differentiate
Example

int sum (int a, int b) {
  // Return sum of a and b as function value
  return a + b;
}

float sum (float a, float b) {
  // Return sum of a and b as function value
  return a + b;
}

Inline function

Inserted in code at compile time rather than being invoked at run time
Actually only a suggestion to compiler
Programmer specifies function as inline

inline int sum (int a, int b) {
  return a + b;
}

Scope

Portion of program in which an identifier may be referenced
Function definitions have file scope
Identifiers declared within block ({}) have block scope, i.e. scope begins at declarations and ends at final right brace
Implication for function reference return type

int sum (int a, int b) {
  // Return sum of a and b as function value
  int S;
  S = a + b;
  return sum;
}
S is local variable. Scope is this function. Therefore, we cannot return its address to be used after execution of this function ends.

int& sum (int a, int b, int &sum) {
  sum = a + b;
  return sum;
}
sum is not local variable. Exists in invoking function. Therefore, may return its location.

Challenge

Try this out: Write two functions which swaps two values twice.  Once by reference.  The other by value. Output the data after every swap.

Back Home Up Next