May it be 'C' - the procedure oriented language or 'Java' - the object oriented language, functions play a vital role. A function in simple terms is a block of statements written to accomplish a specific task. The task can be in layman's language making a daily schedule or a cup of tea etc; and in technical terms a set of instructions which generates a particular output example the very simple add function which is meant only for performing addition.
For instance, a function for a non-coder would be the sequence in which he/she performs some task say, a sample sequence to make daily schedule:
Step 1: Make a todo list
Step 2: Prioritize the tasks
Step 3: Take up the tasks and complete in given time.
Step 4: End
Whereas, a function in programming language is the set of instructions written within a block and for a single purpose, example addition of two numbers:
Step 1: Take two variables to store numbers
Step 2: Take a variable to store the result
Step 3: Apply addition logic
Step 4: Display the result
Step 5: End
The general syntax to create functions:
The following image gives idea about the two categories of functions i.e. the predefined or provided by the system and the user-defined which are developed/written by the coder depending upon the need.
The predefined set of functions in 'C' includes functions such as printf(), scanf(), gets(), getch() etc; and that of in 'Java' includes println(), nextInt(), toString() etc.
Now, depending upon the user-defined functions, the functions are further classified into following types:
The following are the benefits of using functions in our code:
1. Written for a particular task.
2. They are one of the reusable components in programming.
3. They provides better readability of the code to the readers.
4. In case of errors, they are easy to debug and solve the issue to generate error-free code.
5. Since, once written they can be called n-number of times in program and hence the reduce the size of code.
6. Functions are easy to understand and further to use.
Comments
Post a Comment