Functions 2025

Functions #

In C programming, a function is a block of code that performs a specific task. The task is often a repetitive task that is used often. Functions enable code reuse which helps make programs easier to read, understand, maintain and debug.

Early on we learned that every C program has at least one function, main(). We have also learned that we regularly use functions in our programs, these are printf() and scanf(). Both of these are functions. The code for these two functions is stored in the <stdio.h> header file. We have used both of these functions more than once in our programs. Behind both of these funcitons is a block of code that executes everytime we use printf() or scanf(). By creating these blocks of code as functions we are minising the size of our program.

Some of the benefits of using functions are:-

  • Programs can become modular.
  • Programs are easier to read and more efficient.
  • Editing becomes easier so programs are easier to maintain.

.

How to create a FUNCTION #

scanf() and printf() are standard library functions. These are built-in funcitons to C programming and are defined in a header file. Other functions that we have used are strcpy() from the <string.h> header file OR sqrt() from the <amth.h> header file.

Another type of function that can be created is a user-defined function. This is a funciton that user (YOU) creates. User defined funcitons are created to complete a task that is required to execute regulalry in a program but it has not been defined in any of the header files. Say for example the program must calculte a formula multipe times using 4 input values. Rather than running the same code multiple times, a function would be created for that block of code.

There are three stages to creating a function:-

  1. PROTOTYPE
  2. DEFINTION
  3. CALL

.

1. Function PROTOTYPE #

This is where the function is declared by the user to the compiler. We must declare to the compiler that there is a function in the following program. The declaration specifies:-

  • The function name
  • The parameters passed to the function
  • The data return type from the function

The general form of the function PROTOTYPE is:-

return_Type function_Name(parameter list);

An example of a function prototype declaration is shown below.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// Example of a function prototype declaration
#include <stdio.h>

int min(int a, int b); //Function Declaration

// main() function starts here
int main(void) {
int a, b, c;
a = 5;
b = 6;
  • The name of the function is min
  • The return type is int. The function will return an integer value it is finished.
  • The parameter list has TWO arguments named a and b and both are of type int.
    • This means the function will be passed TWO variables that are both integers.

.

Function Prototype

.

2. Function DEFINITION #

This is the actual block of code that the function will execute. It is a group of statements that will perform a specific task.

The general form of the function DEFINITION is:-

return_Type function_Name(parameter_list)
{ 
	//body of code of the function
}

An example of a function definition is shown below. This is the funciton definition for the function min prototype we saw previously.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// A Function definition for the Function called min
int min(int num1, int num2) // The FUNCTION will accept 2 parameters of integer type and return an integer
{
/* LOCAL variable declaration. This is LOCAL to this FUNCTION only */
int result;

if (num1 > num2)
result = num2;
else
result = num1;

return result;  //The function passes the value of result back to the main() function
}

.

3. Function CALL #

This is where the control of the program is transferred to the function that has been created by the user. To get the function to execute we must CALL the function.

The general form of the function CALL is:-

function_Name(parameter_list);

When a program CALLS a function the program control is transferred to the function that was called. The program control is handed back to the main() once the function has completed.

The diagram below illustrates the flow of a function call. In this example:-

  1. The function prototype is declared at the top of the program before the main() function.
  2. The function definition is defined at the end of the main() funciton.
  3. The function call occurs in the middle of the main() function.

After the function is called (step 1), the program control is then passed to the function, min(). The code defined in this function is then executed. Once the program reaches the end of the function it encounters a return statement with the variable name result. The function then returns program control to the main() function (step 2). The value of result is returned to the main() function and is stored in the variable, c.

Function Prototype