Utilizor
Contact Us
HomeC TutorialC User Functions

CC User Functions

Creating blocks of code.

C User Functions

A function is a block of code which only runs when it is called.

You can pass data, known as parameters, into a function.

Functions are used to perform certain actions, and they are important for reusing code: Define the code once, and use it many times.

Create a Function

To create (often referred to as declare) a function, specify the name of the function, followed by parenthesis ():

void myFunction() {
  // code to be executed
}

Examples

Call a Function

c example

Creating and calling a simple function.

#include <stdio.h>

// Create a function
void myFunction() {
  printf("I just got executed!");
}

int main() {
  myFunction(); // call the function
  return 0;
}

Multiple Calls

c example

Calling the same function multiple times.

#include <stdio.h>

void myFunction() {
  printf("I just got executed!\n");
}

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

Test Your Knowledge

1. How do you create a function in C?

2. Which keyword indicates a function does not return a value?