Functions in C Programming

In a C programming language a function is a group of statements that together perform a special task. In every C program has at least one function, which is main().

In a C library has a numerous predefined or built-in functions. For example, strcat() to concatenate two strings, main() from where program execution starts. 

In a C programming function has a 3 part:

  • Function declaration
  • Function definition
  • Function call

Function declaration : function declaration tells the compiler about a function’s name, return type, and parameters.

A syntax of function declaration –

return_type   function_name( parameter list );

Example of  the function declaration is as follows −

int max(int num1, int num2);

here int is a return type of function which tells function will return integer value and max is a name of the function.

“ int num1, int num2” is a parameter list which tells max function will receive two integer arguments.

Arguments are optional which depends on program requirements.

Function definition : function definition is an actual body of the function.

Syntax of function definition:

function definition consist a return type, function name, parameter list and body of the function.

Function call

whenever  program calls a function, the flow of control is transferred to the called function definition.

A called function performs a defined task and when flow of control reached to function-ending closing brace, flow of control returns to back to the main program.

Function can be categories as:

  • Function with no argument, no return value
  • Function with  argument but  no return value
  • Function with  argument and  return value
  • Function with no argument, but  return value

Function  with  no  argument  & no return value

Example: Write a program to find the area of circle using function with no argument, no return value.

The formula for the area of the circle is :

Area_circle = Π * r * r



Function  with  argument  but  no return value

Example: Write a program to find the area of circle using function with argument but no return value.


Function  with  argument  &  return value

Example: Write a program to find the area of circle using function with argument but return value.


Function with no argument but  return value

Example: Write a program to find the area of circle using function with no argument but return value.


CSS Functions

CSS has a list of inbuilt functions that can be used as the value for different CSS properties. Here’s the list of the functions with a small description for each one of them.

Sr NoFunctionsDescription
attr()Can be used to retrieve the value of any attribute for a particular element. You can give the attribute name as the argument.
calc()The values for CSS properties can be calculated using this function. Comes very handy while calculating the dimensional properties like height, width etc.
cubic-bezier()It is used to define a curve called the cubic-bezier curve. It takes 4 points as arguments and can be used define transition and animation speed curves
hsl()Another way of defining colors. H-S-L stands for Hue Saturation and Lightness. We can give the values for each as arguments to the hsl() function to get a desired color
hsla()Same as hsl() but here we also have the alpha mode to set.
linear-gradient()Can be used to define a linear gradient of colors. At least two colors need to be passed as arguments. The gradient, top to bottom, goes from first color argument to last color argument

radial-gradient()Defines the radial-gradient of colors. Here also, at least two colors are required. The gradient, center to edge, goes from first color argument to last color argument
We can repeat the linear gradient using this function. Apart from the color arguments we can also need to give color stop values in percentage or length. The color stop values decide the the smooth transitions of the color upto a certain point.
Similar to repeatin-linear-gradient(), except it defines repeating radial gradients. Can also use color stops.

rgb()The RGB mode for defining the colors
rgba()Along with the Red-Green-Blue of RGB, this also has the Alpha mode to define
var()For custom properties, the value can be inserted using this function

Functions in PHP

A function is a block of code that can be used many times in a program.

It helps us to save a lot of codes and time because function defines only one time and we can use it multiple time whenever & wherever we want.

The most important reason for using function is ‘Reusability’.

Consider an example:
In your program you need a square value of any number more than one time, then what will you do?

You re-type the logic of square value of different number every time.

But using the function you need to declare logic of square of any number only one time and use it anywhere any time without re-typing same code for your program.

PHP User Defined Functions

There are thousands of built-in functions in PHP.

We can also create our own functions in PHP.

PHP function separates the programming logic & easy to understand the flow of code.

How to Define a Function in PHP

A function is a block of statement defined by the developer that can be used repeatedly in a program.

During execution of a program, a function does not execute automatically, the function can be executed upon function call.

The syntax of a function in PHP is as below

NoteFunction name always starts with letter or _ (underscore), never start with integer or any other special characters.

If the function name is too long separate two words by using _ but don’t use space between two words.

PHP is a case-sensitive language so function names are also case sensitive.

Most of the developer follows camel case rules to define function name. Like functionName() not functionname()




By typing ‘showMsg()’code you can easily display ‘Hello World’ message anywhere in your program without typing ‘Hello World’ repeatedly.

Passing Function Arguments or Parameters in PHP

The function can take user input as an argument or parameter and return value. The function always returns type.

Function with Arguments or Parameters



$num is an argument or parameter which received the number value like 43, 65, 112, or 225.

We can use more than one arguments or parameters by using comma separator.

Using squareOfAnynumbers () you can easily calculate square value of any number in your program.


Function with multiple Arguments or Parameters


Call by value in PHP

PHP allows you to call the function by value. Let’s understand the concept of “call by value” with examples.

Function Call By Value:


Call by value means argument ’s value directly passed to the function and the value only modified into the function, not outside of the function.

So, $a only changed into doSomeThing() while passed inside the function, that’s why doSomeThing($a) returns 4 but $a is still 3.

Swap two numbers using Call By Value:


$a and $b changed within function so when we call the function swapTwoNumbers($a,$b).

It returns 8 6 means $a = 8 and $b = 6
But outside of the function $a still 6 and $b still 8.

Call By Reference in PHP function

PHP allows function call by reference also. Let’s start to understand with the simple example.

Function Call By Reference:



Call by reference means value and address (memory location) both passed to function and value will be modified within the function and also outside of the function.

Here we used & (ampersand) as the reference of argument’s address in the function definition.

So any modification of variable in the function also reflects outside of the function in the original variable.

So, $a return 4 and doSomeThing($a) also return 4.

Swap two numbers using Call By Reference:


$a and $b changed within function so when we call the function swapTwoNumbers($a,$b) it returns 8 6
means $a = 8 and $b = 6
and outside of the function $a 8 and $b 6.

Recursion in PHP

In programming, language Recursion occurs when function continues to call itself using some sort of condition.

If there is no condition defined, function calls occur infinite times. These sort of conditions are called the base case.

If a base case is reached function returns the value and stop the recursion.

Until the base case requirement fulfills function automatically calls itself to continue the recursion.

The syntax of the recursive function:

Factorial using the recursive function:


Frist base case is $num is less than 0 means $num value is -1 or any negative number then recursion will be stopped and return -1.

The second base case is $num is equal to 0. If $num value is 0 then recursion will be stopped and return 1.

Otherwise fact() automatically called by itself and return the factorial of any positive number.

Here we passed 6 as input, so the base case has not reached then recursion continued for 5th step and returned 720.