Lets first look into need of function with an example
Program: find factorial of 4, 6 and 8.
#include <stdio.h> int main() { int n = 4; int counter; int fact = 1; for(counter = 1, fact = 1; counter <= n; counter++) { fact = fact * counter; } printf("Factorial of %d is %d\n", n, fact); n = 6; for(counter = 1, fact = 1; counter <= n; counter++) { fact = fact * counter; } printf("Factorial of %d is %d\n", n, fact); n = 8; for(counter = 1, fact = 1; counter <= n; counter++) { fact = fact * counter; } printf("Factorial of %d is %d\n", n, fact); return 0; }
Factorial of 4 is 24 Factorial of 6 is 720 Factorial of 8 is 40320
Now you can see directly that there is lot of repetition in the code. Reason is logic of finding factorial does not change with input number n. Hence it makes sense to logically abstract finding factorial under a function.
Function has 3 parts to it -
- Function Prototype
- Function Definition
- Function Call
Block diagram of the generic function is as follows
As you see function takes lot of input and gives one output. (In some way quite similar to mathematical function!). Inputs are referred to as arguments and output is referred to as result. Above function func take a1, a2 and an.r is the result of the function. Same block diagram is represented in code as follows.
Syntax -
Function Prototype
return_t func(argument1_t, argument2_t, argumentn_t);
Function Definition
return_t func(argument1_t a1, argument2_t a2, argumentn_t an)
{
// Body of function
return ret_value;
}
Function Call
x = func(v1,v2,vn);
Explanation -
- Function Prototype gives the outer description of the block diagram.
- Name of function
- It tells what are types of arguments it can take.
- What is return type you can expect.
- Function prototype helps compiler to understand function call and it will help to verify if the function call is correct.
- Function Definition gives complete description of the block diagram.
- Name of function
- It tells what are types of arguments it can take.
- What is return type you can expect.
- What is operation carried out in function
- What is the value that is returned.
- Function Call gives the instance of use of function.
- What are the values of the arguments.
- Where should the return value assigned into.
Hence Factorial function can be re-written as follows
#include <stdio.h> int factorial(int); /* Function prototype */ int main() { int f; f = factorial(4); printf("Factorial of 4 is %d\n", f); f = factorial(6); printf("Factorial of 6 is %d\n", f); f = factorial(8); printf("Factorial of 8 is %d\n", f); return 0; } int factorial(int n) { int counter, ret_fact; for(counter = 1, ret_fact =1; counter <= n; counter++) { ret_fact *= counter; } return ret_fact; }
Note that
- n, counter and ret_fact are variables local to function they cannot be accessed outside function. (More in future article : C Programming #33: Global Variable)
- Internally C uses stack to establish function. (More in future article: C Programming #37: Function - Stack)
Function Prototype
return_t func(void);
void signifies that func does not take any arguments here.
Function Definition
return_t func(void)
{
// Body of function
return ret_value;
}
Function Call
x = func();
There is possibility that some function do not return anything then -
Function Prototype
void func(argument1_t, argument2_t, argumentn_t);
Function Definition
void func(argument1_t a1, argument2_t a2, argumentn_t an)
{
// Body of function
return ret_value;
}
Function Call
func(v1,v2,vn);
You can of course guess how would it look if some function takes neither arguments nor returns value.
Links
Next Article - C Programming #33: Global VariablePrevious Article - C Programming #31: - Typedef part1
All Articles - C Programming
No comments :
Post a Comment