11 May 2012

Coding Functions in 'C'

In order to create and use a function in 'C', the programmer must provide three things which have matching descriptions:

  1. A function prototype - describes the structure of the function to the compiler.
  2. A function call - passes program control to the function.
  3. A function header - begins the program code of the function.
Function Prototype

A prototype shows the name of the function
and the type of data the function will process.
void DoAction(void);
indicates that the function returns no value and receives no data.

Function Call

The function call passes control to the function.
DoAction();
Passes control to the DoAction function.

Function Header

The function header defines the function. The body of the function code is defined by { }.
// Sample code for Triangle Calculator
#include<stdio.h>

void CalculateArea();    // Function Prototype

void main()
{
    printf("Triangle Area Calculator");
    CalculateArea(); // Function Call
    printf("Program Complete");
    scanf("%*c");
}

void CalculateArea()    // Function Header
{
    float Height, Base, Area;

    printf("Enter the Height ");
    scanf("%f%*c", &Height);
    printf("Enter the Base ");
    scanf("%f%*c", &Base);
    Area = Height * Base / 2.0;
    printf("The Area is %0.2f\n", Area);
}

3 comments:

  1. Helpful post, thanks for sharing this coding functions. It is really helpful. Keep up the good work and share more.
    C++ Training | C Language Training

    ReplyDelete
  2. Glad you find the article helpful. :) happy coding!

    I also have another blog I started recently for code snippets. Here is the link. https://georgesnippets.blogspot.com

    ReplyDelete
  3. I always enjoy reading quality articles by an individual who is obviously knowledgeable on their chosen subject. Ill be watching this post with much interest. Keep up the great work, I will be back
    C and C++ Training Institute in chennai | C and C++ Training Institute in anna nagar | C and C++ Training Institute in omr | C and C++ Training Institute in porur | C and C++ Training Institute in tambaram | C and C++ Training Institute in velachery

    ReplyDelete