Functions in C Programming

A function is a block of code or a group of instructions that as a unit performs a specific task. So functions are programs, and they can be either library functions or programmer written functions.

In the programs that we wrote in the previous sections, we have used functions like printf() and scanf(). These functions are the library function. In this section we will be looking into how to build our own functions – this functions will be called the programmer written functions.

While learning about functions, it is important to understand why it is important to use functions. We use functions because it has the following advantages:

  1. Complex and lengthy programs can be broken into smaller modules of the manageable size.
  2. Ensures reusability of code (a set of frequently used instructions need not have to write time and again but just called!).
  3. Easier to locate faulty instructions in the program.

To use functions in a program, function definition, function call, and function declaration are the three elements required. Functions are identified by their names and their naming rules must adhere to those of identifier.

Function Definition

It is a block of codes that are written to implement the requirements of the function which has in general two segments: function header and function body.

Generally, function definition have the form:

[php]

datatype functionName(arguments){

//block of code;

return;

}

[/php]

Function Header

Function header includes function type, function name and the arguments (also called parameters). Function type specifies the type of the value it is meant to return to the program where it is being called. If the function does not return any value then its type must be specified as void. In case the type of the function is not being mentioned in the header, the compiler assumes the function to be an int type.

The function name should be meaningful, readable, easy to remember and valid C identifier (should adhere to identifier naming conventions as mentioned earlier).

The parameters are the list of arguments that the function would take. These are the input that the function takes so that it performs its tasks. Each parameter is separated by a comma “,” and data type for each parameter should be mentioned. For an instance:

void printHello(int a, float b){//function header

//function body goes here

}

Function Body

The function body may include local variable declaration, function instructions and return statement if the function is to return something.

Variables declared in a function has a local scope (seen only by the function itself).

Function instructions would include one or more lines of instructions that are required to achieve the function’s purpose.

If the function is of void type return statement can be omitted. Thus, a function may or may not return any values to the calling function. If a function returns a value, it would return at most one value per call.

Function Call

Functions can be called anywhere in the program simply by using its name. If the function requires some parameters as input, their values should be mentioned. A function won’t perform its purpose if it is not fed with its required input parameters. If a function returns a value it can be used as variable and its type is defined by the function type.

For example,

int a, b, c;

c = findAverage(a, b);//function call

Function Declaration

It is also known as a function prototype. Before invoking the function, it should be declared like we declare variables before using it. It is often placed above all the functions including main. It has the form:

type function_name(arguments);

Example 9: Write a program that uses a function to calculate the factorial of a number given as input by the user.

Sample Code:

[php]

#include<stdio.h>

//factorial.c by bhutanio.com

void fac( int );

int main(){

int a;

printf(“Enter the no. to find its factorial:\n”);

scanf(“%d”,&a);

fac(a);

}

void fac(int d){

int i;

int f=1;

for(i=1;i<=d;i++){

f=f*i;

}

printf(“the factorial of %d = %d”,d, f);

}

[/php]

Example 10: Write C program that takes the input of two integers. Using user-defined functions find the sum of the numbers, the difference, the product and the quotient of the two input values.

Sample Code:

#include <stdio.h>;

/*program to implement functions by bhutanio.com*/

void add(int a, int b);

void sub(int a, int b);

void pro(int a, int b);

void div(int a, int b);

int main()

{

int a, b;

printf("\n Enter two numbers ");

scanf("%d %d", &a, &b);

printf("\n Sum is ");

add(a, b);

printf("\n Difference is ");

sub(a, b);

printf("\n Product is ");

pro(a, b);

printf("\n Quotient is ");

div(a, b);

}

void add(int a, int b)

{

int c=a+b;

printf("%d", c);

}

void sub(int a, int b)

{

int c= a-b;

printf("%d", c);

}

void pro(int a, int b)

{

int c= a*b;

printf("%d", c);

}

void div(int a, int b){

float c= (float)a/b;

printf("%f", c);

}

From the above two examples, let us conclude some characteristics of functions.

  1. Functions can be called as many times as it is required.
  2. A function may call another function.
  3. Each function has its own specific task.
  4. Functions are black boxes (those who uses the functions can’t see the instructions in it).

Pass by Value

Values to the functions can be passed in two ways, i.e. pass by value and pass by reference. Here let us look into how to pass the values directly into the functions.

Example 11: Implement call by value


#include <stdio.h>;

/*call by value program by bhutanio.com*/
int adder(int, int);//function prototype

main(){

printf("\nThis is program to add two numbers");

int x, y;

printf("\nEnter the first number \n");

scanf("%d", &x);

printf("\nEnter the second number \n");

scanf("%d", &y);

adder(x, y);//function call

}

//function definition

int adder(int a, int b){

int sum = a+b;

printf("\nSum of %d and %d is: %d", a, b, sum);

}

In the above example, say the memory spaces for the variables a, b, x, and y are allocated as:

XYab

During the execution, the value of x and y are being copied to the variables a and b, so during the computation, the values of x and y are not changed. This is because the variables a and b have their own memory locations. Such copying of values is called pass by values.

Pass by Reference

In pass by value, we pass the actual values into the functions. In another way, values are not directly passed but its addresses/references are passed. Often to get the memory addresses of the variables we use pointers. To understand the concept of pointers refers the section on Pointers.

Let us look into another example:

Example 12: Implement call by reference

#include <stdio.h>

/*pass by reference program by bhutanio.com*/

void adder(int *, int *); //function adder() prototype

main(){

int x = 50;

int y = 100;

//pass by reference

adder(&x, &y);//function call

}

//function definition

void adder(int *p, int *q){//takes two addresses

printf("\n%d %d", &p, &q); //print the address of two numbers

int sum = *p + *q;

printf("\n%d", sum);

}

Here, say the memory spaces are allocated as:

X = 50

Address: 50000

Y= 100

Address: 50004

P = ?Q=?

During the execution, the function adder() does not take the actual values of x and y but their addresses. The function variables p and q then get the values referencing the addresses. This is called the call by reference.

Recursions

While learning about functions it is important to understand the concept of recursions too. By definition, recursion is processed by which a function calls itself repeatedly until some specified condition(s) has been fulfilled.

To find the factorial of a number n,

Mathematically, factorial of n = n(n-1)(n-2)(n-3)…(n – (n-1))

For example, 4! = 4.3.2.1

= 24.

This is implemented in Example 11.

Example 13: Write a program that asks the user to enter the number to find the factorial and print it.

Sample Code:

#include <stdio.h>

//factorial.c by bhutanio.com

long int factorial(int n);

main(){

int n;

long int m;

printf("\nEnter the number to find factorial \n");

scanf("%d", &n);

m = factorial(n);

printf("\nFactorial of %d is %d", n, m);

}

long int factorial(int n){

if(n<=1)

return (1);

else

return(n*factorial(n-1));

}

In this section, we have looked into the deep aspects of functions. If you have no checked my previous chapters on C programming, you may like to check them:

Chapter 3: C Programming Basics

Chapter 4: Control Statements

1 thought on “Functions in C Programming”

Leave a Comment

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

Scroll to Top