Definition: A function is a named block of statements designed to perform a specific task. A program can call the function whenever that task is required.
Definition: A function is a named block of statements designed to perform a specific task. A program can call the function whenever that task is required.
add() can perform the task of adding
two numbers.
Functions divide a large program into smaller, manageable parts. This makes programs easier to understand, test, reuse and maintain.
inputMarks(), calculateTotal() and
displayResult().
A function generally involves a declaration/prototype, a definition, and a function call.
| Part | Purpose | Example |
|---|---|---|
| Function Declaration | Tells the compiler about the function. | int add(int, int); |
| Function Definition | Contains the actual statements of the function. | int add(int a, int b) { ... } |
| Function Call | Requests execution of the function. | add(10, 20); |
Definition: A function declaration, also called a function prototype, tells the compiler the function's name, return type and parameters before the function is used.
int add(int, int);
declares a function named add that returns an
integer and accepts two integer parameters.
Definition: A function definition contains the statements that specify what the function actually does.
int add(int a, int b)
{
return a + b;
}
Definition: A function call is an expression used to execute the function and perform its task.
result = add(10, 20);
return_type function_name(parameter_list)
{
// statements
return value;
}
int square(int n) means the function
square accepts an integer and returns an integer.
Functions can be classified based on whether they take arguments and whether they return a value.
| Type | Arguments | Return Value |
|---|---|---|
| No arguments, no return value | No | No |
| Arguments, no return value | Yes | No |
| No arguments, returns a value | No | Yes |
| Arguments, returns a value | Yes | Yes |
Definition: This type of function does not receive any values from the calling function and does not return a value. It simply performs a specific task.
void welcome()
{
printf("Welcome to C Programming!");
}
Definition: This type of function receives values as arguments but does not return a value to the calling function.
void add(int a, int b)
{
printf("Sum = %d", a + b);
}
Definition: This type of function does not receive arguments but performs a task and returns a value to the caller.
int getNumber()
{
return 10;
}
Definition: This type of function receives one or more arguments, performs a task and returns the result to the caller.
int add(int a, int b)
{
return a + b;
}
The function type arguments + return value is very commonly used because data can be supplied to the function and the calculated result can be returned to the calling code.
int sum = add(10, 20);
passes two values and receives the calculated result.
Definition: Library functions are predefined functions provided by C libraries. A programmer can use them without writing their complete implementation.
printf() and scanf() are commonly used
library functions provided through standard headers.
Definition: User-defined functions are functions created by the programmer to perform a specific task required by the program.
calculateAverage() to calculate
the average marks of a student.
| Library Functions | User-Defined Functions |
|---|---|
| Provided by C libraries. | Created by the programmer. |
| Already implemented. | Implementation is written by the programmer. |
Example: printf() |
Example: add() |
Write a C program that uses a user-defined function to add two integers and return the result.
Create an add() function that accepts two integers
and returns their sum.
A function can receive data through parameters and return a result to the calling function. This helps divide a program into smaller reusable tasks.
#include <stdio.h>
int add(int a, int b); // function declaration
int main()
{
int x, y, result;
printf("Enter two numbers: ");
scanf("%d %d", &x, &y);
result = add(x, y); // function call
printf("Sum = %d
", result);
return 0;
}
int add(int a, int b)
{
return a + b; // return the calculated sum
}
Enter two numbers: 10 15 Sum = 25
The function add() receives two arguments and returns
the result. The returned value is stored in result
inside main().
| Function Type | Arguments | Return |
|---|---|---|
| No arguments, no return | No | No |
| Arguments, no return | Yes | No |
| No arguments, return | No | Yes |
| Arguments, return | Yes | Yes |
Watch a beginner-friendly explanation of functions and their types in C programming.
A short handwritten-style revision sheet for function types will be provided here.
Use the mind map for quick revision of declaration, definition, call and the four function types.