C - Language Theory
Functions
1. What is C Function?
Functions are building blocks of C. Function performs the same set of instructions on different sets of data or at different portions of a program.
2. Uses of C Functions?
- C functions are used to avoid rewriting same logic/code again and again in a program.
- There is no limit in calling C functions to make use of same functionality wherever required.
- We can call functions any number of times in a program and from any place in a program.
- A large C program can easily be tracked when it is divided into functions.
- The core concept of C functions are, re-usability, dividing a big task into small pieces to achieve the functionality and to improve understandability of very large C programs.
A function is a block of statements that performs a particular task. A function can call itself is known as 'Recursion'.
Types:
(ii) Call by value
Classification
In call by value method, the value of the variable is passed to the function as parameter. The value of the actual parameter can not be modified by formal parameter. Different Memory is allocated for both actual and formal parameters. Because, value of actual parameter is copied to formal parameter.
Call by Reference
This method is used to copies the address of an argument into the formal parameter.
Syntax |
---|
1) function_name(arguments list); Ex: k(); #include<stdio.h> k(); main() { printf("Sunday"); k(); } k(); { int i; for(i=1; i<10; i++) { printf ("%d",i); } } |
Call by Value
This method is used to copies the actual value of an argument into the formal parameter of the function.
Syntax |
---|
Return_type function_name(argument list); Ex:int add(int x, int y) |
Advantages:
- By using functions, we can avoid rewriting same logic or code again and againg in the program.
- We can call functions at any number of times and from any place in a program.
- To improve the reusability.
- To reduce the size of the code.