Using Procedures and Functions in C

A C program starts its execution with a function named main that returns an integer. A procedure does not have a return type so it is referred to as a void. In C, procedures and functions without parameters must be defined and called with a pair of brackets following the identifier. This first example, the equivalent of our Pascal Welcome, shows a procedure without parameters.

#include <stdio.h>

void hello()
{
  char forename[20];
  printf("What is your first name? ");
  scanf("%s", &forename);
  printf("Hello, %s!", forename);
}

int main()
{
  hello();
  return 0;
}    

Input and output:

What is your first name? Mo
Hello, Mo!

Procedures with Parameters

This example is the C equivalent of the Pascal program DrawStars.

#include <stdio.h>
#include <strings.h>

void write_stars(int num_of_stars)
{
  int count;
  for (count = 0; count < num_of_stars; count++)
  {
    printf("*");
  }
  printf("\n");
}

int main()
{
  const char title[18] = "DrawStars Program";
  int title_length = strlen(title);
  write_stars(title_length);
  printf("%s\n", title);
  write_stars(title_length);
  return 0;
}
    

Output:

*****************
DrawStars Program
*****************

The procedure can be made more versatile by introducing a second parameter of type char. In the next program the variables title_length and i are available only within the function main, and num_of_chars, character and count are accessible only within the write_chars procedure. Note how parameters of different types in the procedure definition are separated by a comma rather than by a semicolon as in Pascal.

#include <stdio.h>
#include <strings.h>

void write_chars(int num_of_chars, char character)
{
  int count;
  for (count = 0; count < num_of_chars; count++)
  {
    printf("%c", character);
  }
  printf("\n");
}

int main()
{
  const char title[18] = "DrawChars Program";
  int title_length = strlen(title);
  write_chars(title_length, '*');
  printf("%s\n", title);
  write_chars(title_length, '_');
  int i;
  for (i = title_length; i > 0; i--)
  {
    write_chars(i, 48 + i);
  }
  return 0;
}    
*****************
DrawChars Program
_________________
AAAAAAAAAAAAAAAAA
@@@@@@@@@@@@@@@@
???????????????
>>>>>>>>>>>>>>
=============
<<<<<<<<<<<<
;;;;;;;;;;;
::::::::::
999999999
88888888
7777777
666666
55555
4444
333
22
1

In these examples we pass the argument by value (preserving the original value of the passed variable). We demonstrate elsewhere passing by reference, including passing an array.

Functions

This example of the syntax is the equivalent of our first demonstration of a Pascal function.

#include <stdio.h>

float circle_area (float circle_radius)
{
  return 3.142 * circle_radius * circle_radius;
}

int main()
{
  float radius, area;
  printf("Please enter radius of circle: ");
  scanf("%f", &radius);
  area = circle_area(radius);
  printf("Area = %8.2f", area);
  return 0;
}    

Input and output:

Please enter radius of circle: 38.48
Area =  4652.39
Programming - a skill for life!

How to apply your knowledge of Pascal when learning C/C++