C functions
A function is a group of statements that together perform a task. Every C program has at least one function, the main function main() , and all simple programs can define additional functions.
You can divide code into different functions. How to divide code into different functions is up to you, but logically, the division is usually based on each function performing a specific task.
functionDeclarationtells the compiler the function's name, return type, and parameters. A functiondefineprovides the actual body of the function.
The C standard library provides a large number of built-in functions that programs can call. For example, the function strcat() is used to concatenate two strings, and the function memcpy() is used to copy memory to another location.
Functions also have many other names, such as method, subroutine, or procedure, and so on.
Defining a function
The general form of a function definition in C is as follows:
return_type function_name( parameter list )
{
body of the function
}
In C, a function consists of a function header and a function body. All the components of a function are listed below:
- **Return type:** A function can return a value.return_type is the data type of the value returned by the function. Some functions perform required operations without returning a value; in this case, return_type is the keyword void.
- **Function name:** This is the actual name of the function. The function name together with the parameter list forms the function signature.
- **Parameters:** Parameters are like placeholders. When the function is called, you pass a value to the parameter, and this value is called the actual argument. The parameter list includes the type, order, and number of the function's parameters. Parameters are optional, meaning the function may contain no parameters.
- **Function body:** The function body contains a set of statements that define the task the function performs.
Example
The following are max() The source code of the function. This function has two parameters num1 and num2 and returns the larger of the two numbers:
/*
The function returns the larger of two numbers
*/
int
max
(
int
num1
,
int
num2
)
{
/*
Local variable declaration
*/
int
result
;
if
(
num1
num2
)
{
result
=
num1
;
}
else
{
result
=
num2
;
}
return
result
;
}
Function declaration
functionDeclarationtells the compiler the function name and how to call the function. The actual body of the function can be defined separately.
A function declaration includes the following parts:
return_type function_name( parameter list );
For the function max() defined above, the following is the function declaration:
int max(int num1, int num2);
In a function declaration, the parameter names are not important; only the parameter types are required, so the following is also a valid declaration:
int max(int, int);
When you define a function in one source file and call it in another, a function declaration is required. In this case, you should declare the function at the top of the file that calls it.
Calling a function
When you create a C function, you define what the function does, and then accomplish the defined task by calling the function.
When the program calls a function, program control is transferred to the called function. The called function executes the defined task, and when the function's return statement is executed, or the closing brace of the function is reached, program control is returned to the main program.
When calling a function, pass the required arguments; if the function returns a value, you can store the return value. For example:
Example
#include
<
stdio.h
/*Function declaration
*/
int
max
(
int
num1
,
int
num2
)
;
int
main
(
)
{
/*
Local variable definition
*/
int
a
=
100
;
int
b
=
200
;
int
ret
;
/*
Call the function to get the maximum value
*/
ret
=
max
(
a
,
b
)
;
printf
(
"
Max value is : %d
\
n
"
,
ret
)
;
return
0
;
}
/*
The function returns the larger of two numbers
*/
int
max
(
int
num1
,
int
num2
)
{
/*
Local variable declaration
*/
int
result
;
if
(
num1
num2
)
result
=
num1
;
else
result
=
num2
;
return
result
;
}
Put the max() function and the main() function together and compile the source code. When running the final executable, the following result is produced:
Max value is : 200
Function parameters
If a function wants to use parameters, it must declare variables that accept the parameter values. These variables are called the function'sformal parameters.
Formal parameters are like other local variables inside the function; they are created when entering the function and destroyed when exiting it.
When calling a function, there are two ways to pass arguments to the function:
| Call type | Description |
|---|---|
| call by value | This method copies the actual value of an argument into the formal parameter of the function. In this case, changes to the formal parameter inside the function do not affect the actual argument. |
| call by reference | With the pointer-passing method, the formal parameter is a pointer to the address of the actual argument; when you operate on what the formal parameter points to, it is equivalent to operating on the actual argument itself. |
By default, C usescall by valueto pass arguments. Generally, this means that code inside the function cannot change the actual arguments used to call the function.