BCP Unit-5 Wordpress PDF
BCP Unit-5 Wordpress PDF
UNIT: 5
Functions
Introduction to Functions:
Definition of Function: Function is a group of statement to perform a specific task.
Types of functions:
1. Library function
2. User-defined Function
1. By using functions, we can avoid rewriting same logic/code again and again in a program.
2. We can call C functions any number of times in a program and from any place in a program.
3. We can track a large C program easily when it is divided into multiple functions.
User-defined Function:
The function which is develop by user itself is known as User-defined function.
Example: main()
User-defined function is created by the user at the time of writing of program.
Example:
int sum(int x, int y)
{
int total;
total = x + y;
return total;
}
When a function is called using the value of variables, then it is known as call by value.
The value of variables, which are to be passed, will be copied from the variables of the calling functions
to the variables of the called functions.
All the process done on the duplicate variables rather then actual variables.
Example:
#include <stdio.h>
void swap(int , int); // function prototype
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
swap(a,b); // function call
printf("After swapping values in main a = %d, b = %d\n",a,b); //actual parameters a=10, b=20
}
}
void swap (int a, int b)
{
int temp;
temp = a;
a=b;
b=temp;
printf("After swapping values in function a = %d, b = %d\n",a,b); // Formal parameters, a=20, b=10
}
When a function is called using the address of variables, then it is known as call by reference.
Instead of passing the value of variables from calling function to the called function, addresses of the
variables are passed.
All the process done on the actual variables.
Example:
#include <stdio.h>
void swap(int *, int *); // function prototype
void main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
Recursion
Advantages of Recursion:
The recursion is very flexible in data structure like stacks.
Using recursion, the length of the program can be reduced.
Disadvantages of Recursion:
It requires extra storage space.
The recursion function is not efficient in execution speed and time.
Proper termination is required; otherwise it leads to infinite loop.
Built-in Functions:
C Maths Function:
The <math.h> header file contains various methods for performing mathematical operations such as
sqrt(), pow(), ceil(), floor() etc.
ceil(number)
Rounds up the given number.
It returns the integer value which is greater than or equal to given number.
Ex: ceil(3.6)
It returns 4.
floor(number)
Rounds down the given number. It returns the integer value which is less than or equal to given number.
Ex: floor(3.6)
It returns 3.
sqrt(number)
Returns the square root of given number.
Ex: sqrt(16)
It returns 4.
pow(base, exponent)
Returns the power of given number.
Ex pow(2,4)
It returns 16.
abs(number)
Returns the absolute value of given number.
Ex: abs(-16)
It returns 16.
C String Functions:
There are many important string functions defined in "string.h" library.
3) strcat(first_string, second_string) concats or joins first string with second string. The
result of the string is stored in first string.
Storage Classes in C:
Storage classes in C are used to determine the lifetime, visibility, memory location, and initial value of a
variable. There are four types of storage classes in C
Automatic
External
Static
Register
extern RAM Zero Global Till the end of the main program Maybe declared
anywhere in the program
static RAM Zero Local Till the end of the main program, Retains value
between multiple functions call