The data we want to send to a function when it is called to be executed is known as an argument or a parameter of the function. Within the function, parameters function as variables. After the function name, parameters are listed between parentheses. You can enter as many options as you like; simply comma-separate them. Here is a function’s fundamental syntax.
return_type functionName(parameter1, parameter2) {
// body of the function
}
What is the return type?
- The data type the function should return once it has been executed is specified by the return type. In cases where the function must return nothing, it may also be a void.
Different ways to define a function
1.Without arguments and without return value
- In this function, we are not required to provide the function with any value (arguments or parameters) and are not even required to get any value in return.
One illustration of such features might be,
#include <stdio.h>
void func()
{
printf("This function doesn't return anything.");
}
int main()
{
func();
}
Output: This function doesn’t return anything.
2.Without arguments and with the return value.
- In these kinds of functions, we don’t need to provide the function with any values or arguments, but in exchange, we receive something from it, i.e. some value.
One illustration of such features would be
#include <stdio.h>
int func()
{
int a, b;
scanf("%d", &a);
scanf("%d", &b);
return a + b;
}
int main()
{
int ans = func();
printf("%d", ans);
}
Input:8,5
Output:13
3.With arguments and without a return value.
- In this kind of function, we supply the function with arguments or parameters, but we receive nothing in return.
One illustration of such features might be,
#include <stdio.h>
void func(int a, int b)
{
printf("%d", a * b);
}
int main()
{
func(2, 3);
}
Output:6
4.With arguments and with the return value
- In these functions, we pass arguments to a function and receive a value back in return.
One illustration of such features might be,
#include <stdio.h>
int func(int a, int b)
{
return a + b;
}
int main()
{
int ans = func(2, 3);
printf("%d", ans);
}
Output:5