A function is a group of statements that together perform a specific task. Every C program has at least one function, which is main().
Why use function ?
Function are used for divide a large code into module, due to this we can easily debug and maintain the code. For example if we write a calculator programs at that time we can write every logic in a separate function (For addition sum(), for subtraction sub()). Any function can be called many times.
Advantage of Function
- Code Re-usability
- Develop an application in module format.
- Easily to debug the program.
- Code optimization: No need to write lot of code.
Type of Function
There are two type of function in C Language. They are;
- Library function or pre-define function.
- User defined function.
Library function
Library functions are those which are predefined in C compiler. The implementation part of pre-defined functions is available in library files that are .lib/.obj files. .lib or .obj files are contained pre-compiled code. printf(), scanf(), clrscr(), pow() etc. are pre-defined functions.
Limitations of Library function
- All predefined function are contained limited task only that is for what purpose function is designed for same purpose it should be used.
- As a programmer we do not having any controls on predefined function implementation part is there in machine readable format.
- In implementation whenever a predefined function is not supporting user requirement then go for user defined function.
User defined function
These functions are created by programmer according to their requirement for example suppose you want to create a function for add two number then you create a function with name sum() this type of function is called user defined function.
Defining a function.
Defining of function is nothing but give body of function that means write logic inside function body.
Syntax
return_type function_name(parameter) { function body; }
- Return type: A function may return a value. The return_type is the data type of the value the function returns.Return type parameters and returns statement are optional.
- Function name: Function name is the name of function it is decided by programmer or you.
- Parameters: This is a value which is pass in function at the time of calling of function A parameter is like a placeholder. It is optional.
- Function body: Function body is the collection of statements.
Function Declarations
A function declaration is the process of tells the compiler about a function name. The actual body of the function can be defined separately.
Syntax
return_type function_name(parameter);
Note: At the time of function declaration function must be terminated with ;.
calling a function.
When we call any function control goes to function body and execute entire code. For call any function just write name of function and if any parameter is required then pass parameter.
Syntax
function_name(); or variable=function_name(argument);
Note: At the time of function calling function must be terminated with ';'.
Example of Function
#include<stdio.h> #include<conio.h> void sum(); // declaring a function clrsct(); int a=10,b=20, c; void sum() // defining function { c=a+b; printf("Sum: %d", c); } void main() { sum(); // calling function }
Output
Sum: 30
Call by Value and Call by Reference
On the basis of arguments there are two types of function are available in C language, they are;
- With argument
- Without argument
If a function take any arguments, it must declare variables that accept the values as a arguments. These variables are called the formal parameters of the function. There are two ways to pass value or data to function in C language which is given below;
- call by value
- call by reference
GIF Animation
Now we can understand about call by reference and call by value by using Animated images.
Call by value
In call by value, original value can not be changed or modified. In call by value, when you passed value to the function it is locally stored by the function parameter in stack memory location. If you change the value of function parameter, it is changed for the current function only but it not change the value of variable inside the caller method such as main().
Call by value
#include<stdio.h> #include<conio.h> void swap(int a, int b) { int temp; temp=a; a=b; b=temp; } void main() { int a=100, b=200; clrscr(); swap(a, b); // passing value to function printf("\nValue of a: %d",a); printf("\nValue of b: %d",b); getch(); }
Output
Value of a: 200 Value of b: 100
Call by reference
In call by reference, original value is changed or modified because we pass reference (address). Here, address of the value is passed in the function, so actual and formal arguments shares the same address space. Hence, any value changed inside the function, is reflected inside as well as outside the function.
Example Call by reference
#include<stdio.h> #include<conio.h> void swap(int *a, int *b) { int temp; temp=*a; *a=*b; *b=temp; } void main() { int a=100, b=200; clrscr(); swap(&a, &b); // passing value to function printf("\nValue of a: %d",a); printf("\nValue of b: %d",b); getch(); }
Output
Value of a: 200 Value of b: 100
Difference between call by value and call by reference.
call by value | call by reference |
---|---|
This method copy original value into function as a arguments. | This method copy address of arguments into function as a arguments. |
Changes made to the parameter inside the function have no effect on the argument. | Changes made to the parameter affect the argument. Because address is used to access the actual argument. |
Actual and formal arguments will be created in different memory location | Actual and formal arguments will be created in same memory location |
Note: By default, C uses call by value to pass arguments.
Important points related to Function
- The basic purpose of the function is code reuse.
- From any function we can invoke (call) any another functions.
- Always compilation will be take place from top to bottom.
- Always execution process will starts from main() and ends with main() only.
- In implementation when we are calling a function which is define later for avoiding the compilation error we need to for forward declaration that is prototype is required.
- In function definition first line is called function declaration or function header.
- Always function declaration should be match with function declaratory.
- In implementation whenever a function does not returns any values back to the calling place then specify the return type.
- Void means nothing that is no return value.
- In implementation whenever a function returns other than void then specify the return type as return value type that is on e type of return value it is returning same type of return statement should be mentioned.
- Default return type of any function is an int.
- Default parameter type of any function is void.
Recursive Function
When Function is call within same function is called Recursion. The function which call same function is called recursive function. In other word when a function call itself then that function is called Recursive function.
Recursive function are very useful to solve many mathematical problems like to calculate factorial of a number, generating Fibonacci series, etc.
Advantage of Recursion
- Function calling related information will be maintained by recursion.
- Stack evaluation will be take place by using recursion.
- In fix prefix, post-fix notation will be evaluated by using recursion.
Disadvantage of Recursion
- It is a very slow process due to stack overlapping.
- Recursive programs can create stack overflow.
- Recursive functions can create as loops.
Find the Factorial of any number using recursion
Example
#include<stdio.h> #include<conio.h> void main() { int fact(int); int i,f,num; clrscr(); printf("Enter any number: "); scanf("%d",&num); f=fact(num); printf("Factorial: %d",f); getch(); } int fact(int n) { if(a<0) return(-1); if(a==0) return(1); else { return(n*fact(n-1)); } }
Output
Enter any number: 5 Factorial: 120
Find the Table of any number using recursion
Example
#include<stdio.h> #include<conio.h> void main() { int table(int,int); int n,i; // local variable clrscr(); printf("Enter any num : "); scanf("%d",&n); for(i=1;i< =10;i++) { printf(" %d*%d= %d\n",n,i,table(n,i)); } getch(); } int table(n,i) { int t; if(i==1) { return(n); } else { t=(table(n,i-1)+n); return(t); //return(table(n,i-1)+n); } }
Output
Enter any number: 5 5*1= 5 5*2= 10 5*3= 15 5*4= 20 5*5= 25 5*6= 30 5*7= 35 5*8= 40 5*9= 45 5*10= 50
No comments:
Post a Comment