- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
A function is a group of reusable code which can be called anywhere in your program. This eliminates the need of writing the same code again and again. It helps programmers in writing modular codes. Functions allow a programmer to divide a big program into a number of small and manageable functions.
The basic syntax is shown here.
Try the following example. It defines a function called sayHello that takes no parameters −
Calling a Function
To invoke a function somewhere later in the script, you would simply need to write the name of that function as shown in the following code.
Till now, we have seen functions without parameters. But there is a facility to pass different parameters while calling a function. These passed parameters can be captured inside the function and any manipulation can be done over those parameters. A function can take multiple parameters separated by comma.
Try the following example. We have modified our sayHello function here. Now it takes two parameters.
The return Statement
For example, you can pass two numbers in a function and then you can expect the function to return their multiplication in your calling program.
Try the following example. It defines a function that takes two parameters and concatenates them before returning the resultant in the calling program.
Useful Video Courses
72 Lectures 4.5 hours
88 Lectures 14 hours