User Defined Functions in C

In this lesson, you will learn about user-defined functions in C. Basically, a function is a block of code that performs specific actions or tasks. Besides pre-defined system functions, you can also create your own, known as user-defined functions.

A simple User-Defined Functions in C

Let’s create a very basic function that prints something on the screen.

Example of User-Defined functions in C

void printSomething() {
  printf("Hello World!");
}

Then call this function from the main()

#include <stdio.h>

int main() {

  printSomething();

  return 0;
}

All you have to do is to define a function. Once defined, you can call it from any part of the code.

Passing Parameters

Passing parameters in a function is simple. For example, let’s say that we need a function that adds two integers that we passed.

Example of passing parameters in C

int addNumbers(int firstNumber, int secondNumber) {
  
  int result;
  
  result = firstNumber + secondNumber;

  printf("The result is: %d", result);
}

Now call the function

#include <stdio.h>

int main() {
 
  int a, b;
  a = 5;
  b = 8;

  addNumbers(a, b);

  return 0;
}

Output

The result is: 13

Return Statement in C

Using return will terminate the function and return the value. Therefore, it’s always used at the end of the function. Let’s modify the function from above so that it only provides the sum, instead of printing the result.

Example of Return statement in C

int addNumbers(int firstNumber, int secondNumber) {
  
  int a;
  a = firstNumber + secondNumber;

  return a;
}

Call the function:

#include <stdio.h>

int main() {
    
  int a, b, result;
  a = 5;
  b = 8;

  result = addNumbers(a, b);

  printf("The result is: %d", result);

  return 0;
}

Output

The result is: 13

Function Declaration in C

From these examples, you can see the structure of the function declaration. It consists of the function name, return type, and parameters.

  • The name of the function is addNumbers
  • The return type is int
  • Passed arguments are int

Basically, the syntax of the function declaration looks like this:

returnType functionName (type1 argument1, type2 argument2, type3 argument3, …);

Please note that the type of arguments passed to the function must match the formal parameters. For example, if you pass a string where the int is required, the compiler will throw an error. Also, you need to pass the exact number of arguments that the function requires.

Summary

  • Functions reduce the size of the code because they replace duplicate sets of statements.
  • By using functions, your code becomes easier to debug and trace errors.
  • Functions improve the readability of your code.
  • If the function is not meant to return any value, its return type should be void – like in the first example.
  • You should always use the return statement at the end of the function since it automatically terminates the function and returns the value.

 

Back to: Learn C Programming > Functions in C

Leave a Reply

Your email address will not be published. Required fields are marked *