Reading:  

Getting started with C Language


Functions in C

Functions

Functions are the named part of a program which can be invoked from any part of the program. Functions are of two types library functions and user defined functions. main () is the example for user defined functions printf() , scanf() are examples for library functions. 

The main difference between library and user defined functions are that library functions do not require any definition where as we have to develop user defined functions while writing the program. 

Need of functions

Every program must have a main function. The execution of program begins from the main function. We can also define a program only with main function without any other user defined functions. But if we define a large program without any user defined functions then the program will be too large and complex. Then the problem is while testing, debugging and maintaining these tasks will be too difficult.

The main advantages of functions are if we divide the program into many functions then each part may be independently coded and later they are combined into a single unit. These sub divided program parts are called functions. Functions make a program easier to understand debug and test.

In many times we have to do the same operations or calculations, for example if we want to find sum of two numbers many times in a program at several points we want to repeat the same program statements throughout the program whenever they are needed. If we make those repeated steps as a function we only want to call the function wherever they are required. This saves the time and space of a program. This sub-sectioning of a program is called functions.

Advantages of functions

  • It facilitates top-down modular programming.
  • Can reduce the size of a program.
  • Easy to understand and locate faults or errors of a function
  • Reusability of the functions in other programs.

How to write a function

A function has two components the function header and body of the function. The function header is the data type of return value followed by function name and a set of arguments. This argument list is optional if there is more than one argument it is separated by commas and the arguments are always enclosed in parenthesis along with the data type.

Syntax :

Return-type  function-name (argument list);

The argument list contains its return type.

Here  return-type is the data type of the variable that is returned by the function , function-name is the name of the function it must follow the same rules of formation as other variable names in C, additional care must taken to avoid duplicating library routine. The argument list contains its return type and its arguments. The argument variables receive values from the calling function. Thus providing a means for data communication from the calling function to the called function.

Return values and their types

A function may or may not return any value to the calling function. If want to return any value the keyword return is used. The return statement may be of the form

return;

or

return(expression);

the return does not return any value. When a return is found then the control passes to the calling function.

Calling a function

A function can be called by simply using the function name in a statement.

Syntax :

Function-name( argument list (if any));

Example :

addition(p,q);

 

Function to add two numbers.

int addtion(int a,int b) 
{
int c = a + b;
return c;
}

Category of functions

  • Functions with no arguments and no return values
  • Functions with arguments and no return values
  • Functions with arguments and return values

Arguments and no return values

When a function has no arguments it does not receive any data from the calling function. Also when it does not return any value, the calling function does not receive any data from the called function. 

{
…..
addition(); // function call
…..
}
void addition()
{
int a,b,c;
printf(“ Enter two numbers”);
scanf(“%d %d”,&a,&b);
c=a+b;
printf(“sum is %d”,c);
}

Where there is arguments and no return values.

Arguments but no return values

Here in this method the arguments are passed to the functions but do not return any value from the function.

{
…..
printf(“ Enter two numbers”);
scanf(“%d %d”,&a,&b);
addition(a,b); // function call
…..
}
void addition(int p, int q)
{
int c;
c= p + q;
printf(“sum is %d”,c);
}

With arguments and with return values

Here the arguments are passed to the function and receives a return value from that function.

{
…..
printf(“ Enter two numbers”);
scanf(“%d %d”,&a,&b);
c=addition(a,b); // function call
printf(“sum is %d”,c);
…..
}

void addition(int p, int q)
{
int c;
c= p + q;
return ( c );
}

The arguments can be passed into a function in two methods they are.

  • Call by value
  • Call by reference

Call by value

When a value is passed to a function via an actual argument, the value of the actual argument is copied into the function. Therefore, any change in formal parameter will not affect the actual parameters, thus we can change the formal parameters value without changing its actual parameters value. This procedure of passing the value of an argument to a function is known as call by value.

{
…..
printf(“ Enter two numbers”);
scanf(“%d %d”,&a,&b);
c=addition(a,b); // function call
printf(“sum is %d”,c);
…..
}
void addition(int p, int q)
{
int c;
c= p + q;
return ( c );
}

Where  a, b are the actual parameters and p, q are formal parameters the value in a, b are copied into p, q. 

Call by reference

In call by value the copy of actual parameters are passed into the function and save as formal parameters but in call by reference the address of the actual parameters are passed as arguments then any change in formal parameters will affect the actual parameters value because it deals with the location where the actual parameters are stored.

{
…..
printf(“ Enter two numbers”);
scanf(“%d %d”,&a,&b);
addition(&a,&b); // function call
…..
}


void addition(int *p, int *q)
{
int c;
c = * p + * q;
printf(“sum is %d”,c);
}

Recursion

A function call itself is called recursion. The function call contains in the body of the same function.  When we write any recursive functions, it must contain an if statement. This if statement helps to stop the function.

Example :

Factorial(n)
int n;
{
   int fact;
   if(n==1)
          return (1);
  else
      fact = n * Factorial(n-1);
    return(fact);
}

Working of the function

If n = 3 then

fact   = 3  * Factorial ( 2 ) ;

        = 3 * 2 * Factorial ( 1 )

        = 3 * 2 * 1

        = 6

 

 

 

 

 

 

 

 

 

 

  

 

 

Description

This tutorial introduces you to C language. Many tutorials has been written. We are writing this tutorial to be brief and to the point, so that you concentrate on what matter, rather than finding yourself in a mess. We want you to get a start and build form knowledge you will gain from this tutorial



Environment

We will cover environment needs as we progress through this tutorial. At the very least you will need a PC with windows or Linus operating system.

Audience

Programmers new to C Language

Learning Objectives

Learn basics of C language and build from that knowledge.

Author: Subject Coach
Added on: 3rd Jan 2015

You must be logged in as Student to ask a Question.

None just yet!