| My Training Period: xx hours
Note: Function is one of the important topics in C and C++. The source code for this Module is: C/C++ functions source codes and the lab worksheets for your practice:Function lab worksheet 1,lab worksheet 2,lab worksheet 3 andlab worksheet 4.
The C and C++ skills that supposed to be acquired:
4.1 Some Basic Definition
|
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()
#include <stdio.h>
// function prototype, explained later
long cube(long);
void main()
{
long input, answer;
printf("\n--Calculating cube volume--\n");
printf("Enter a positive integer, for cube side (meter): ");
scanf("%d", &input);
// calling cube() function, bringing an input argument. main() is a caller
answer = cube(input);
// %ld is the conversion specifier for a long integer, more on this in another module
printf("\nCube with side %ld meter, is %ld cubic meter.\n", input, answer);
}
// function definition, calculating cube's volume
// 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.
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.
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).
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 inpseudo 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:
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.
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.
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);
}
---------------------------------------------------------------------
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:
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
Doublecost=12.55; // local variable, been initialized
}
Program example:
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 beforemain() 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).
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.
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;
}
4.5.5 The Function Prototype
function_return_type function_name(type parameter1, type parameter2,…, type parameterN)
long cube(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.
If mismatch occurs, the compiler generates an error message enabling programmers to trap errors.
A function prototype need not exactly match the function header.
The optional parameter names can be different, as long as they are the same data type, number and in the same order.
But, having the name identical for prototype and the function header makes source code easier to understand.
Normally placed before the start of main() but must be before the function definition.
Provides the compiler with the description of a function that will be defined at a later point in the program.
Includes a return type which indicates the type of variable that the function will return.
And function name, which normally describes what the function does.
Also contains the variable types of the arguments that will be passed to the function.
Optionally, it can contain the names of the variables that will be returned by the function.
A prototype should always end with a semicolon ( ; ).
For example (can’t be executed):
// function prototype code snippet example
// this example cannot be compiled or run
double squared (double); // function prototype
void print_report (int); // function prototype
double squared(double number) // function header
{ // opening bracket
return (number * number); // function body
} // closing bracket
void print_report(int report_number)
{
if(report_number == 1)
printf("Printing Report 1");
else
printf("Not printing Report 1");
}
- ISO/IEC 9899 (ISO/IEC 9899:1990) - C Programming languages.
- ISO/IEC 9899 (ISO/IEC 9899:2018) - C Programming languages.
- ISO/IEC 9945-1:2003 POSIX Part 1 Base Definition.
- ISO/IEC 14882:1998 on the programming language C++.
- Latest, ISO/IEC 14882:2017 on the programming language C++.
- ISO/IEC 9945:2018, The Single UNIX Specification, Version 4.
- Get the GNU C library information here.
- Read online the GNU C library here.