C Function

In C, function is a set of statements or code block with a name, return type, parameters and function body used to perform specific task and only executes when it is called.

Using functions in a c program has following advantages

  1. No code duplicacy or reptation. means same function can be called wherever necessary in c program.
  2. Makes program modular. Functions are also called procedure.
  3. Increase readability.
  4. Easy to maintain code or debug.
  5. Changes or modification become easier.

Function Parts with Syntax

  1. Function Declaration
  2. Function Definition
  3. Function calling

Function Declaration

  1. It is providing information to c compiler, that a given function is created in program with specific name, return type and parameters.
  2. Function declaration has no function body and ends with semicolon. It must be declared first, so, compiler knows some function is declared and may defined or use later in program.
  3. Parameters are optional, and specified with only data type and separated by comma, variables are not used.

Syntax

<return type> <function name>(parameter1, parameter2);

Example

int addTwoNumbers(int, int);

Explanation

  1. return type is int.
  2. function name is addTwoNumbers.
  3. Parameter list is int, int.

Function Definition

  1. It should have same name, return type and parameters as function declaration.
  2. Parameters must be defined with data type and variable.
  3. It has function body which contains statements to perform task.

Syntax

<return type> <function name>(parameter1, parameter2)
{
	// set of statements
}

Example

int addTwoNumbers(int a, int b){
   int c = 0;
   c =  a + b;
   printf("%d", c);
}

Function calling

To use a function in a c program, function needs to be called. Function call executes function, While calling a function return type is not used. Parameters value should be of given data type in function declaration.

Syntax

<function name>(parameter1, parameter2);

Example

addTwoNumbers(10, 12);

Return type

Function can return or also not return any value depends on its return type, On basis of return type function can return following type of values i.e. int, float, double, char, long, void.

Function Name

Name by which it is identified in the program. Function name should be unique and a valid c name, used to declare, define and call a function.

Parameter list

Parameters in function are variables which is used to accept input. Parameters can be of any data type separated by comma.

Statement set

These statements are inside function body, executes when a function is called and gives output or return a value.

Complete Example

#include<stdio.h>

//Function Declaration
int addTwoNumbers(int, int);

void main()
{
   addTwoNumbers(20, 50); //Function call
}
// //Function Definition
int addTwoNumbers(int a, int b)
{
   int c = 0;
   c =  a + b;
   printf("%d", c);
}

Function with return type and Arguments

On basis of return type and Arguments function are of four types

  1. Function with no arguments and no return value.
  2. Function with no arguments and with return value.
  3. Function with argument and with no return value.
  4. Function with arguments and with return value.

Function Parameters

Parameters in function can be passed in two different ways.

  1. Pass by value
  2. Pass by reference

Pass by value

The value of Variable is passed as copy during function call to the function parameters.
Changing value of function parameters does not change the original variables value of function call.

#include<stdio.h>

int swapvalues(int, int);

void main()
{
   int a1 = 20;
   int a2 = 50;
   printf("Before swap value of a1 is %d ", a1);
   printf("\nBefore swap value of a2 is %d ", a2);
   swapvalues(a1, a2); // Original values
   printf("\nAfter swap value of a1 is %d ", a1);
   printf("\nAfter swap value of a2 is %d ", a2);

}
int swapvalues(int a1, int a2)
{
 // Values of variable changed during swap
   int c = 0;
   c = a1;
   a1 = a2;
   a2 = c;
}
Output:
Before swap value of a1 is 20
Before swap value of a2 is 50

After swap value of a1 is 20
After swap value of a2 is 50

Pass by reference

Address of variables are passed during function call to the function parameters.
Changing value of function parameters changes the original variables values because of sharing same variable address.

int swapvalues(int*, int*);

void main()
{
   int a1 = 20;
   int a2 = 50;
   printf("Before swap value of a1 is %d ", a1);
   printf("\nBefore swap value of a2 is %d ", a2);
   swapvalues(&a1, &a2);
   printf("\nAfter swap value of a1 is %d ", a1);
   printf("\nAfter swap value of a2 is %d ", a2);

}
int swapvalues(int* a1, int *a2)
{
   int c = 0;
   c = *a1;
   *a1 = *a2;
   *a2 = c;
}
Output:
Before swap value of a1 is 20
Before swap value of a2 is 50

After swap value of a1 is 50
After swap value of a2 is 20