Function
Function
Functions are set of Instructions.Functions are also known as block of code.
Functions are used to reduce the repetition of the code.
In a Single Program we can define more than one Function.
Functions can be used as many time the Program needs to execute the task.
Advantages
- Used to reduce the Line of Code.
- It increases the Program readability.
- It reduce the chance of error of the Program.
- Functions are used to divide a large Problem into many parts to reduce the complexity.
Types of Functions
- Predefined Functions
- User Define Functions
Predefined Functions:
Predefined functions are also Library functions which are stored in the the Header file.Predefined functions can only be called.
User Define Function
User define functions can be defined as the user wants to define the Functions.It can be of Two Types:
- Non-Parameterized
- Parameterized
Syntax of Non-Parameterized Function
Function Definition:Function can be define either before the main() or after the main() functions.
Function Calling:
Function can be called in the main() function.
functionName();
Syntax of Parameterized Function
Function Definition:Function can be define either before the main() or after the main() functions.
return type functionName(Parameters seprated by Comma)
{
Statements;
}
Function Calling:
Function can be called in the main() function.
functionName(Parameters);
Program
Write a program to print the Message 10 times using function.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgweqelxaq9MnKjG753s2C4cBveZTq36pz_3-r_qUqz0qgZxmeyR1AExe2bbyz05cSpOQJjQi1C0lWAh2HhPoo5rRzN4dzQRu817i6PD6Cv4vvJYaCohfRdVDikHWwTeSfwLf9OG88qAWE/w400-h324/non+para+pro.png)
Parameterized Function
Write a Program to Print the Square of a number using function.
2. Write a Program to print Square of numbers between two numbers.
Function With return type
Write a program to print the factorial of a number.
Call By Value
- In this the copy of value is passed to function.
- Actual and formal Arguments created in different memory locations so that the changes inside a functions does not reflect another function.
Call By Reference
- In the the reference of the value is passed at calling time to the called function.
- Actual and formal Arguments created in same memory locations so that the changes inside a functions reflect another function.
1. Call By reference Basic Example
2. If we Change the value inside the function definition It will reflect the values of calling function.
Here is an Example of It:
Comments
Post a Comment
If you have any doubts, Please let me Know