5. Functions

So far we have used functions which have been programmed by the creators of C and which have been resided in libraries but we have also made functions, specially in earlier chapter. Now it is time to introduce these functions much better, how to create them,  so that everyone can create own functions.

Functions break large computing tasks into smaller ones, and enable people to build on what others have done instead of starting over from scratch. A program may reside in one or more source files. Source files may be compiled separately and loaded together, along with previously compiled functions from libraries. Example: cl code1.c code2.c code3.c.

Each function definition has the form

Return-type function-name (argument declarations)
{
declarations and statements
}

Various parties may be absent; a minimal function is
dummy() { }

which does nothing and returns nothing.

The functions can occur in any order in the source file, and the source file can split into multiple files, so long as no function is split. A good rule for making functions is that when you have written so much code that it covers the whole screen, it is time to make a function.   Before use  of function it must be declared, ie before it is used in main-function or in other functions. You cannot define function inside other function, because C does not allow it! Communication between the functions is by arguments and values returned by the functions and through external values

The return statement is the mechanism for returning a value from the called function to its caller. Any expression can follow return

return expression;

The expression will be converted to the returned type of the function if necessary. Next simple function, which return the double value to its caller shows how to create and use functions.

/*returns entered number double to its caller*/
#include <stdio.h>
int MakeDouble (int number);/*Function declaration, function definition could also be here and then the declaration is not needed*/
void main()
{
  int value, result;
  printf("Welcome to the program, which counts the number double you enter \n?");
  scanf("%d", &value);
  result = MakeDouble(value);/*Function call*/
  printf("\n Value is %d", result);
}
int MakeDouble (int number)/*Function definition*/
{
  number=number*2;
  return number;
}

Variables can be either internal or external. External variables are defined outside of any function and are thus potentially available to many functions. Internal variables are defined  inside functions.

/*Example of external variables*/
#include
int first;/*external variable*/
int second;/*external variable*/
int time (int value);
void main()
{
  extern in first;
  extern int second;
  time(first);
  ...
}
int time(int value)
{
  extern int second;
  ...
  return value;
 }

The static declaration can be applied to external and internal variables but also it can be applied to functions as well. Normally function names are global, visible to any part of the entire program. If a function is declared static, however, its name is invisible outside of the file in which it is declared. Internal static variables provide private permanent storage within a single function.

Usage:   static int x;   static int function_name( int x);

A register declaration advises the compiler that the variable in question will be heavily used. The idea is that register variables are to be placed in machine registers, which may result in smaller and faster programs. But compilers are free to ignore the advice.

Usage:  register int x;

How to draw lots with a program? Not very complicated:

/* RAND.C: This program seeds the random-number generator with the time, 
 and then displays 10 random integers. */
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

void main( void )
{
  /* Seed the random-number generator with current time so that * the numbers will be different every time we run. */
  srand( (unsigned)time( NULL ) );
  /* Displays 1 number. */
  printf( "Number is %6d\n", rand() );
}

Delay using clock()-function:

#include
#include
int main(void)
{
printf("\nEnter the delay in seconds: ”);
float sek;
scanf("%f", &sek);
clock_t delay=sek*CLK_TCK;/*change*/
printf("\nStarting: ”);
clock_t begin=clock();
while(clock() – begin < delays)/*waiting*/
;
printf(”\nReady!\n”);
return 0;
}

 

C offers a large collection of built in functions declared in various libraries.

In library math.h are useful functions like pow(x,y) = x powered to y, sqrt(x), log (x), sin x, cos x and tan x, where x is in radians ( Π = 180°, Π/2 0 90°) . Also the value of pii is defined there and its name is M_PI.

Library string.h offers string functions.

Library time.h offers time and date functions.

Library ctype.h offers character class tests.

Library stdlib.h offers utility functions.

NOTE! This chapter  exercises can be found in quiz exercises to chapter 5 This information will be tested in quizzes 5-6! It is very important that the student will also make all examples shown in this chapter.