Understanding Functions in C Programming

 

A function is a module or block of program code which deals with a particular task. Making functions is a way of isolating one block of code from other independent blocks of code. Functions serve two purposes. They allow a programmer to say: this piece of code does a specific job which stands by itself and should not be mixed up with anything else’, and they make a block of code reusable since a function can be reused in many different contexts without repeating parts of the program text.

A program written with numerous functions is easier to maintain, update and debug than one very long program.  By programming in a modular (functional) fashion, several programmers can work independently on separate functions which can be assembled at a later date to create the entire project. Each function has its own name.

Functions that a programmer writes will generally require a prototype. Just like a blueprint, the prototype gives basic structural information: it tells the compiler what the function will return, what the function will be called, as well as what arguments the function can be passed.

The function which sends the data to the function is called as the Calling Function and the function which is called by the calling function is called as the Called Function. This is described as given below.

What is Calling Function and Called Functions in C Programming?

What is Calling Function and Called Functions in C Programming?

 

Creating User-Defined Functions

Declaring the function

The declaration, called the FUNCTION PROTOTYPE, informs the compiler about the functions to be used in a program, the argument they take and the type of value they return.

How is Function defined in C programming?

How is Function defined in C programming?

Consider the following example to understand the function definition in detail.

int sum(int x, int y)   //Function header

{

int ans = 0;   //holds the answer that will be returned

ans = x+y;   //calculate the sum

return ans   //return the answer

}

In the first line of the above code function header int sum (int x, inty) It has four main parts

  1. The name of the function i.e. sum
  2. The parameters int x, int y, of the function enclosed in parenthesis
  3. Return value type i.e. int
  4. Function Body

Whatever is written with in { } in the above example is the body of the function

Defining the function

The function definition tells the compiler what task the function will be performing. The function prototype and the function definition must be same on the return type, the name, and the parameters.  The only difference between the function prototype and the function header is a semicolon.
The function definition consists of the function header and its body.  The header is EXACTLY like the function prototype, EXCEPT that it contains NO terminating semicolon.

//Prototyping, defining and calling a function

#include <stdio.h>
void starline();         // prototype the function
int main()
{
starline( );      // function call
printf( "\t\t The Computer Students dot Com\n");
starline( );    // function call
return 0;
}

// function definition void starline()
{
int count;         // declaring a LOCAL variable
for(count = 1; count <=65; count++)
printf( "*\n");
}

Function Invocation

The function is called (or invoked) from main (). To invoke a function in main, the function name is written, followed by parentheses. The syntax of the function call is very similar to that of declaration, except that the return data type is not used. A semicolon is placed at the end of the call expression. The following facts occur when function is invoked.

  • When a function is invoked, control is transferred to the first the statement in the functions body. Computer immediately begins executing statements from the beginning of the called function. Each time the function is called; execution always starts at the beginning of the function.
  • Execution continues inside the called function until either:
    • It reaches the right } at the end of the function
    • Or a return statement.
  • Either way, the function stops at this point and execution picks up right where it had left off in the original function (e.g., back in main)


Argument to a Function

Sometimes the calling function supplies some values to the called function. These are known as parameters. The variables which supply the values to a calling function called actual parameters. The variable which receive the value from called statement are termed formal parameters.

Consider the following example that evaluates the area of a circle.

#include<stdio.h>
void area(float);
int main()
{
float radius;
printf(“Enter Radius”);

scanf(“%f”,&radius);
area(radius);
return 0;
}
void area(float r)
{

float a;

a=3.14*r*r;
printf(“the area of the circle is %0.2f”,a);
}

Here radius is called actual parameter and r is called formal parameter.

Return Type of a Function

// Example program

#include <stdio.h.h>

int timesTwo(int num);   // function prototype
int main()
{
int number, response;
printf("Please enter a number: ");
scanf(“%d”,&number);
response = timesTwo(number);  //function call
printf("The answer is %d",response);
return 0;
}
//timesTwo function
int timesTwo (int num)
{
int answer;   //local variable
answer = 2 * num;
return (answer);
}

Calling of a Function

The function can be called using either of the following methods:
i) Call by value
ii) Call by reference

Call by Value

In call by value method, the called function creates its own copies of original values sent to it. Any changes, that are made, occur on the function’s copy of values and are not reflected back to the calling function.

Call by Reference

In call be reference method, the called function accesses and works with the original values using their references. Any changes, that occur, take place on the original values are reflected back to the calling code.

Consider the following program which will swap the value of two variables.

using call by reference using call by value

#include<stdio.h>
void swap(int &, int &);
int main()
{
int a=10,b=20;
swap(a,b);
printf(“a=%d b=%d”,a,b);
return 0;
}
void swap(int &c, int &d)
{
int t;
t=c;
c=d;
d=t;
}

 

#include<stdio.h>
void swap(int , int );
int main()
{
int a=10,b=20;
swap(a,b);
printf(“a=%d b=%d”,a,b);
return 0;
}
void swap(int c, int d)
{
int t;
t=c;
c=d;
d=t;
}

 

output: 
20 10

output: 
10 20

 Why are Function Used in C Programming?

  1. Many programs require that a particular group of instructions be accessed repeatedly, from several different places within the program. The repeated instructions can be placed within a single function, which can then be accessed whenever it is needed. Thus the use of a function avoids the need for redundant (repeated) programming of the same instructions.
  2. A different set of data can be transferred to the function each time it is accessed.
  3. Logical clarity is achieved by using functions in Programs.
  4. Testing and correcting errors is easy because errors are localized.
  5. The flow of program and its code are easily understandable since the readability is enhanced while using the functions.
  6. A single function written in a program can also be used in other programs also.
  7. A function also promotes portability since programs can be written that are independent of system-dependent features.

P.S – We will be updating this post so visit again