Functions in C Programming Language

Notes:

Functions in C Programming Language:
- A function is a reusable block of code with name, meant to perform a specific task
- Whenever we want to perform a specific task, we create a function in C
Ex: add, subtract, square, sort, search, etc.

Note: To perform a specific task, first we must declare and define a function for it

Syntax for declaring a function is:
return_type functionName([datatype param1,datetype param2, …. , datetype paramN]);

Ex: void wishHi(); // prototype of wishHi function

Note:
- Declaration of a function is also called a prototype of a function.
- prototype of a function tells to the compiler what is the name of the function, what is the return type of the function, how many parameters a function accepts, and data type of each parameter.

Syntax for defining a function is:
- Defining a function means implementing a function, i.e. adding body to the function

return_type functionName ([datatype param1,datetype param2, …. , datetype paramN])
{
statement(s) to be executed;

[return returningValue;]
}

Ex: // definition of a wishHi function
void wishHi()
{
printf("Hi\n");
}

Note: a function gets executed only when it is called

Syntax for calling a function is:
functionName( [ param1, param2 …. paramN ] );
Ex:
wishHi(); // calling wishHi function

Note: if it is required; we can call and execute a function n number of times.

Why Functions?
Functions are created for:
- manageability (i.e. to divide a larger problem into smaller tasks or modules)
- reusability (i.e. once a function is defined, it can be used multiple times)
- abstraction (i.e. hiding the way how tasks are done) (creating libraries)
etc.

Example Code:

#include <stdio.h>

void wishHi();

int main()
{
wishHi();
wishHi();
wishHi();

return 0;
}

void wishHi()
{
printf("Hi\n");
}