C Function

Summary: in this tutorial, you will learn about C function, which is one of the most important building block of C programming. This tutorial shows you what a function is, how to define your own functions and how to pass arguments to a function.

C function

Introduce to C function

A function is a named block of code designed to perform a specific task and may return a value to the calling function or program.

A function is a block of code that is assigned a unique name so that other functions can execute or call it.

A function is designed to perform a specific task e.g., sort an array, write data into a file or connect to a database system. By using functions, you can divide a big task into smaller and more manageable ones that make your code more testable and reusable.

When a program calls a function, the code inside the function executes, and it may return a value back e.g., the rand() function returns a random number that falls within a range of numbers.

C function example

See the following example:

The following line is the function prototype:

It specifies:

  • Return type of the function is int
  • The function name: max
  • A list of parameters that you can pass to the function.

The max() function accepts two integer parameters and returns the maximum one.

A variable that you pass to the function is called an argument e.g., x and y are arguments. a and b are called parameters because they are a part of the function signature. Programmers often use argument and parameter interchangeably.

The following code is the function definition:

The function definition has two parts:

  • Function header is identical to the function prototype, which consists of return type, function name and parameter list.
  • Function body is enclosed in braces {}. It contains a set of statements.

To call a function, you need to pass all arguments to the function inside parentheses as follows:

Because the max() function returns an integer value, you can assign it to the m variable when you call it.

Passing arguments to a function

Passing arguments by values

Passing argument to a function by values means all the arguments that you pass to the function are copied into copied variables. The function works on the copied variables only therefore arguments does not change by the function.

You pass arguments to a function by value when you don’t want the function change the value of the arguments. See the following example:

The following is the output of the program:

The values of x and y does not change because we pass them to the swap() function by values.

Pass arguments to a function by pointers

Recall that a pointer refers to the memory address of a variable therefore when you pass a pointer to a function, the function work on the value of the memory address stored in the pointer, which affects the variable that the pointer points to.

You pass arguments to a function by pointers when you want to change the values of the arguments. Let’s rewrite the swap() function above:

The output of the program:

Passing an array to a function

The array name is a pointer that points to the first element of the array. To pass an array to a function, you just need to pass the array name. The following example illustrates how to pass an array to a function.

The output of the program:

C inline functions

C allows you to define special functions called inline functions. An inline function is a relatively small function that the compiler will optimize it to ensure that the inline function will execute as fast as possible. The compiler will copy the code of an inline function to the calling function when it reaches an inline function.

To define an inline function, you use the inline keyword as follows:

You use inline function as the normal functions.

In this tutorial, you have learned about C function, how to define your own functions and how to pass arguments to a function in different ways.