Functions

Functions

Published by: Nuru

Published date: 21 Jun 2021

Functions Photo

Functions

A functions declaration tells the compiler about a function's name, return type, and parameters. Functions definition provides the actual body of the function.
A function is a block of statements that performs a specific task. A function is a complete and independent program that is used (or invoked) by the main program or other subprograms. A subprogram receives values called arguments from a calling program, performs calculations, and returns the results to the calling program.

Advantages of using Functions

  1. It facilitates top-down modular programming. In this programming style, the high-level logic of the overall problem is solved first while the details of each lower-level function are addressed later.
  2. The length of the source program can be reduced by using functions at appropriate places. This factor is critical with microcomputers where memory space is limited.
  3. It is easy to locate and isolate a faulty function for further investigation.
  4.  A function may be used by many other programs this means that a c programmer can build on what others have already done, instead of starting over from scratch.
  5.  A program can be used to avoid rewriting the same sequence of code at two or more locations in a program. This is especially useful if the code involved is long or complicated.
  6.  Programming teams do a large percentage of programming. If the program is divided into subprograms, each subprogram can be written by one or two team members of the team rather than having the whole team to work on the complex program

We already know that C supports the use of library functions and user-defined functions. The library functions are used to carry out a number of commonly used operations or calculations. The user-defined functions are written by the programmer to carry out various individual tasks.

 

Two types of functions:

⚫ Library functions (Built-in functions):

These functions are provided in the programming language and we can directly use them as required. However, the functions name, return type, number of arguments, and types must be known in advance. For e.g. printf(), scanf(), sqrt(), getch(),etc.

⚫ User-defined functions:

These functions are written by the user. The user selects the name of the function, return type, number of arguments, and types.

⚫ Note: main() is a user-defined function. However, the name of the function is defined or fixed by the programming language.

 

Function definition

⚫ The collection of program statements that describes the specific task to be done by the function is called a function definition.
⚫ A function definition consists of function header (a function name, return type, and number and types of arguments)and function body (block of code or statements enclosed in parentheses).

⚫ Syntax:
return_type function_name(data_type variable1, …,data_type variableN)
{
…………………;
…………………;
statements;
}

return_type is optional. The default value is an integer.

⚫ Whenever return_type is provided, a value must be returned using the return statement except for the void case.
⚫ function_name is a user-defined name given to the function. (Same as identifier naming)
⚫ variable1, …,variableN are called formal arguments or formal parameters that are passed to the function. Also these are local variables for the function

Example of the function definition:
#include
int add(int c, int d)
{
int sum;
sum = c+d;
return sum;
}
void main()
{
………;
}

Calling a function through main()

⚫ A function can be called by specifying the function name, followed by a list of arguments enclosed in parentheses and separated by commas.
⚫ For example, the function add() can be called with two arguments from main() function as:add(a, b);
⚫ These arguments appearing in the function call are called actual arguments or actual parameters.
⚫ In this case, main() is called calling function and add() iscalled called function.

Note:
⚫ In a function call, there must be one actual argument for each formal argument. This is because the value of the actual argument
is transferred into the function and assigned to the corresponding formal argument.
⚫ If a function returns a value, the returned value can be assigned to a variable of data type same as the return type of the function to
the calling function.
⚫ When a function is called, the program control is passed to thefunction and once the function completes its task, the program
control is transferred back to the calling function.

#include
#include
int add(int c, int d)
{
int sum;
sum = c+d;
return sum;
}
void main()
{
int a=50,b=100, x;
x=add(a, b);
printf(“%d”, x);
getch();

void add(int c, int d)
{
int sum;
sum = c+d;
printf(“%d”, sum);
}
void main()
{
int a=50,b=100;
add(a, b);
getch();
}

Write functions to add, subtract, multiply and divide two numbers a and b.

// Combination Problem
#include
#include
long factorial(int n)
{
long fact=1;
int i;
for(i=1;i<=n;i++)
fact *= i;
return fact;
}
void main()
{
long f1=1,f2=1,f3=1,comb;
int n, r;
clrscr();
printf("\nEnter n and r:");
scanf("%d %d",&n,&r);
f1=factorial(n);
f2=factorial(n-r);
f3=factorial(r);
comb=f1/(f2*f3);
printf("\n The combination is: %ld", comb);
getch();
}

 

Function Prototype or Declaration

⚫ The function prototype is a model or blueprint of the function.
⚫ The function prototype is necessary if a function is used before the function is defined.
⚫ When a user-defined function is defined before the use,function prototype is not necessary (maybe given though).
⚫ Syntax:
return_type function_name(data_type1, …,data_typeN);
⚫ E.g.
void add(int, int);

#include
#include
int add(int, int); //Function Prototype
void main()
{
int a=12,b=5,x;
clrscr();
x=add(a,b);
printf("%d",x);
getch();
}
int add(int c, int d)
{
int sum;
sum=c+d;
return;

}

 

The return Statement

The return statement serves two purposes:
1. It immediately transfers the control back to the calling function (i.e. no statements within the function body after the
the return statement is executed).
2. It returns the value to the calling function.

Syntax:
return (expression);
where expression, is optional and, if present, it must evaluate to a value of the data type specified in the function header for
the return_type.

Note: When nothing is to be returned, the return_type in thefunction definition is specified

/*Program using function to find the greatest number among three numbers*/
#include
#include
int greater(int, int);
void main()
{
int a, b, c, d, e;
clrscr();
printf("\n Enter three numbers:");
scanf("%d %d %d",&a,&b,&c);
d=greater(a, b);
e=greater(d, c);
printf("\n The greatest number is:%d", e);
getch();
}
int greater(int x, int y)
{
if(x>y)
return x;
else
return y;
}

Function parameters

⚫ Function parameters are the means for communication between the calling and the called functions.
⚫ Two types: formal parameters and actual parameters.
⚫ Formal parameters are given in the function definition while actual parameters are given in the function call.
⚫ The name of formal and actual parameters need not be the same but data types and the number of parameters must match.