The C and C++ skills that
supposed to be acquired:

Able to understand and use
function.

Able to create user defined
functions.

Able to understand Structured
Programming.

Able to understand and use
macro.

Able to appreciate the recursive
function.

Able to find predefined/built-in
standard and non-standard functions resources.

Able to understand and use
predefined/built-in standard and non-standard functions.

Able to understand and use
the variadic functions.

4.1 Some
Basic Definition

Most computer programs that solve real-world
problem are large, containing thousand to million lines of codes
and developed by a team of programmers.

The best way to develop and maintain
large programs is to construct them from smaller pieces or modules,
each of which is more manageable than the original program.

These smaller pieces are called functions.
In C++ you will be introduced to Class, another type smaller pieces
construct.

The function and class are reusable.
So in C / C++ programs you will encounter and use a lot of functions.
There are standard (normally called library) such as maintained
by ANSI C / ANSI C++, ISO/IEC C, ISO/IEC C++ and GNU’s glibc or
other non-standard functions (user defined or vendors specific or
implementations or platforms specific).

If you have noticed, in the previous
Modules, you have been introduced with many functions, including
the main(). main() itself is a function but with a program
execution point.

Functions are very important construct
that marks the structured or procedural programming approach.

In general terms or in other programming
languages, functions may be called procedures or routines and in
object oriented programming, methods may be used interchangeably
with functions.

Some definition: A function is a named,
independent section of C / C++ code that performs a specific task
and optionally returns a value to the calling program.

So, in a program, there are many calling
function (caller) and called functions
(normally called callee).

There are basically two categories of
function:

Predefined functions - available
in the C / C++ standard library such as stdio.h, math.h, string.h
etc. These predefined functions also depend on the standard
that you use such as ANSI C, ANSI C++, ISO/IEC C, Single UNIX
Specification, glibc (GNU), Microsoft C etc. but the functions
name and their functionalities typically similar. You
have to check your compiler documentation which standard that
they comply to.

User-defined functions –
functions that the programmers create for specialized tasks
such as graphic and multimedia libraries, implementation extensions
or dependent etc. This is non-standard functions normally
provided in the non-standard libraries.

You
will encounter a lot of the predefined functions when you proceed from Module
to Module in this Tutorial. Here we will try to concentrate on the
user-defined functions (also apply to predefined function), which basically
having the following characteristics:

A function is named with unique name
- By using the name, the program can execute the statements contained
in the function, a process known as calling the function. A function
can be called from within another function.

A function performs a specific task
- Task is a discrete job that the program must perform as part of its
overall operation, such as sending a line of text to the printer, sorting
an array into numerical order, or calculating a cube root, etc.

A function is independent - A function
can perform its task without interference from or interfering with other
parts of the program. The main program, main() also a function
but with an execution point.

A function may receive values from the
calling program (caller) - Calling program can pass values to function
for processing whether directly or indirectly (by reference).

A function may return a value to the
calling program - When the program calls a function, the statements
it contains are executed. These statements may pass something
back to the calling program.

Let
try a simple program example that using a simple user defined function:

// demonstrates a simple function

// calling predefined functions needed in this program
such as printf()

// receive
one argument
that is cube's side (x), and supposed to return long type, this
is a callee

long cube(long x)

{

// local scope (to this
function) variable, only effective in this function 'body'

long
x_cubed;

// do the volume calculation

x_cubed =
x * x * x;

// return a result to caller

return
x_cubed;

}

Output:

The
following statement is calling
cube() function, bringing along the value
assigned to the input
variable.

When
this statement is executed, program jump to the cube()
function definition.

After
finished the execution, the cube()
function returns to the calling code, where in this case, returning and
then assigning the return value, x_cubed
to an answer
variable.

In
this program the scanf()
and print()
are examples of the standard predefined functions. It is conventionally
used main()
as the main entry point of execution. However standard doesn't state the
use of main()
as the main execution point. So you can use other name instead of main()
as the main execution point.

4.2
How A Function Works

A C
/ C++ program does not execute the statements in a function until the function
is called by another part of the program.

When
C / C++ function is called, the program can send information to the function
in the form of one or more what is called arguments
although it is not a mandatory.

Argument
is a program data needed by the function to perform its task.

When
the function finished its processing, program returns to the same location
that called the function.

The
following figure illustrates a function call.

Arrow

Means

→

Calling function with
data (argument) if any

←

Return to the next
statement or execution with data if any

When
a program calls a function, executions passed to the function and then back
to the calling program’s code.

Function
can be called as many times as needed as shown for function2() in the above
figure, and can be called in any order provided that it has been declared
(as a prototype) and defined.

4.3
A Function Definition

Is
the actual function body, which contains the code that will be executed
as shown below for our previous example.

long cube(long
x)

{

// local scope (to this function)
variable, only effective in this function 'body'

long x_cubed;

//
do the volume calculation

x_cubed = x * x * x;

// return a result to caller

return x_cubed;

}

First
line of a function definition is called the function header, should
be identical to the function prototype, except without the semicolon.

Although
the argument variable names (x in this case)
were optional in the prototype, they must be included in the function header.

Function
body, containing the statements, which the function will perform, should
begin with an opening brace and end with a closing brace.

If
the function returns data type is anything other than void
(nothing to be returned), a return
statement should be included, returning a value matching the return data
type (long
in this case).

4.4
Structured Programming

Functions
and structured programming are closely related.

Structured
programming definition: In which individual program tasks are performed
by independent sections of program code.

Normally,
the reasons for using structured programming may be:

It is easier to write a structured program
- Complex programming problems or program are broken into a number of
smaller, simpler tasks. Every task can be assigned to a different
programmer and/or function.

It’s easier to debug a structured program
- If the program has a bug (something that causes it to work improperly),
a structured design makes it easier to isolate the problem to a specific
section of code.

Reusability - Repeated tasks or routines
can be accomplished using functions. This can overcome the redundancy
of code writing, for same tasks or routines, it can be reused, no need
to rewrite the code, or at least only need a little modification.

Advantages:
Can save programmers’ time and a function written can be reused (reusability).

Structured
program requires planning, before we start writing a single line of code.

The
plan should be a list of the specific tasks that the program performs.

However
the structured programming approach just play a small role in the real program
development because of the today's program complexities.

Today's
program development achieved using tools such as Unified Modeling Language
(UML) that could cover a complete, broad spectrum of the program development
life cycle.

A Very Simple Example

Imagine
that you are planning to create a program to manage a name and address list
of students.

Planning:
Roughly the algorithm normally written in
pseudo code might be (not in order):

Enter new names and address.

Modify existing entries.

Sort entries by last name.

Printing mailing labels.

So,
the program is divided into 4 main tasks, each of which can be assigned
to a function.

Next,
if needed, we still can divide these tasks into smaller tasks called subtasks.

For
example, as a common sense, for the "Enter new names and addresses" we can
divide to the following subtasks:

Read the existing address list from disk.

Prompt the user for one or more new entries.

Add the new data to the list.

Save the updated list to disk.

Then
if needed, "Modify existing entries" task still
can be subdivided to the following subtasks:

Read the existing address list from disk.

Modify one or more entries.

Save the updated list to disk.

Finally
we can see that there are two common subtasks: "Reading
from disk and saving to disk"

So,
one function can be called by both the "Enter new
names and address" function and the "Modify
existing entries" function. No redundancy or repetition and
the functions created and tested can be reused later on by programs that
need the same tasks.

Same
for the subtask "Save the updated list to disk".

Structured
programming method results in a hierarchical or layered program structure,
as depicted in figure 4.1:

4.4.1
A Top-down Approach

Using
structured programming, C / C++ programmers take the top-down approach as
in the previous figure. So, program’s structure resembles an inverted
tree, from the root then to the trunk then to the branch and finally to
the leafs.

From
the main() program, subdivide the task to smaller tasks (subtasks).
Then these smaller subtasks are divided again if needed, to smaller subtasks
and so on.

Every
subtask then assigned to the specific functions.

You
can see that most of the real work of the program is performed by the functions
at the “tip of the branches”.

The
functions closer to the trunk primarily are direct program execution among
these functions.

So,
main body of the program has a small amount of code and every function acts
independently. To see how this approach been implemented, check
Example #13 at the end of this Module.

4.5
Writing A Function

The
first step is to know what task the function should perform. Then,
detail the function declaration and definition.

When
you create functions or use the pre-defined functions, for debugging and
testing, you may convert each function to main()
program, to test it individually, because function cannot be run individually.

Next,
when you have satisfied with the execution of each separate main()
program, re convert these separate main()
programs to the respective functions and call the functions from within
one main()
program. Keep in mind that main()
is also a function but with execution point. Let discussed the details
how to write a function, a user defined function.

4.5.1
The Function header

The
first line of every function is called function header. It has 3 components,
as shown below:

Function return type - Specifies the data
type that the function should returns to the calling program.
Can be any of C/C++ data types: char, float, int, long, double etc.
If there is no return value, specify a return type of void. For
example,

int
calculate_yield(…)
//
returns a int type

float
mark(…)
//
returns a float type

void
calculate_interest(…) // returns nothing

Function name - Can have any name as long
as the rules for C / C++ variable names are followed and must be unique.

Parameter list - Many functions use arguments,
the value passed to the function when it is called. A function
needs to know what kinds of arguments to expect, that is, the data type
of each argument. A function can accept any of C / C++ basic
data types. Argument type information is provided in the function
header by the parameter list. This parameter list just acts as
a placeholder.

For
each argument that is passed to the function, the parameter list must contain
one entry, which specifies the data type and the name of the parameter.

For
example:

void myfunction(int x, float y, char
z)

void yourfunction(float myfloat, char mychar)

int ourfunction(long size)

The
first line specifies a function with three arguments: a type int named
x, a type float named y and a type char named z.

Some
functions take no arguments, so the parameter list should be void such as:

long thefunction(void)

void testfunct(void)

int zerofunct()

Parameter
is an entry in a function header. It serves as a placeholder for an
argument. It is fixed, that is, do not change during execution.

The
argument is an actual value passed to the function by the calling program.
Each time a function is called, it can be passed with different arguments
through the parameters.

A function
must be passed with the same number and type of arguments each time it is
called, but the argument values can be different.

In
function, using the corresponding parameter name accesses the argument.

Program
example:

//
demonstrate the difference between arguments and parameters

//
using predefined functions in the C standard library

#include
<stdio.h>

//
main() function

void
main()

{

float x = 3.5, y = 65.11, z;

// function prototype is here, a rare
implementation...

float half_of (float);

// In this call,
x is the argument to half_of().

z = half_of(x);

printf("The function call statement is z = half_of(x)\n");

printf("where x = 3.5 and y = 65.11...\n\n");

printf("Passing argument x\n");

printf("The value of z = %f \n", z);

// In this call,
y is the argument to half_of()

z = half_of(y);

printf("\nPassing argument y\n");

printf("The value of z = %f \n\n", z);

}

//
function definition, must receive one float value that will be placed in

// k and must
return a float value back to the caller...

float
half_of(float k)

{

// k is the parameter, a placeholder.
Each time half_of() is called,

// k may has different value
that was passed as an argument.

return (k/2);

}

Sample output:

---------------------------------------------------------------------

For
the first function call:

Then,
the second function call:

Each
time a function is called, the different arguments
are passed to the function’s parameter.

z = half_of(y)
and z = half_of(x),
each send a different argument to half_of()
through the k
parameter.

The
first call send x,
contain a value of 3.5,
then the second call send y,
contain a value of 65.11.

The
value of x
and y
are passed (copied) into the parameter k
of half_of().

Same
effect as copying the values from x
to k,
and then y
to k.

half_of()
then returns this value after dividing it by 2.

We
can depict this process graphically as follows:

4.5.2
The Function Body

Enclosed
in curly braces, immediately follows the function header.

Real
work in the program is done here.

When
a function is called execution begins at the start of the function body
and terminates (returns to the calling program) when a return
statement is encountered or when execution reaches the closing braces (}).

Variable
declaration can be made within the body of a function.

Variables
declared in a function, are called local variables.
The scope, that is the visibility and validity of the variables are local.

Local
variables are the variables apply only to that particular function, are
distinct from other variables of the same name (if any) declared elsewhere
in the program outside the function.

It
is declared like any other variable and can also be initialized. Outside
of any functions, those variables are called global
variables.

Example
of declaring local variables:

int
function1(int y)

{

Int
a, b=10; // local
variable

Float
rate; // local
variable

Double
cost=12.55;
//
local variable, been initialized

}

Program
example:

Output:

The
function parameters are considered to be variable declaration, so, the variables
found in the functions parameter list are also available.

Function prototype normally placed before
main()
and your function definition after main()
as shown below. For C++, the standard said that we must include the
prototype but not for C.

#include …

/* function prototype */

int funct1(int);

int main()

{

/* function call */

int y = funct1(3);

…

}

/* Function definition */

int funct1(int x)

{…}

But
it is OK if we directly declare and define the function before main()
as shown below. This becomes an inline function.

#include …

/* declare and define */

int funct1(int x)

{

…

}

int main()

{

/* function call */

int y = funct1(3);

…

}

Or
you will find later that we can declare, define and implement the functions
in other files.

Three
rules govern the use of variables in functions:

To use a variable in a function, the programmer
must declare it in the function header or the function body.

For a function to obtain a value from the
calling program (caller), the value must be passed as an argument (the
actual value).

For a calling program (caller) to obtain
a value from function, the value must be explicitly returned from the
called function (callee).

4.5.3
The Function Statements

Any
statements can be included within a function, with one exception: a function
may not contain the definition of another function.

For
examples: if statements, loop, assignments etc are valid statements.

4.5.4 Returning
A Value

A function
may or may not return a value.

If
function does not return a value, then the function return type is said
to be of type void.

To
return a value from a function, use return
keyword, followed by C/C++ expression.

The
value is passed back to the caller.

Example
of returning a value from a function:

The
return value must match the return data type. For the above code segment,
x must be an integer.

A function
can contain multiple return statements. The first return executed
is the only one that has any effect.

An
efficient way to return different values from a function may be as shown
in the following example.

//
using multiple return statements in a function

#include
<stdio.h>

//
a function prototype, a mandatory in C++

int larger_of(int,
int);

//
a caller

void main()

{

int x, y, z;

puts("Enter two different integer values,");

puts("separated by space. Then press Enter key: ");

scanf("%d%d", &x, &y);

//
call large_of() with 2 arguments stored in x and y respectively

z = larger_of(x, y);

printf("\nThe larger value is %d.", z);

printf("\n");

}

// a callee

//
a function definition, must receive 2 arguments stored (copied) in a and b respectively

// and must
return a value of type int

int larger_of(int
a, int b)

{

// return a or b

if(a > b)

return a;

else

return b;

}

Output:

4.5.5
The Function Prototype

Must be included for each function that
it uses, (required by Standards for C++ but optional for C) if not
directly declare and define before main().

In most cases it is recommended to include
a function prototype in your C program to avoid ambiguity.

Identical to the function header, with
semicolon (;)
added at the end.

Function prototype includes information
about the function’s return type,
name and parameters’
list and type.

The name is optional and the following
is an example of function prototype.

long cube(long);

Function prototype provides the C/C++
compiler with the name and arguments of the functions contained
in the program and must appear before the function is used or defined.
It is a model for a function that will appear later, somewhere in
the program.

So, for the above prototype, the function
is named cube,
it requires a variable of the type long,
and it will return a value of type long.

Then
the compiler can check every time the source code calls the function, verify
that the correct number and type of arguments are being passed to the function
and check that the return value is returned correctly.

Further
C and C++ reading and digging:

For this Module purpose, you can check the standard
libraries of these various standards of C / C++. Explore and compare
the standard functions and their variation if any in the libraries.
You can download or read online the specification at the following links.
(ISO/IEC is covering ANSI and is more general):