Summary: in this tutorial, you will learn about the C functions and how to define your functions.
Introduce to C functions. #
In programming, you often organize a large program in smaller pieces. Each piece is more manageable than the original program. To divide the program into smaller pieces, you use functions.
A function is a named block of code that performs a specific task.
In the previous tutorials, you have learned various C functions such as printf() and scanf() from the stdio.h library.
To output a piece of text to the standard output, you call the printf() function. When you call the printf() function, C will execute the code of the printf() function.
A function may accept inputs. The inputs of a function are called arguments. For example, the printf() function accepts multiple arguments:
printf("Counter %d", counter);Code language: JavaScript (javascript)In this example, the "Counter %d" and counter variable are the inputs of the printf() function. They are also called the arguments of the printf() function.
A function may return a value. For example, the sqrt() function from the math.h library returns the square root of a number:
float number = 81;
float result = sqrt(number); // 9Code language: JavaScript (javascript)Besides providing standard functions, C allows you to define your functions. These functions are known as custom functions.
You must specify a function prototype and function definition to define a custom function.
Function prototype #
The following shows how to define a function prototype:
return_type function_name(parameter_list);In this syntax:
- First, specify the type of return value. If a function doesn’t return any value, you use the
voidkeyword. - Second, provide the name of the function. The function name should describe exactly what it does.
- Third, specify the parameters within the parentheses that follow the function name. If a function has multiple parameters, you use a comma (,) to separate two parameters. If the function has no parameters, you can leave it empty or use the
voidkeyword.
Function definition #
The syntax of the function definition is as follows:
return_type function_name(parameter_list)
{
// statements
}Code language: JavaScript (javascript)The function definition looks like the function prototype, except it has a function body.
The function body is wrapped between curly braces {} that follow the parentheses. Inside the function body, you can have one or more statements.
Return value
To return a value from a function, you use the return statement followed by a return value:
return expression;Code language: JavaScript (javascript)When the program encounters the return statement, it immediately stops the function and returns the value to the caller.
If you want to stop the function without returning a value, you can omit the expression in the return statement like this:
return;Code language: JavaScript (javascript)Typically, you place the function prototype at the beginning of the file before the main() function. You define the function after the main() function as follows:
#include <stdio.h>
// function prototypes
return_type function_name(parmeter_list);
int main()
{
// statements
return 0;
}
// function definition
return_type function_name(parmeter_list)
{
// function body
}
Code language: PHP (php)When you call a function, the C compiler checks the function call against the function prototype. If they don’t match, the compiler issues a compilation error.
For example, if the function prototype has three parameters, but the function call only has two, the compiler will issue a compilation error.
Also, if the function prototype and function definition disagree, the compiler will issue a compilation error.
C function example #
The following example illustrates how to create a custom function. It prompts for two numbers and displays the larger number.
#include <stdio.h>
int max(int a, int b);
int main()
{
int x, y, m;
// prompt for two integers
puts("Enter two numbers to find the max:");
scanf("%d", &x);
scanf("%d", &y);
// find the max
m = max(x, y);
printf("The max of %d and %d is %d", x, y, m);
return 0;
}
// return the max of two numbers
int max(int a, int b)
{
if (a > b)
return a;
else
return b;
}Code language: C++ (cpp)How it works.
First, declare a function prototype that finds the maximum number of two numbers:
int max(int a, int b);The max() function has two parameters of type integer (int). It also returns an integer (int).
Second, define the max() function definition after the main() function:
// return the max of two numbers
int max(int a, int b)
{
if (a > b)
return a;
else
return b;
}Code language: JavaScript (javascript)The max() function returns a if a is greater b. Otherwise, it returns b.
Third, prompt for two integers in the main() function:
puts("Enter two numbers to find the max:");
scanf("%d", &x);
scanf("%d", &y);Code language: JavaScript (javascript)Fourth, call the max() function to get the max of the two input numbers.
// find the max
m = max(x, y);
Code language: JavaScript (javascript)Finally, show the max of the two numbers:
printf("The max of %d and %d is %d", x, y, m);Code language: JavaScript (javascript)Summary #
- A function is a named block of code that performs a specific task.
- To define a custom function, specify the function prototype and function definition.
- Use the
returnstatement to return a value from a function.