Functions in C
Functions in C
Functions in C
A function in C is a block of organized reusuable code that is performs a single
related action. Every C program has at least one function, which is main(), and all
the most trivial programs can define additional functions.
When the algorithm of a certain problem involves long and complex logic, it is
broken into smaller, independent and reusable blocks. These small blocks of code are
known by different names in different programming languages such as a module, a
subroutine, a function or a method.
You can divide up your code into separate functions. How you divide up your code
among different functions is up to you, but logically the division is such that each
function performs a specific task.
A function declaration tells the compiler about a function's name, return type, and
parameters. A function definition provides the actual body of the function.
The C standard library provides numerous built-in functions that your program can
call. For example, strcat() to concatenate two strings, memcpy() to copy one
memory location to another location, and many more functions.
Modular Programming in C
Functions are designed to perform a specific task that is a part of an entire process.
This approach towards software development is called modular programming.
The main advantage of this approach is that the code becomes easy to follow,
develop and maintain.
Library Functions in C
https://www.tutorialspoint.com/cprogramming/c_functions.htm 1/7
6/16/24, 12:26 PM Functions in C
These functions perform a predefined task and can be called upon in any program as
per requirement. However, if you don't find a suitable library function to serve your
purpose, you can define one.
Defining a Function in C
In C, it is necessary to provide the forward declaration of the prototype of any
function. The prototype of a library function is present in the corresponding header
file.
For a user-defined function, its prototype is present in the current program. The
definition of a function and its prototype declaration should match.
After all the statements in a function are executed, the flow of the program returns
to the calling environment. The function may return some data along with the flow
control.
Function Declarations in C
A function declaration tells the compiler about a function name and how to call the
function. The actual body of the function can be defined separately.
For the above defined function max(), the function declaration is as follows −
Parameter names are not important in function declaration only their type is
required, so the following is also a valid declaration −
A function declaration is required when you define a function in one source file and
you call that function in another file. In such cases, you should declare the function
at the top of the file calling the function.
https://www.tutorialspoint.com/cprogramming/c_functions.htm 2/7
6/16/24, 12:26 PM Functions in C
Parts of a Function in C
The general form of a function definition in C programming language is as follows −
Return Type − A function may return a value. The return_type is the data
type of the value the function returns. Some functions perform the desired
operations without returning a value. In this case, the return_type is the
keyword void.
Function Name − This is the actual name of the function. The function name
and the parameter list together constitute the function signature.
A function in C should have a return type. The type of the variable returned by the
function must be the return type of the function. In the above figure, the add()
function returns an int type.
In this program, we have used a user-defined function called max(). This function
takes two parameters num1 and num2 and returns the maximum value between
the two −
#include <stdio.h>
https://www.tutorialspoint.com/cprogramming/c_functions.htm 3/7
6/16/24, 12:26 PM Functions in C
return result;
}
int main(){
printf("Comparing two numbers using max() function: \n");
printf("Which of the two, 75 or 57, is greater than the other? \n");
printf("The answer is: %d", max(75, 57));
return 0;
}
Output
When you runt this code, it will produce the following output −
Calling a Function in C
While creating a C function, you give a definition of what the function has to do. To
use a function, you will have to call that function to perform the defined task.
To call a function properly, you need to comply with the declaration of the function
prototype. If the function is defined to receive a set of arguments, the same number
and type of arguments must be passed.
When a function is defined with arguments, the arguments in front of the function
name are called formal arguments. When a function is called, the arguments
passed to it are the actual arguments.
https://www.tutorialspoint.com/cprogramming/c_functions.htm 4/7
6/16/24, 12:26 PM Functions in C
When a program calls a function, the program control is transferred to the called
function. A called function performs a defined task and when its return statement is
executed or when its function-ending closing brace is reached, it returns the program
control back to the main program.
To call a function, you simply need to pass the required parameters along with the
function name. If the function returns a value, then you can store the returned
value. Take a look at the following example −
#include <stdio.h>
/* function declaration */
int max(int num1, int num2);
return 0;
}
https://www.tutorialspoint.com/cprogramming/c_functions.htm 5/7
6/16/24, 12:26 PM Functions in C
return result;
}
Output
We have kept max() along with main() and compiled the source code. While running
the final executable, it would produce the following result −
From inside the main() function, other functions are called. The main() function can
call a library function such as printf(), whose prototype is fetched from the header
file (stdio.h) or any other user-defined function present in the code.
Note: In C, any function can call any other function, any number of times. A
function can call itself too. Such a self-calling function is called a recursive
function.
Function Arguments
If a function is to use arguments, it must declare variables that accept the values of
the arguments. These variables are called the formal parameters of the function.
Formal parameters behave like other local variables inside the function and are
created upon entry into the function and destroyed upon exit.
While calling a function, there are two ways in which arguments can be passed to a
function −
1 Call by value
This method copies the actual value of an argument into the formal
parameter of the function. In this case, changes made to the parameter
https://www.tutorialspoint.com/cprogramming/c_functions.htm 6/7
6/16/24, 12:26 PM Functions in C
Call by reference
This method copies the address of an argument into the formal parameter.
2 Inside the function, the address is used to access the actual argument
used in the call. This means that changes made to the parameter affect
the argument.
By default, C uses call by value to pass arguments. In general, it means the code
within a function cannot alter the arguments used to call the function.
https://www.tutorialspoint.com/cprogramming/c_functions.htm 7/7