Free2Code
Tutorials » Browse » Programming
Tutorials - Introduction to C - Functions
This article written by
  OldSite

Member since
  October 11, 2006

C allows you to create your own functions. Functions accept parameters and return a result. They let us simplify our programs into sections that can be called by other sections. Let’s have a look at a simple example:

void header()
{
    printf("Author: Jestern");
    printf("Program: Blah blahn");
    printf("Version: 1.0n");
}

Now remember we said a functions returns a value. We put void because our function doesn’t. if we expected it to return an int value, we put int. if a char, we put char, etc. We then declare the name of the function and the code to be executed goes between the curly braces.

header();

Is how we call the function, we must use the *()* in the call to tell the compiler we’re calling a function. Our program uses no parameters and returns no result, it is a very simple function. But it shows you the structure and how to declare one. We could now use this in a program.

#include <stdio.h>

void header()
{
    printf("Author: Jestern");
    printf("Program: Blah blahn");
    printf("Version: 1.0n");
    return 0;
}

int main()
{
    header();
    /* program continues */
}

Functions allow us to break up the program, if we wanted to print out the info about our program over and over in differen parts of a program, it’s easier to use a function and just call it.

Returning a value

We can return values from a function, take a look below:

int our_sum()
{
    int the_sum;
    the_sum = 2 + 3;
    return the_sum;
}

Now our function returns a result, it returns an integer. We add 2 and 3 together, assign the result to the_sum and then return it.

#include <stdio.h>

int our_sum()
{
    return 2 + 3;
}

int main()
{
    int sum;
    sum = our_sum();
    printf("%d", sum);
    return 0;
}

So we define the return type as int:

int our_sum()

Then we write the function that adds to numbers together. Then we open the main() function of our program and assign the result returned from our function to a variable:

sum = our_sum();

That’s returning a result, the result the function returned was assigned to sum.

Parameters

We can pass parameters to a function and use them within that function. This is how we do it.

int our_sum(int a, int b)
{
    return a + b;
}

Instead of *()* being empty this time, we define our parameters. We expect two parameters to be passed to this function, which we call a and b We can then return a value, we return the value of a added to b. So we could do:

#include <stdio.h>

int our_sum(int a, int b)
{
    return a + b;
}

int main()
{
    int sum;
    sum = our_sum(2, 3);
    printf("%d", sum);
    return 0;
}

When we call sum = our_sum(2, 3); we pass two values to the function, 2 and 3. The function then handles these as a and b, we add them together and return the result, which we then assign to sum and print out.

Old Style

Sometimes you may see functions written in the old style. C has evolved over the years but it’s important to see what the old style looks like incase you ever read code that uses it, here’s our function in old style.

int our_sum(a, b)
    int a;
    int b;
{
    return a + b;
}

It is the same as our function, it executes exactly the same, i just thought you should see it incase you ever come across it.

Program

To conclude the functions sestion we’re going to write a little program. Now we have covered quite a few concepts we can put them together.

Let’s write a program that let’s the user enter three names (maximum 30 chars) and three ages, then print out the combined ages of the people and write the info to a text file named “data.txt”. Pretty simple but it will allow us to put a few things together.

#include <stdio.h>

int add_ages(int a, int b, int c)
{
    return(a + b + c);
}

int main()
{
    int age1, age2, age3, sum;
    char name1[30], name2[30], name3[30];
    FILE *f;
    printf("Enter the name of a person followed by their age, i.e joe 23:");
    scanf("%s%d", name1, &age1);
    printf("Enter the name of another person followed by their age:");
    scanf("%s%d", name2, &age2);
    printf("Enter the name of another person followed by their age:");
    scanf("%s%d", name3, &age3);
    sum = add_ages(age1, age2, age3);
    printf("Combined ages of %s, %s and %s is %d", name1, name2, name3, sum);
    f = fopen("data.txt", "a"); /* open for appending */
    if(!f) {
        return 1;
    }
fprintf(f, "Combined ages of %s, %s and %s is %dn",name1,name2,name3,sum);
    fclose(f);
    return 0;
}

There we have it, i’m not going to go through and explain the code. If you’ve been reading the chapters properly you’ll be well aware of how that works. It isn’t difficult, and it’s kind of pointless, though things are getting a little more interesting.

Let’s go on and discuss pointers with functions.


Continue to Pointers »
In this tutorial:
  1. What is C?
  2. Control Structures
  3. Arrays
  4. Text Files
  5. Bibliography
  6. Functions & Pointers
  7. Functions
  8. Pointers
  9. Taking Input
  10. Variables
  11. Compilers
  12. Hello World
icons