Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit


Computing for Engineers

Lecture 6

Functions and algorithms


1. Basic ideas about functions

In C, a function is used to support a particular task. We have already seen a few examples of functions. printf() and scanf() are two functions written by the         designers of C which perform particular tasks, namely output and input               respectively. We can write our own functions within a C program. Splitting up a program into a collection of functions each of which performs a separate part    of the total task, is called procedural programming and is a subject we will          consider in more detail in a subsequent section.

A first example of a function and how it is called is shown below:

#include

void print_message(void)

{

printf("This is the print_message function. \n"); }

int main(void)

{

print_message();

return 0;

}

Sample program output:

This is the print_message function.

We can note the following points about this program:

The function print_message() takes no arguments (we will look at              function arguments in the next section.) This is indicated by the keyword void between the parentheses. Also, the function returns no value which again is indicated by the keyword void preceding the function name.

To call a function, it is only necessary to write its name. The code     associated with the function name is executed at that point in the   program. When the function terminates, execution begins with the statement which follows the function name.

In the above program, execution begins at main(). The only statement

inside the main body of the program is a call to the code of function print_message(). This code is executed and, when finished, returns back to main().

2.  Function arguments, returning function values

The general form of a function definition is as follows:

return_data_type function_name (data_type argument1, data_type2 argument2 ….)

{

statement1;

statement2;

.

.

return return_value;

}

return_data_type (to the left of the function name) indicates the data

type of any value returned back to the main program. If nothing is being returned to the main program (as with print_message in the previous      section), then "void" should be inserted here.

Only one return_data_type may be specified per function.

The arguments of a function are the values that it receives from the

main program. A function can be defined with any number of arguments, of any datatypes. The datatype of each argument, followed by the              variable name of each argument, should be listed in brackets to the right  of the function name.

If a function has no arguments at all, then "void" should be specified.

#include

#include

#define PI 3.14

float area(float radius)

{

float x=PI*radius*radius;

return x;

}

int main(void)

{

float a, r;

printf("Input the radius: ");

scanf("%f",&r);

a=area(r);

printf("The area of the circle is %f ",a);

return 0;

}

Sample program output:

Input the radius: 2.0

The area of the circle is 12.570000

This is how the program works:

The function definition float area(float radius) indicates that the function      returns a floating point value and takes a floating point variable radius as an argument.

As with all C programs, when the program runs it starts at int main(void).

In the main program, r is a float value that is read in from the user. This is sent to the area function as an argument.

As the function is called, the value of the argument r is copied into the function argument radius.

The function then works out the actual value of the area and stores it in the variable x.

x is returned back to the main program, where the value of x is stored in the variable a.

a is printed on the screen.

A second example below shows a function calc_factorial() which calculates the factorial of an input integer.

#include

int calc_factorial(int n)

{

int i, factorial_number=1;

for (i=1; i<=n; i++)

factorial_number*=i;

return factorial_number;

}

int main(void)

{

int number;

printf("Input a number: ");

scanf("%d",&number);

printf("The factorial of %d is %d \n",

number, calc_factorial(number));

return 0;

}

In this case, the function calc_factorial() takes an integer argument and returns an integer.

Sample program output:

Input a number : 5

The factorial of 5 is 120

2.1.     Does a function change the value of its arguments?

It is important to understand the effect a function has on its arguments.        Consider the following function increment() which increments its argument:

#include

void increment(int n)

{

n++;

}

int main(void)

{

int number;

printf("Input a number: ");

scanf("%d",&number);

increment(number);

printf("The number now equals %d ",number); return 0;

}

Sample program output:

Input a number : 5

The number now equals 5

As can be seen from the sample program output, the function has not changed the value ofits argument. This is a very important point to understand and         comes about because the argument to the function increment() is passed by value (also known as call-by-value):

The following diagram shows what is meant by passing by value.

1

number=5

main program

copy


2

number=5

main program

function increment() on exit

n=6

It is important to remember that number and n are separate variables in separate memory locations.

When the function is called, the value of number is copied automatically into n so that the function can start executing.

However when the function exits, the value of n is not copied back into number, so the value of number in the main program is unchanged.

An obvious question is what if we want our function to alter the value of its       argument on exit, i.e. we actually want the function to be able to modify the     argument variable in the main program. In C we do this through the use of pointers which allow the argument to be passed by reference. This is described in a future lecture.

3. Array arguments to functions

Passing array arguments to functions is relatively straightforward. In the             following program, a function maximum() takes an array of positive integers as an argument and returns the maximum value in the array:

#include

int maximum (int v[5])

{

int max_value=0, i;

for (i=0; i<5; i++)

if (v[i]>max_value)

max_value=v[i];

return max_value;

}

int main(void)

{

int values[5], i, max;

printf("Enter 5 numbers \n ");

for (i=0; i<5; i++)

scanf("%d", &values[i]);

max=maximum(values);

printf("The maximum value is %d \n",max);

return 0;

}

Sample program output:

Enter 5 numbers

6 3 67 45 23

The maximum value is 67

The above function has a problem because it can only operate on arrays which have exactly five elements.

In general, in programming we want functions to be able to work on any data, no matter how large it is. Such functions are said to be scalable.


We can modify the program so that the number of elements in the array is  passed as an argument to the function. This is need to control when the for loop will stop.