Functions in C Programming Language

Functions in C Programming Language

The reason why you want to use and eventually get used to functions in C programming language is because they save a lot of time when writing code. It simplifies the process and it let you use third party software (such as libraries!). At the end you will find an example with a code from a microcontroller. Keep reading!

What are the Functions in C?

A function is a specific piece of code which can be executed from anywhere in the code. For example, the Main function can call other functions to execute specific tasks. When the functions finishes, it returns to the last line of code where it began. In other words,

  1. It ‘jumps' from one place to the code.
  2. It does whatever it needs to do.
  3. It returns to where it started and continues.

How do Functions in C look like?

The functions is composed by the following parts in order:

  • Type: Expresses if the functions returns a variable upon completion or not.
  • Name: How will the code identify this function? If the name has 2 or more words, use underscores _ instead of spaces or use capital letters. More on that later.
  • Input Variables (if any): They must be placed between parethensis (). If 1 or more variables are required to execute the code, they have to be specified here. The variable type has to be specified first, and then the name of the variable.
void textUART(char LEDstatus) {
  // code here
}

In the example above, void is the type of function, textUART is the name of the function, char is the type of variable and LEDstatus is the name of the variable required in the function.

When to implement Functions in C

When you program in any language, you will eventually need to reuse the pieces of code inside of it. Copying and pasting may be easier in the beginning, but in the long run it will be a waste of time, your program memory will fill up quickly, and it will be a mess. Instead, use a simple method to program better.

If you would like to measure the voltage of a pin, for example, then the ADC method is required:

unsigned int value;
unsigned char channel=1;
  switch (channel) {
    case 0: CHS2_bit=0; CHS1_bit=0; CHS0_bit=0; break;
    case 1: CHS2_bit=0; CHS1_bit=0; CHS0_bit=1; break;
    case 2: CHS2_bit=0; CHS1_bit=1; CHS0_bit=0; break;
    case 3: CHS2_bit=0; CHS1_bit=1; CHS0_bit=1; break;
    case 4: CHS2_bit=1; CHS1_bit=0; CHS0_bit=0; break;
    case 5: CHS2_bit=1; CHS1_bit=0; CHS0_bit=1; break;
    case 6: CHS2_bit=1; CHS1_bit=1; CHS0_bit=0; break;
    case 7: CHS2_bit=1; CHS1_bit=1; CHS0_bit=1; break;
  }
  ADON_bit=1;
  delay_ms(10);
  GO_NOT_DONE_bit=1;
  while(GO_NOT_DONE_bit);
  value=(ADRESH<<2)|(ADRESL>>6);
  if (value>1022) value=1023;

Do you imagine writing this code every-time that you want to ADC? What if you make a typo? Do you understand the risks of NOT standardize? Instead, use just this line of code when you require ADC (or any other function)_

ADC(channel);

Now that you see how simple is to use them, let's see the types of functions available:

Types of Functions

Void Function

The void function doesn't return any value (or result) after it has completed.

For example, the following code will take a piece of text and displays it in a LCD display. What it does is to take the variable puntero (which is a pointer) and prints all its characters, one by one in sequential order, until there is no more characters left.

void TextUART(const char *puntero) {
  while (*puntero!=0) { //!*puntero
       UART1_Write(*puntero);
       puntero++;
  }
}

Use void at the beginning of the function, then the name, and the variable input between parenthesis (). The open brackets specifies the beginning of the function. The first example has variable input but the second one does not.

void nameOfTheFunction(unsigned char variable1, int variable2) { //With variable input
//Function of the code goes here
}

void nameOfTheFunction() { //Without variable input
//Function of the code goes here
}

Call of the Function

These functions in C are called if there is variable input:

nameOfTheFunction(variable1, variable2);

and if there is not a variable input:

nameOfTheFunction();

Return Function

The return function will return a specified value back to the code (where it came from), once it has completed.

For example, the following code measures the voltage of a pin from a microcontroller. It is complex to explain the whole process but the idea is that the variable value returns the voltage back to the code.

unsigned int ADC(unsigned short channel) { //ADC stands for Analog-Digital Conversion
  unsigned int value;

  switch (channel) {
    case 0: CHS2_bit=0; CHS1_bit=0; CHS0_bit=0; break;
    case 1: CHS2_bit=0; CHS1_bit=0; CHS0_bit=1; break;
    case 2: CHS2_bit=0; CHS1_bit=1; CHS0_bit=0; break;
    case 3: CHS2_bit=0; CHS1_bit=1; CHS0_bit=1; break;
    case 4: CHS2_bit=1; CHS1_bit=0; CHS0_bit=0; break;
    case 5: CHS2_bit=1; CHS1_bit=0; CHS0_bit=1; break;
    case 6: CHS2_bit=1; CHS1_bit=1; CHS0_bit=0; break;
    case 7: CHS2_bit=1; CHS1_bit=1; CHS0_bit=1; break;
  }

  ADON_bit=1;
  delay_ms(10);
  GO_NOT_DONE_bit=1;
  while(GO_NOT_DONE_bit);
  value=(ADRESH<<2)|(ADRESL>>6);
  if (value>1022) value=1023;
  return value;
}

At the end of the function, write down return and the name of the variable. This is how the result of the function returns to the code where it was called.

Use the variable at the beginning of the function, then the name, and the variable input between parenthesis (). The open brackets specifies the beginning of the function. The first example has variable input but the second one does not.

int nameOfTheFunction(unsigned char variable1, int variable2) { //With variable input
  //Function of the code goes here
  return desiredVariable;
}

int nameOfTheFunction() { //Without variable input
  //Function of the code goes here
  return desiredVariable;
}

Call of the Function

X is any variable that requires the operation (and result) from the function. Make sure that x uses the same type of variable as the return in the function.

These functions in C are called if there is variable input:

x=nameOfTheFunction(variable1, variable2);

and if there is not a variable input:

x=nameOfTheFunction();

techZorro's Index of Content

Keep Reading!

2 Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.