Home » C Programming » Functions in C Programming

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

Managed by MochaHost - Fast & Easy WordPress Hosting