C Function
In C, function is a set of statements or code block with a name, return type, parameters and function body used to perform specific task and only executes when it is called.
Using functions in a c program has following advantages
- No code duplicacy or reptation. means same function can be called wherever necessary in c program.
- Makes program modular. Functions are also called procedure.
- Increase readability.
- Easy to maintain code or debug.
- Changes or modification become easier.
Function Parts with Syntax
- Function Declaration
- Function Definition
- Function calling
Function Declaration
- It is providing information to c compiler, that a given function is created in program with specific name, return type and parameters.
- Function declaration has no function body and ends with semicolon. It must be declared first, so, compiler knows some function is declared and may defined or use later in program.
- Parameters are optional, and specified with only data type and separated by comma, variables are not used.
Syntax
<return type> <function name>(parameter1, parameter2);
Example
int addTwoNumbers(int, int);
Explanation
- return type is int.
- function name is addTwoNumbers.
- Parameter list is int, int.
Function Definition
- It should have same name, return type and parameters as function declaration.
- Parameters must be defined with data type and variable.
- It has function body which contains statements to perform task.
Syntax
<return type> <function name>(parameter1, parameter2)
{
// set of statements
}
Example
int addTwoNumbers(int a, int b){
int c = 0;
c = a + b;
printf("%d", c);
}
Function calling
To use a function in a c program, function needs to be called. Function call executes function, While calling a function return type is not used. Parameters value should be of given data type in function declaration.
Syntax
<function name>(parameter1, parameter2);
Example
addTwoNumbers(10, 12);
Return type
Function can return or also not return any value depends on its return type, On basis of return type function can return following type of values i.e. int, float, double, char, long, void.
Function Name
Name by which it is identified in the program. Function name should be unique and a valid c name, used to declare, define and call a function.
Parameter list
Parameters in function are variables which is used to accept input. Parameters can be of any data type separated by comma.
Statement set
These statements are inside function body, executes when a function is called and gives output or return a value.
Complete Example
#include<stdio.h>
//Function Declaration
int addTwoNumbers(int, int);
void main()
{
addTwoNumbers(20, 50); //Function call
}
// //Function Definition
int addTwoNumbers(int a, int b)
{
int c = 0;
c = a + b;
printf("%d", c);
}
Function with return type and Arguments
On basis of return type and Arguments function are of four types
- Function with no arguments and no return value.
- Function with no arguments and with return value.
- Function with argument and with no return value.
- Function with arguments and with return value.
Function Parameters
Parameters in function can be passed in two different ways.
- Pass by value
- Pass by reference
Pass by value
The value of Variable is passed as copy during function call to the function parameters.
Changing value of function parameters does not change the original variables value of function call.
#include<stdio.h>
int swapvalues(int, int);
void main()
{
int a1 = 20;
int a2 = 50;
printf("Before swap value of a1 is %d ", a1);
printf("\nBefore swap value of a2 is %d ", a2);
swapvalues(a1, a2); // Original values
printf("\nAfter swap value of a1 is %d ", a1);
printf("\nAfter swap value of a2 is %d ", a2);
}
int swapvalues(int a1, int a2)
{
// Values of variable changed during swap
int c = 0;
c = a1;
a1 = a2;
a2 = c;
}
Output:
Before swap value of a1 is 20
Before swap value of a2 is 50
After swap value of a1 is 20
After swap value of a2 is 50
Pass by reference
Address of variables are passed during function call to the function parameters.
Changing value of function parameters changes the original variables values because of sharing same variable address.
int swapvalues(int*, int*);
void main()
{
int a1 = 20;
int a2 = 50;
printf("Before swap value of a1 is %d ", a1);
printf("\nBefore swap value of a2 is %d ", a2);
swapvalues(&a1, &a2);
printf("\nAfter swap value of a1 is %d ", a1);
printf("\nAfter swap value of a2 is %d ", a2);
}
int swapvalues(int* a1, int *a2)
{
int c = 0;
c = *a1;
*a1 = *a2;
*a2 = c;
}
Output:
Before swap value of a1 is 20
Before swap value of a2 is 50
After swap value of a1 is 50
After swap value of a2 is 20