Functions in C:
What is a Function?
The function is a block of code to perform a specific task In C. we divide a large program into basic building blocks known as a function. The block contains certain statements of code enclosed by the { }. When a program calls a function, the code inside the function executes and it may return a value back to the calling function.
Advantages of a Function:
1. By using functions, we can track large programs when it is divided into multiple functions.
2. We can avoid rewriting the code again and again in a program.
3. We can call C functions any number of times from anywhere in the program.
4. We can easily modify the errors that occurred.
5. Code optimization takes place while using the functions.
Function Aspects:
The Aspects of the function are:
1. Function Declaration: A Function must be declared globally in a C program to tell the compiler about the function name, function parameters(formal parameters), and return value.
2. Function Call: The function can be called anywhere from the function. The function parameters list must not differ in function calling and a function declaration. We must pass the same number of parameters as it is declared in the function declaration.
3. Function definition: It contains the actual statements which are to be executed.
Types of Functions:
There are two types of functions in C:
1. Inbuild/Library Functions
2. User-defined Functions
1. Library Functions: These functions are declared in the C header files such as print(), scanf(), strlen(), pow(), strcpy() etc.
"In this tutorial, we are completely focusing on user-defined functions."
2. user-defined Functions: These functions are created by the Programmer so that these functions are used multiple times. Reusing the code decreases the complexity and increases code optimization.
Function Declaration without return value: In this declaration, we use void as a return type so that the function cannot return anything.
Syntax:
return_type fun_name(parameters_list)
{
}
Example:
Syntax:
return_type function_name(parameters list)
{
// code to execute
return value;
}
Example:
Output:Function Call: A Function can be called from anywhere and any number of times in a program. The Function call differs in two ways. One is a calling a function and another one is called a function.
While calling a Function there are two methods to pass data into the function in C language.
1. Call By Value
2. Call By Reference
Call By Value:
- The actual parameter is the argument that is used in the function call whereas the formal parameter is the argument that is used in the function definition.
- Different Memory locations are allocated for actual and formal parameters.
- In call by value method, the value of actual parameters is copied into the formal parameters.
- In call by value method, we can not modify the value of the actual parameter by the formal parameter.
- In call By Reference, the memory location is similar for both actual and formal parameters.
- All the operations in the function are performed on the value stored at the address of actual parameters. The modified value get stored at the same address.
- In Call By Reference, the address of actual parameters is passed into the formal parameters.
- The value of actual parameters can be modified by changing the formal parameters since the address of actual parameters is passed.
Explanation: When the function sample is called, the address of x is passed as an argument to the called function. To store the address passed by the calling function, you need to declare the formal parameter as a pointer(*y). According to call by reference, the value of the actual parameter(x) gets modified by changing the formal parameter(y).
No comments:
Post a Comment