4.2 Functions Note
4.2 Functions Note
Introduction
Function in C programming is a reusable block of code that makes a
program easier to understand, test and can be easily modified without
changing the calling program. In short, a larger program is divided into
various subprograms which are called as functions.
Types of Function:
1. Library / Built-in Function
▪ Library functions are also called built-in functions.
▪ These functions are already defined in header files with .h extension.
▪ This function saves our time and easy to use.
▪ Examples: printf(), scanf(), strlen(), sqrt() etc.
2. User-defined Function
▪ User defined function defined by the user to performed specific task.
▪ These functions do not need to include any header file.
▪ This take more time then library function.
▪ The user-defined functions reduce the complexity of a big program
and help in optimizing the code.
▪ Examples: sum (int a, int b), prime (int a) etc.
Difference between User-defined function and Library Function
User defined function Library function
User defined functions are Library functions are pre-defined
functions which are created by the functions.
programmer.
User defined function requires Library function requires header file
function prototype to use it. to use it.
Program development time is Program development time is
slower than library function. faster than user defined function.
It is called at compile time. It is called at run time.
The name of user defined function The name of library function can’t
can be changed any time. be changed.
The program using user defined The program using library function
function will be usually lengthy. will be usually short.
Example: add(), product() etc. Example: printf(), strlen() etc.
Advantages of Function:
▪ Big programs can be divided into smaller module using functions.
▪ Program development will be faster.
▪ Program debugging will be easier.
▪ Use of functions reduce program complexity.
▪ Program length can be reduced through code reusability.
▪ Several programmers can work on a single project.
▪ Functions can be independently tested.
▪ They facilitate top-down modular design.
Function Definition, Prototype, Call and Return Statements
Function Definition
Function definition consists of the whole description and code of a
function. It tells what the function is doing and what are its input and
outputs. Function definition consists of two parts:
▪ Function header
▪ Function body
Syntax
return_type function_name(type1 arg1, type2 arg2, ….)
{
statements;
return;
}
Example
Function Prototype or Function Declaration
Function prototype provides the following information to the Computer:
▪ The type of the value returned
▪ The name of the function
▪ The number and the type of the arguments that must be supplied in
a function call.
Syntax
return_type function_name(type arg1, type arg2, ….)
Example
Function Call
A function call is specified by the function name followed by the arguments
enclosed in parenthesis and terminated by a semicolon.
Syntax
variable= function_name(arg1, arg2, ….)
Example
result=sum(a, b);
Return Statement
One of the main features of the function is that it can return a value to the
calling function. For this, a return keyword is used.
Syntax
return(expression);
Example
return(sum);
Function Example
#include <stdio.h>
#include <conio.h>
int add(int x, int y); // function prototype
void main()
{
int a, b, result;
// asking for input
printf("Enter two numbers:\n");
scanf("%d%d", &a,&b);
void main()
{
{
int a=20; //you can also write: int auto a=20
printf("%d",a);
}
printf(" %d",a); //a is not visible here
}
Output
External Storage Class
▪ The default storage class of all global variables (variables declared
outside function) is external storage class.
▪ The keyword extern is used to define an external storage class.
▪ It stores in RAM.
Example:
#include<stdio.h>
extern int i=5; //extern variable, you can also write: int i
void main()
{
printf("%d",i);
}
Output
void main()
{
register int a=10;
printf("%d",a);
}
Output
Concept of Recursion
Recursion is the technique of making a function call itself. This technique
provides a way to break complicated problems down into simple problems
which are easier to solve.
Example
// Write a program to calculate sum of n natural numbers using recursion.
[2074, Set A Q.N. 4]
#include <stdio.h>
int sum(int n);
void main()
{
int n, result;
printf("Enter last number: ");
scanf("%d", &n);
result=sum(n);
printf("Sum is %d", result);
}
int sum(int n)
{
if(n<=0)
{
return 0;
}
else
{
return(n+sum(n-1));
}
}
Output