0% found this document useful (0 votes)
10 views

4.2 Functions Note

Functions in C programming allow dividing a program into reusable blocks of code. The document discusses different types of functions like library functions and user-defined functions. It also covers function definition, prototype, call, return statements, and how functions are used to pass values between programs.

Uploaded by

susan1002003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

4.2 Functions Note

Functions in C programming allow dividing a program into reusable blocks of code. The document discusses different types of functions like library functions and user-defined functions. It also covers function definition, prototype, call, return statements, and how functions are used to pass values between programs.

Uploaded by

susan1002003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Functions

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);

result=add(a, b); // function call


printf("Sum of two numbers: %d", result);
}

int add(int x, int y) //function definition


{
int sum=x+y;
return(sum); //return statement
}
Output
Accessing a Function by Passing Values
Types of user defined function:
1) Function with no arguments and no return value
Function with no argument means the called function does not
receive any data from calling function and Function with no
return value means calling function does not receive any data from
the called function. So there is no data transfer between calling and
called function.
Example:
/* program to calculate the area of square */
#include <stdio.h>
void area(); //function prototype
int main()
{
area(); //function call
return 0;
}
void area() //function definition
{
int square_area, square_side;
printf("Enter the side of square :");
scanf("%d",&square_side);
square_area = square_side * square_side;
printf("Area of Square = %d",square_area);
}

In the above program, area ( ); function calculates area and no


arguments are passed to this function. The return type of this
function is void and hence return nothing.
2) Function with no arguments and one return value
As said earlier function with no arguments means called function
does not receive any data from calling function and function with one
return value means one result will be sent back to the caller from the
function.
Example:
#include <stdio.h>
int area(); //function prototype with return type int
int main()
{
int square_area;
square_area = area(); //function call
printf("Area of Square = %d",square_area);
return 0;
}
int area() //function definition
{
int square_area,square_side;
printf("Enter the side of square :");
scanf("%d",&square_side);
square_area = square_side * square_side;
return square_area;
}

In this function int area( ); no arguments are passed but it returns


an integer value square_area.
3) Function with arguments and no return values
Here the function will accept data from the calling function as there
are arguments, however, since there is no return type nothing will
be returned to the calling program. So it’s a one-way type of
communication.
Example:
#include <stdio.h>
void area( int square_side); //function prototype
int main()
{
int square_side;
printf("Enter the side of square :");
scanf("%d",&square_side);
area(square_side); //function call
return 0;
}
void area(int square_side) //function definition
{
int square_area;
square_area = square_side * square_side;
printf("Area of Square = %d",square_area);
}

In this function, the integer value entered by the user


in square_side variable is passed to area(); .The called
function has void as a return type as a result, it does not return value.
4) Function with arguments and return value
Function with arguments and return value means both the calling
function and called function will receive data from each other. It’s
like dual communication.
Example:
#include <stdio.h>
int area(int square_side); //function prototype with return type int
int main()
{
int square_area,square_side;
printf("Enter the side of square :");
scanf("%d",&square_side);
square_area = area(square_side); //function call
printf("Area of Square = %d",square_area);
return 0;
}
int area(int square_side) //function definition
{
int square_area;
square_area = square_side * square_side;
return square_area;
}
Concept of Storage
The period of time during which memory is associated with a variable is
characterized by storage classes. C supports four storage classes
automatic, external, static and register.

Automatic Storage Class


▪ The default storage class of all local variables (variables declared
inside block or function) is auto storage class.
▪ The keyword auto is used to define an automatic variable.
▪ It stores in RAM.
Example:
#include<stdio.h>

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

Static Storage Class


▪ The static storage class is used to create variables that hold value
outside its scope until the end of the program.
▪ It allows to initialize only once and can be modified any time.
▪ The keyboard static is used to define static variables.
▪ It stores in RAM.
Example:
#include<stdio.h>

static int i=10;


void main()
{
printf("%d\n",i);
i=25;
printf("%d",i);
}
Output:
Register Storage Class
▪ The memory of the register variable is allocated in CPU Register but
not in Computer memory (RAM).
▪ The keyboard register is used to define register variables.
▪ The register variables enable faster accessibility compared to other
storage class variables.
▪ As the number of registers inside the CPU is very less we can use very
less number of register variables
Example:
#include<stdio.h>

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

You might also like