1. C function declaration or function prototype
int sum (int a, int b);
int sum(int , int);
2. C function definition
Function syntax:
<returnType> <functionName> (parameters list)
{
function body;
}
From the above syntax we can see that function has below parts:
1. function name: Function name is mandatory for a function. A function name should be short and meaningful.
2. function return type: This is a mandatory parameter. The return type gives the data type of the value that is going to be returned from the function. If the function is not returning anything, then it should be declared as void.
3. function parameters: This is an optional parameter. A function can accept a input parameter or it might not accept a parameter.
4. function body: In function body, we write a set of statements that are needed to perform to get the desired output from function.
Example:
int sum(int a , int b)
{
return(a + b);
}
In the above function, “sum” is the function name, it returns “int” data type and it takes 2 int type parameters
In C language, all the function definition should be defined above main(). So that the compiler will get to know before that function is being executed.
If you are defining function after main(), then you have to declare the function before main() and define after main(). Else you will get the below warning:
warning: implicit declaration of function ‘sum’ [-Wimplicit-function-declaration]
3. Calling a function:
1. Function should be called by the respective function name.
2. You need to pass the exact function parameters and the type.
3. If the function is returning a value, then the return value should be stored in a variable of same type.
Example:
result = sum(10, 20);
Example: Full example of a function in C:
#include<stdio.h>
int sum(int a, int b); //Function declaration
int main()
{
int result = 0;
result = sum(10, 20); //Calling a function
return 0;
}
int sum(int a, int b) //Function definition
{
return (a + b );
}
4. Types of functions in C
Type 1. Function with no arguments (parameter) and no return value
#include<stdio.h>
void sum();
int main()
{
sum();
return 0;
}
void sum()
{
printf("Called sum function\n");
}
Type 2. Function with arguments (parameter) and no return value
#include<stdio.h>
void sum(int a, int b);
int main()
{
sum(10, 20);
return 0;
}
void sum(int a, int b)
{
printf("The sum is = %d\n",(a+b));
}
Type 3. Function with no arguments (parameter) and with return value
#include<stdio.h>
int sum();
int main()
{
int result = 0;
result = sum(10, 20);
printf("The result is = %d\n", result);
return 0;
}
int sum()
{
return(10 + 20);
}
Type 4. Function with with arguments (parameter) and with return value
#include<stdio.h>
int sum(int a, int b);
int main()
{
int result = 0;
result = sum(10, 20);
printf("The result is = %d\n", result);
return 0;
}
int sum(int a, int b)
{
return (a+b);
}
Types of function call
Before we go into the topic, let us understand the type of arguments.
There are two types of arguments.
1. Actual arguments: The arguments that we are sending while calling a function is called as Actual arguments.
Example:
sum (a, b);
In the above example, we are calling sum function. The parameters a, b are called as actual arguments.
2. Formal arguments: The arguments that are used in function definition are called as Formal arguments.
Example:
int sum( int c, int d){
return (c+ d);
}
In the above example, the arguments c, d are called as formal arguments.
There are 2 types of function calls in C:
1. call by value
2. call by reference
1. Call By value:
In this type, when we make a function call using arguments, the actual arguments are copied into formal arguments.
Hence any changes in the formal arguments will not reflect in the actual arguments after the function has completed it’s execution.
Example:
#include<stdio.h>
void swap(int c, int d);
int main()
{
int a = 10, b = 20;
printf("The value of a = %d b = %d before function call \n", a, b);
swap(a, b);
printf("The value of a = %d b = %d after function call \n", a, b);
return 0;
}
void swap(int c, int d)
{
int temp = 0;
temp = c;
c = d;
d = temp;
printf("The value of a = %d b = %d in swap function \n", c, d);
}
Output:
The value of a = 10 b = 20 before function call
The value of a = 20 b = 10 in swap function
The value of a = 10 b = 20 after function call
Hence from the above we can see that the actual arguments are not changed due to formal arguments.
Call by reference:
In this type, we send the address of the actual arguments, instead of values. Hence if there is a change in formal arguments, that will be reflected in actual arguments also.
Example:
#include<stdio.h>
void swap(int *c, int *d);
int main()
{
int a = 10, b = 20;
printf("The value of a = %d b = %d before function call \n", a, b);
swap(&a, &b);
printf("The value of a = %d b = %d after function call \n", a, b);
return 0;
}
void swap(int *c, int *d)
{
int temp = 0;
temp = *c;
*c = *d;
*d = temp;
printf("The value of a = %d b = %d in swap function \n", *c, *d);
}
Output:
The value of a = 10 b = 20 before function call
The value of a = 20 b = 10 in swap function
The value of a = 20 b = 10 after function call
Form the output we can infer that there is change in the actual arguments when we change formal arguments.
6. More function examples
Example: Function as an argument
#include<stdio.h>
int double_num(int num)
{
return (num * 2);
}
int square(int num)
{
return num * num;
}
int main()
{
int num = 20;
int result = double_num(square(num)); // sending a function as an argument.
// Here the square of the number is calculated.
// Then double of that number is calculated
// Square of 20 = 400
//Double of 400 is 800 hence the result.
printf("The result is %d\n", result );
}
Output:
The result is 800
Example: Function with decision statement
#include<stdio.h>
int get_num()
{
int num;
printf("Enter a number \n");
scanf("%d", &num);
return num;
}
int main()
{
// below we are calling a function inside if statement.
if (get_num() % 2 == 0)
{
printf("The number is even\n");
}
else
{
printf("The number is odd\n");
}
return 0;
}
Output
Enter a number
4
The number is even
Example: Function with looping statement
#include<stdio.h>
int get_num()
{
int num;
printf("Enter a number \n");
scanf("%d", &num);
return num;
}
int main()
{
// below we are calling a function inside while statement.
// till you enter -ve value, it will keep on looping
while (get_num() > 0 )
{
printf("The number is greater than 0\n");
}
printf("The number is lesser than or equal to 0. Hence exit\n");
return 0;
}
Output:
Enter a number
3
The number is greater than 0
Enter a number
4
The number is greater than 0
Enter a number
-1
The number is lesser than or equal to 0. Hence exit.
7. Recursion Function
A function that calls itself is called as a recursion function.
Only user defined functions are involved in recursion.
In recursion, there has to be a base condition, that will make to break the loop. Else it will become infinite recursive calls.
Example: Get the factorial of a number using recursion
#include<stdio.h>
int factorial_recursion(int num)
{
if(num == 1) return num; // base condition
else
return num * (factorial_recursion(num -1));
}
int main()
{
int num = 5;
printf("The factorial of 5 is %d \n", factorial_recursion(5));
}
Output:
The factorial of 5 is 120