Function in C is defined as set of statements that perform some specific task on calling it. It provides modularity and code re-usability. { } braces are used to enclose the programming statements of a function. It is also the basic building block of a C program.
Hence, the compiler is instructed by a statement (function call) in which function name and parameters are used.
Syntax of function is divided into 3 parts:-
- Function declaration
- Function definition
- Function calling
How does C function work?
This working can be divided in the following steps-
- Declaring function: Here, we declare a function. Also, we define it’s return types and parameters.
- Defining function: Here, actual work or purpose of function is defined and it also includes logic of the program.
- Calling function: Here, we call that functions by passing arguments in it.
- Executing function: Here, we can run all the statements inside function to get final result.
- Returning value: Here, the calculated value is returned after execution of the function.
- Exiting function: It is the final step where all allocated memory, functions,etc is destroyed before the full control is given to main function.
Note: C function returns only one value. We have to use pointers or structures to return multiple values.
· In C, functions may or might not return values to the calling functions
· Hence, there are 4 types of functions:
- Function that has no arguments and no return value
- Function that has no arguments but has return value.
- Function that has argument but has no return value.
- Function that has arguments and return value.
#include <stdio.h>
void average(int, int, int, int, int);
int main() {
int p, q, r, s, t;
printf("\nCalculating average of five numbers:");
printf("\nEnter 5 numbers: ");
scanf("%d %d %d %d %d", &p, &q, &r, &s, &t);
average(p, q, r, s, t);
return 0;
}
void average(int p, int q, int r, int s, int t) {
float avg;
avg = (p + q + r + s + t) / 5.0;
printf("Calculated average is: %f", avg);
}
/*
Output:
Calculating the average of five numbers:
Enter 5 numbers: 50 60 70 80 90
Calculated average is: 70.000000
*/
User-defined Functions:-
These are also called as “tailor-made functions”. Functions that the programmer creates and are improved and modified according to the need of that programmer are called user defined functions.
Library Functions:-
These are also called “built-in functions”. These are the functions that are already present in a compiler package.
All C standard library functions are defined inside different header files and these header files are saved with the extension .h
Passing arguments to C function can be done in 2 ways:
- Pass by Value:
Here, values are copied from actual parameters into formal function parameters
#include<stdio.h>
void swap(int a, int b) {
int temp = a;
a = b;
b = temp;
}
// Driver code
int main() {
int a = 4, b = 5;
printf("Before swapping, value of a and b is: %d, %d\n", a, b);
swap(a, b);
printf("After swapping, value of a and b is: %d, %d", a, b);
return 0;
}
/*
Output:
Before swapping, value of a and b is: 4, 5
After swapping, value of a and b is: 4, 5
*/
2. Pass by Reference:
Here, same locations are referred by the caller’s actual parameters and the function’s actual parameters.
#include<stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
// Driver code
int main() {
int a = 4, b = 5;
printf("Before swapping, value of a and b is: %d, %d\n", a, b);
swap(&a, &b);
printf("After swapping, value of a and b is: %d, %d", a, b);
return 0;
}
/*
Output:
Before swapping, value of a and b is: 4, 5
After swapping, value of a and b is: 5, 4
*/
Summary:-
- Function can be reused as many times as we want inside a program.
- We need to call a function to use it.
- function_name, return type and parameters are included in function declaration.
- Body of the function is included in function definition.
- Function has 2 types- User-defined and library functions.
- According to the values passed, two types are- call by value and call by reference.