Function, Parameter/Arguments in c
Definition Of function :
A block of code that performs a calculations and return a value. when a program is very long and repeating a same code many times then we try to cut it into different parts. every c program at least has one function, which is main (). function definition in c programming language consist of a function header and a function body.
Part of a Function :
- Return Value - a function may return a value. return value is type of value returned by function. some functions perform the desired operations without returning a value. if function is not going to return a value, the return type may be "void".
- Function name - the actual name of a function. all variable naming conversions are aplicable for declaring valid function name.
- Parameters - when the function is invoked, you pass the value to the parameter. comma-separated list of types and names of parameters. parameters field is optional. if no parameters is passed then no need to write this field.
- Value - it is value returned by function upon termination. function will not return a value if return type is "void".
Example of Function :
#include <stdio.h>
int sum (int x, int y)
{
return (x + y);
}
int main ()
{
int i = 5;
int j = 8;
int result;
result = sum (i, j);
printf ("result : %d \n", result);
return 0;
}
Explanation of example of function :
Return type = integer
Calling function = main
Actual parameter 1 = i
Actual parameter 2 = j
Formal Parameter 1 = x
Formal parameter 2 = y
Function will returned = x + y
Return Value = result
Statement = result = sum (i, j)
Local declaration = int i = 5;
int j = 8;
int result;
Parameters and Argument :
- Parameter - the variable which is part of the method's declaration.
- arguments - an expressing used when calling the method.
Void function (int i, float j) // i and j are the parameters
{
// do things
}
Void parg ()
{
int x = 10;
function (x, 3.14); // x and 3.14 are the arguments
}