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, language 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 the 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 FUNCTION_NAME(PARAMETER1,PARAMETER2,...){
FUNCTION_BODY
}
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. These 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.
FUNCTION_NAME(PARAMETER1,PARAMETERS);
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 types like integer, float, string, etc. Generally return
keyword is used to return provided data or result to the caller.
return RESULT
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.
function FUNCTION_NAME(PARAMETER1,PARAMETER2,...){
FUNCTION_BODY;
}
As an example, we will create a function that will sum two given integers and returns the result to the caller.
function sumNumbers($number1,$number2){
$result=$number1+$number2;
return $result;
}
and we can call the sumNumbers()
function like below for different parameters.
$result=sumNumbers(2,3);
$result=sumNumbers(2,9);
$result=sumNumbers(8,3);
Create and Call Function In Python
Python syntax is different from C related language 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. The function body will be 1 tab indent. The result can be returned with the return
keyword.
def sumNumbers(number1,number2):
result=number1+number2
return result
We can call this function like below.

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.
function FUNCTION_NAME(PARAMETER1,PARAMETER2,...){
FUNCTION_BODY;
}
In this example, we will create the function named sumNumber()
with two parameters to sum.
function sumNumbers(number1,number2){
result = number1 + number2;
return result;
}
We can call this function like below.

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.
RETURN_TYPE FUNCTION_NAME (PARAMETER1, PARAMETER2,...){
FUNCTION_BODY;
}
int sumNumbers(int number1, int number2){
int result = number1 + number2;
return result;
}
We can call this function like below.
int result = sumNumbers(2,3);
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.
RETURN_TYPE FUNCTION_NAME (PARAMETER1, PARAMETER2,...){
FUNCTION_BODY;
}
int sumNumbers(int number1, int number2){
int result = number1 + number2;
return result;
}
And we can call it like below.
int result = sumNumbers(2,3);
Create and Call Function In Java
Java is C and C++ as a programming language. Also, C# is very similar to Java. We can use C# or C functions in a Java programming language like below.
RETURN_TYPE FUNCTION_NAME (PARAMETER1, PARAMETER2,...){
FUNCTION_BODY;
}
int sumNumbers(int number1, int number2){
int result = number1 + number2;
return result;
}
and we can call it like below.
int result = sumNumbers(2,3);
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.
FUNCTION_NAME(){
FUNCTİON_BODY
}
Or we can use function
keyword in order to create a function.
function FUNCTION_NAME{
FUNCTİON_BODY
}
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.
sumNumbers () {
number1=$1;
number2=$2;
result = $(( number1 + number2 ))
return $result
}
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 function
keyword before the function name and surround the function body with the curly braces {
, }
.
function sum-Numbers(){
return $args[0]+ $args[1]
}
and we can call like below.
PS> sum-Numbers 1 2
PS> sum-Numbers 10 20

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