Functions in programming - Study24x7
Social learning Network

Welcome Back

Get a free Account today !

or

Forgot password?

By Registering, you agree to our Privacy Policy and Terms of use.

Functions in programming

study24x7
Web Camp Published on 20 April 2020

Hello Guys!!

Hope you are doing safe at your home.

So, Let's get started with Functions.


One of the most basic building blocks of any programming language are FUNCTIONS.

You can write simple programs without functions but to develop any application or program at a larger scale, you need functions.

Basically, Functions are like mini-programs that are written separately which divides a large module into chunks so that we can easily debug and maintain the application. So, Function is a reusable piece of code that performs a specific task.


Let's dive into the reasons why they are so useful and vital for programmers:


1. It lets you reuse the code. There are times when you write a specific code and you need it to use more than once in your application. Once you have written the function, it can be called multiple times within the program.This feature removes the duplicacy of code and reduces the amount of code resulting in a much more cleaner code.


2. It decreases complexity and easy to maintain. While developing any application, there are times when we need to change the logic multiple times. Copying and pasting the needed block of code multiple times is a bad practice and annoying as well. Functions remove this problem and makes it easier for you to maintain the code in a cleaner way.


3. Makes testing and debugging easier. This means that if any large program is divided into functions, it will be much more easier for a user to understand and debug the code. If the program is clearly divided into functions, Instead of checking the entire program, programmers can point to one function. Ensuring that the particular functions works, there is no need to test it again and again, and can move to next part in an easy manner.


Types of functions:


There are two type of functions in any programming language.

1. Built-In:-

Functions which are already provided by the programming language. Some examples are:

PHP: phpinfo(), date(), strlen(), error_reporting(), array_unique(), round(), getcwd(), mkdir() etc.

Javascript: toString(), parseInt(), length(), indexof(), substr(), join(), Date(), round(), sqrt() etc.


2. User-defined:-

Functions that are developed by user or developers for their programs/applications. To perform custom validations, authenticating users, these functions comes in handy. While creating functions, there are some rules that are mandatory:

  1. Function is created with the keyword 'function'.
  2. Function name must be unique and should not contain spaces.
  3. Function name must start with a letter or an underscore. Function names are NOT case-sensitive(If you defined function name in lowercase, but calling them in uppercase it will work).
  4. It must return or echo out some value.


Syntax:

function functionName(){

// code

}

Example #1:

function firstFunction(){

echo 'This is my first function';

}

firstFunction();


Example #2:

function calculateMax($x,$y) {

return ( $x > $y )? $x : $y; // ternary operators

}

$max = calculateMax(40,50);

echo "The maximum number is $max\n"; // '\n' for new line


Explanation: $x and $y are called arguments that are sent to the function.The function will process these arguments depending on the login and will return some value.


I will be covering more details in the next article.

Till then Stay tuned...Follow WebCamp for more such stuff

#StayHome #StaySafe #KeepCoding


study24x7
Write a comment...