What Is A Function and How To Create Function In PHP, Python, JavaScript, C/C++, C#, Bash, Java , PowerShell Programming Languages?

RSS
FACEBOOK
FACEBOOK
PINTEREST
PINTEREST
LINKEDIN
INSTAGRAM

A function is a basic element of the programming languages. The function is derived from mathematics where complex operations are summed and aggregated together for multiple use cases. In programming, languages function provides abstractions, simplicity, elegance for code which can be used multiple times with a simple call.

Syntax Of The Function

The function is provided with different programming languages with different syntax but most of them are very similar and provides similar features and attributes. In this part, we will provide the syntax of the generic function and its attributes, parameters etc.

  • FUNCTION_TYPE exist in some programming languages where it defines the type of the result of the function. For example, if we want to sum integers with a function the result should be an integer too.
  • FUNCTION_NAME defines the function identifier. This is used when calling functions. Functions are identified with the name of the function.
  • PARAMETER is used to provide data to the function. Functions may accept single or multiple parameters or do not accept any parameter.
  • {,} curly braces are used to surround a function body. The function body or code we will execute with a function will be put into the curly braces.
  • FUNCTION_BODY contains the function code which will execute when the function is called or used. This can also return a value for the caller.

Function Parameters

Functions are very useful with parameters. In most of the cases, a function will provide single or more parameters. The parameter may have some data type like integer, string, floating point, object, pointer, structure or complex types. Some parameter is optional were using given parameters is not a must and can be omitted while calling function. Functions can be defined multiple times with the same name where parameters are different in each definition. This is called function overloading.

Calling A Function

Calling a function can be some times tricky. When we call a function we are expecting something to happen. This can be different things like saving some file, read a file, run SQL query, execute some calculations, calling other functions etc. The function is called by providing the function name like below where we will provide two parameters.

LEARN MORE  How To Define, Use Functions in Python With Def?

Return Value

Functions can be created for different purposes where returning a value is very popular. Functions calculate some data and return to the caller. This return value can be different type like integer, float, string etc. Generally return keyword is used to return provided data or result to the caller.

Create and Call Function In PHP

PHP functions can be created with the function keyword before the function name. We will use curly braces in order to surround the function body. We will also use ; for each statement in the function body which is related to the PHP syntax. Here is the syntax of the PHP function.

As an example, we will create a function which will sum two given integers and returns the result to the caller.

and we can call the sumNumbers()function like below for different parameters.

Create and Call Function In Python

Python syntax is different from C related languages syntax. It is similar to Visual Basic programming language. We will use def keyword before the function name and add : to the function name line. The function body is nor surrounded by some characters where it is defined with spaces. Function body will be 1 tab indent. The result can be returned with the return keyword.

We can call this function like below.

Create and Call Function In Python

Create and Call Function In Python

Create and Call Function In JavaScript

JavaScript function definition syntax is very similar to the Python programming language syntax definition. There is two different where the biggest one is the function body is surrounded with curly braces and another one is each statement is ended with ;. Here is the generic function definition syntax of the JavaScript programming language.

In this example, we will create the function named sumNumber() with two parameters to sum.

We can call this function like below.

Create and Call Function In JavaScript

Create and Call Function In JavaScript

Create and Call Function In C/C++

C and C++ programming languages are very old where a lot of different programming languages like PHP, JavaScript has inherited its syntax. C and C++ programming languages are system level languages where a lot of details should be defined precisely. We need to define the function return value type and parameter value types during the function definition. Also, we need to provide proper values as a parameter and return value.

The syntax is like below.

 

We can call this function like below.

Create and Call Function In C#

C# is Java and C oriented programming language where the very same syntax is used. We will use a C or C++ programming language function in a C# code without a problem like below.

And we can call it like below.

Create and Call Function In Java

Java is C and C++ like programming language. Also, C# is very similar to Java. We can use C# or C functions in a Java programming language like below.

and we can call it like below.

Create and Call Function In Bash

Bash is a very popular Linux shell. Bash provides more than a shell-like programming language features. Bash can be used to create scripts and functions. We can create some function in order to use in scripts or Bash interactive shell. There are two types of syntax to create a Bash function.

Or we can use function keyword in order to create a function.

We will create a function named sumNumbers which accept two parameters. In Bash functions, parameters are read inside the function body with $1,$2,… in an incremental manner.

Create and Call Function In PowerShell

PowerShell is a shell used in Windows operating systems. It provides advanced programming language features. We can create a function like a Bash shell. We will use the functionkeyword before the function name and surround the function body with the curly braces {, }.

and we can call like below.

Create and Call Function In PowerShell

Create and Call Function In PowerShell

RSS
FACEBOOK
FACEBOOK
PINTEREST
PINTEREST
LINKEDIN
INSTAGRAM

1 Response

  1. Márton Tamás says:

    You can also declare return types in PHP as of version 7 (;
    See: https://www.php.net/manual/en/functions.returning-values.php#functions.returning-values.type-declaration

Leave a Reply

Your email address will not be published. Required fields are marked *

Enjoy this blog? Please spread the word :)