Functions and Program Design

A function is simply a named block of code. A function is like a small program that performs a small, well-defined task. Like the larger program it is a part of, a function is usually fed data to process and can have its own variables.

There are two main types of functions, value-returning functions and void functions. A void function does not return any value.

A function call is a statement or expression that transfers control to a function so that the function can perform its designated task. A function call is executed by calling the name of the function immediately followed by parentheses. If we wish to supply values to the function, we place those values in order within the parentheses, separated by commas. These values constitute arguments to the function, that are passed over to the function’s parameters.

Value parameters are an integral part of functions. Value parameters are in essence a type of variable that a function uses to receive the data it will process. A value parameter has a life separate from the corresponding value sent into the function from the function’s caller; this is true even if that value is itself a variable. In other words, if we change the parameter in the function, the associated value in the rest of the program remains the same; in other words, parameters are a copy of the data, not the original data, and the scope of the parameter is limited to the length of the function.

Let’s start by practicing with some of the predefined mathematical functions from the library math.h. As our program will be using a function from a library file, we must have an include statement specifying that library file. We will be calling the log10() function, which takes a double as an argument and returns a double value. The log10() function finds the natural logarithm, AKA the base 10 algorithm, for a specified value. We recalled here that the logarithm of a number is simply the exponent to which another number, the base, must be raised to produce the number. Thus, log10(100) would return the value 2.

#include <stdio.h>
#include <math.h>

int main(void){

	double x = 100000;
	int y = 1000;

	printf("The base10 log of %f is %f\n", x, log10(x));
	//cast y into double to match parameter
	printf("The base10 log of %d is %f\n", y, log10((double)y));

	return 0;

}

We should remember here that any value a function call supplies to the function is called an argument of the function, whereas the result calculated and returned by the function is known as the return value.

A function expects the argument type to match the parameter type; we can see an example of this in the three functions abs(), labs(), and fabs(). Each of these three functions performs the same task, they return the absolute value of the argument provided. Where they differ is that each function accepts an argument of a different type, abs() accepts an argument of type int, labs() accepts an argument of type long, and fabs() accepts an argument of type double.

#include <stdio.h>
#include <math.h>

int main(void){

    int iX = 1999;
    double dY = -1.05;
    long lZ = 8675309;

    printf("The absolute value of %d is %d\n", iX, abs(iX));
    printf("The absolute value of %f is %f\n", dY, fabs(dY));
    printf("The absolute value of %ld is %ld\n", lZ, labs(lZ));

    return 0;

}

When writing a function, we normally split the function into two separate pieces, the declaration and the definition. A function declaration is placed before the main() function. The function declaration specifies what the function is supposed to do and what a programmer should known to call the function. The function definition includes the code that actually performs the task. The function definition includes before this code the function header, which matches the function declaration, except that the function declaration is followed by a semicolon.

#include <stdio.h>

//function declarations go here
void Greet();
void GreetByName(char nameParameter[]);

int main(void){

	char szName[256];

	//call function Greet()
	Greet();

	printf("What is your name? Please enter it here: ");
	scanf(" %s", szName);

	//call function GreetByName()
        //note the single argument
	GreetByName(szName);

	return 0;

}


//function definitions go here
void Greet(){
	printf("\nSaluton Mundo!\n");
}

void GreetByName(char nameParameter[]){
	printf("\nHello, %s\n", nameParameter);
}


As stated earlier, arguments and parameters must match each others data types. Arguments and parameters are matched in order from left to right, so that the first parameter is assigned the value specified in the first argument, etc. When writing a call to a function, we must therefore be careful to give the arguments in the same order as the parameters to which they will be paired.

#include <stdio.h>

//function declaration
//AddUpChange adds up the number of quarters, dimes, nickels and pennies and 
//returns the dollar amount as a double value
double AddUpChange(int quarters, int dimes, int nickels, int pennies);

int main(void){

	int iQuarters, iDimes, iNickels, iPennies;

	iQuarters = 3;
	iDimes = 9;
	iNickels = 4;
	iPennies = 3;

	printf("We have %d quarters, %d dimes, %d nickels, %d pennies.\n", iQuarters, iDimes, iNickels, iPennies);

	printf("That equals $%.2f\n", AddUpChange(iQuarters, iDimes, iNickels, iPennies));

	return 0;

}


double AddUpChange(int quarters, int dimes, int nickels, int pennies){
	double d = 0.0;
	d += quarters * .25;
	d += dimes * .10;
	d += nickels * .05;
	d += pennies * .01;

	return d;

}

In the program above, the double value d is a local variable; local variables are accessible only by statements within the function. Just as with parameters, the memory for local variables is allocated when the computer begins executing the function and is deleted when the function completes its execution.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s