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

Program

Output

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.

Output

Function  with  argument  &  return value

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

Output

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.

Output

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
1
attr()Can be used to retrieve the value of any attribute for a particular element. You can give the attribute name as the argument.
2
calc()The values for CSS properties can be calculated using this function. Comes very handy while calculating the dimensional properties like height, width etc.
3
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
4
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
5
hsla()Same as hsl() but here we also have the alpha mode to set.
6
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

7
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
8repeating-linear-gradient()
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.
9repeating-radial-gradient()
Similar to repeatin-linear-gradient(), except it defines repeating radial gradients. Can also use color stops.

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

PHP Functions

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.

Define a Function

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

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()

Example

Output

Explanation

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

Function Arguments or Parameters

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

Function with Arguments or Parameters

Output

Explanation

$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.

Example

Function with multiple Arguments or Parameters

Output

Call by value

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

Function Call By Value:

Output

Explanation
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:

Output

Explanation
$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

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

Function Call By Reference:

Output

Explanation

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:

Output

Explanation
$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.

Recursive Function

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:

Output

Explanation
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.