Functions – C Programming

A function is a group of statements that together performs a task. Every C program has at least one function, which is main(), and all the most trivial programs can define additional functions.

You can divide up your code into separate functions for better understanding and readability. Dividing up your code among different functions is up to you, but logically the division usually is to each function will perform a specific task.
A function declaration tells the compiler about a function’s name, return type, and para meters. A function definition provides the actual body of the function.
The C standard library provides numerous built-in functions that your program can call. Few example for built-in functions are strcat(), memcpy(), function strcat() is to concatenated two strings, function memcpy() is to copy one memory location to another location and many more functions.

syntax:
return_type function_name( arguments )
{
body of the function
}

Parts of a function:

Return Type:

A function may return a value or may not. The return_type is the data type of the value that function returns. Some functions perform the desired operations without returning a value. In this case, the return_type should be void.

Function Name:

This is the actual name of the function. The function name and the parameter list together constitute the function signature.

Arguments:
An Argument is like a placeholder. When a function is invoked, you pass a value as the Argument. This value is referred as actual parameter or argument. The Argument list refers to the type, order, and number of the parameters a function has.
Arguments are optional; that is, a function may contain parameters or not.

Function Body:

The function body contains a collection of statements that describes what the function does.
Example:
Hope you understand , now lets write a simple function for sum of two numbers. This function has two numbers as arguments ,and result should be returned,

int sumOfTwoNumbers(int num1,int num2)
{
int sum = num1+num2;
return sum;                  // return value.
}

Function Declaration:

A function declaration tells the compiler about a function name and how to invoke the function name.

syntax:

return_type function_name( arguments );

example:
int sumOfTwoNumbers(int num1,int num2);

Function calling:

We know what is function, and how to declare it , now we need to know , how can we use this function to achieve our desired task.

To invoke a function, you will have to call the function to perform the defined task . When a program calls a function, program control is transferred to the called function. A called function performs a defined task un till return statement is executed or its function-ending closing brace is reached, and it returns the program control back to the main program.

To call a function, you simply need to pass the required parameters along with function name, and if function returns a value, then you have to store the returned value.

example :

To invoke our sum of two number function

int sum = sumOfTwoNumbers(10,20);

So, result 30 is stored in sum as returned value.

Program:

void main()

{

int num1 = 10,num2=20;

int sum = sumOfTwoNumbers(num1,num2);

printf("sum = %d",sum);

}

int sumOfTwoNumbers(int num1,int num2)
{
int sum = num1+num2;
return sum;                  // return value.
}

Function arguments:

Arguments  can behave like other local variables inside the function and are created upon entry into the function and destroyed upon exit.

While calling a function, there are two ways to pass the arguments to a function:

  1. Call by value
  2. Call by reference

Call by value:


This method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.
Program:
void swap(int x, int y)
{
int z;
z = x;
x = y;
y = z;
printf("Swapped values are a = %d and b = %d", x, y);
}

void main()
{
int a = 1, b = 2;
printf("Original values are a = %d and b = %d", a, b);
swap(a, b);
printf("The values after swap are a = %d and b = %d", a, b);
}

Output:
Original Values are a = 1 and b = 2
Swapped values are a = 2 and b = 1
The values after swap are a = 1 and b = 2

Call by Reference:

This method copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the argument.
program:
void swap(int *x, int *y)
{
int z;
z = *x;
*x = *y;
*y = z;
printf("Swapped values are a = %d and b = %d", *x, *y);
}
void main()
{
int a = 1, b = 2;
printf("Original values are a = %d and b = %d", a, b);
swap(&a, &b);
printf("The values after swap are a = %d and b = %d", a, b);
}

Output:
Original Values are a = 1 and b = 2
Swapped values are a = 2 and b = 1
The values after swap are a = 2 and b = 1

Leave a Reply

Your email address will not be published. Required fields are marked *