unit-2 (part B) C- Pro (1)
unit-2 (part B) C- Pro (1)
unit-2 (part B) C- Pro (1)
Unit-II
FUNCTIONS
A function as series of instructions or group of statements with one specific purpose.
A function is a program segment that carries out some specific, well defined task.
A function is a self contained block of code that performs a particular task.
4.1 Types of functions
C functions can be classified into two types,
1. Library functions /pre defined functions /standard functions /built in functions
2. User defined functions
1. Library functions /pre defined functions /standard functions/Built in Functions
These functions are defined in the library of C compiler which are used frequently in
the C program.
These functions are written by designers of c compiler.
C supports many built in functions like
Mathematical functions
String manipulation functions
Input and output functions
Memory management functions
Error handling functions
EXAMPLE:
pow(x,y)-computes xy
sqrt(x)-computes square root of x
printf()- used to print the data on the screen
scanf()-used to read the data from keyboard.
2. User Defined Functions
The functions written by the programmer /user to do the specific tasks are called user
defined function(UDF’s).
The user can construct their own functions to perform some specific task. This type of
functions created by the user is termed as User defined functions.
1
Introduction to C Programming
i. Function header
Syntax
datatype functionname(parameters)
It consists of three parts
a) Datatype:
The data type can be int,float,char,double,void.
This is the data type of the value that the function is expected to return to
calling function.
b) functionname:
The name of the function.
2
Introduction to C Programming
2. Function Declaration
The process of declaring the function before they are used is called as function
declaration or function prototype.
function declaration Consists of the data type of function, name of the function
and parameter list ending with semicolon.
3
Introduction to C Programming
3. Function Call:
The method of calling a function to achieve a specific task is called as function
call.
A function call is defined as function name followed by semicolon.
A function call is nothing but invoking a function at the required place in the
program to achieve a specific task.
Ex:
void main()
{
add( ); // function call without parameter
}
4
Introduction to C Programming
5
Introduction to C Programming
6
Introduction to C Programming
In this category, there is data transfer from the calling function to the called
function using parameters.
But there is no data transfer from called function to the calling function.
The values of actual parameters m and n are copied into formal parameters a and b.
The value of a and b are added and result stored in sum is displayed on the screen
in called function itself.
7
Introduction to C Programming
int add();
{
void main()
{ int a,b,sum;
result=add( ); b:”);
} sum= a+b;
return sum;
}
In this category there is no data transfer from the calling function to the
called function.
But, there is data transfer from called function to the calling function.
No arguments are passed to the function add( ). So, no parameters are defined in
the function header
When the function returns a value, the calling function receives one value from
the called function and assigns to variable result.
The result value is printed in calling function.
8
Introduction to C Programming
In this category, there is data transfer between the calling function and called
function.
When Actual parameters values are passed, the formal parameters in called
function can receive the values from the calling function.
When the add function returns a value, the calling function receives a value from
the called function.
The values of actual parameters m and n are copied into formal parameters a and
b.
Sum is computed and returned back to calling function which is assigned to
variable result.
4.3 Passing parameters to functions or Types of argument passing
The different ways of passing parameters to the function are:
Pass by value or Call by value
Pass by address or Call by address
9
Introduction to C Programming Module 4
1. Call by value:
In call by value, the values of actual parameters are copied into formal parameters.
The formal parameters contain only a copy of the actual parameters.
So, even if the values of the formal parameters changes in the called function, the
values of the actual parameters are not changed.
The concept of call by value can be explained by considering the following program.
Example:
#include<stdio.h>
void swap(int a,int b);
void main()
{
int m,n;
printf("enter values for a and b:");
scanf("%d %d",&m,&n);
printf("the values before swapping are m=%d n=%d \n",m,n);
swap(m,n);
printf("the values after swapping are m=%d n=%d \n",m,n);
}
Execution starts from function main( ) and we will read the values for variables
m and n, assume we are reading 10 and 20 respectively.
We will print the values before swapping it will print 10 and 20.
The function swap( ) is called with actual parameters m=10 and n=20.
In the function header of function swap( ), the formal parameters a and b
receive the values 10 and 20.
In the function swap( ), the values of a and b are exchanged.
10
Introduction to C Programming
But, the values of actual parameters m and n in function main( ) have not been
exchanged.
The change is not reflected back to calling function.
2. Call by Address
In Call by Address, when a function is called, the addresses of actual
parameters are sent.
In the called function, the formal parameters should be declared as pointers
with the same type as the actual parameters.
The addresses of actual parameters are copied into formal parameters.
Using these addresses the values of the actual parameters can be changed.
This way of changing the actual parameters indirectly using the addresses of
actual parameters is known as pass by address.
Example:
#include<stdio.h>
void swap(int a,int b);
void main()
{
int m,n;
printf("enter values for a and b:");
scanf("%d %d",&m,&n);
printf("the values before swapping are m=%d n=%d \n",m,n);
swap(&m,&n);
printf("the values after swapping are m=%d n=%d \n",m,n);
}
11
Introduction to C Programming
Pointer: A pointer is a variable that is used to store the address of another variable.
Syntax: datatype *variablename;
Example: int *p;
#include<stdio.h>
void main()
{
inta ,*p;
p=&a;
}
In the above program p is a pointer variable, which is storing the address of variable a.
The type of formal parameters should The type of formal parameters should be
be same as type of actual parameters same as type of actual parameters, but
they have to be declared as pointers.
12
Introduction to C Programming
Page 13
Introduction to C Programming
e.g
#include<stdio.h>
main()
{
int m=1000;
func2();
printf(“%d\n”,m);
}
func1()
{
int m=10;
printf(“%d\n”,m);
}
The static variables can be declared outside the function and inside the function.
They have the characteristics of both local and global variables.
Static can also be defined within a function.
Ex: static int a,b;
#include<stdio.h>
main()
{
int I;
for (I=1;I<=3;I++)
stat();
}
stat()
{
static int x=0;
x=x+1;
printf(“x=%d\n”,x);
}
iv. Register variables
Page 14
Introduction to C Programming
Any variables declared with the qualifier register is called a register variable.
This declaration instructs the compiler that the variable under use is to be stored
in one of the registers but not in main memory.
Register access is much faster compared to memory access. Ex:
register int a;
Recursion
Recursion is a method of solving the problem where the solution to a problem depends
on solutions to smaller instances of the same problem.
Recursive function is a function that calls itself during the execution.
Consider Example for finding factrorial of 5
Factorial(5)=n*fact(n-1)
Example 1.
/******* Factorial of a given number using Recursion ******/
#include<stdio.h>
int fact(int n);
void main( )
{
int num,result;
printf("enter number:");
scanf("%d",&num);
result=fact(num);
printf("The factorial of a number is: %d",result);
}
int fact(int n)
{
if(n==0)
return 1;
else
return (n*fact(n-1));
Page 15
Introduction to C Programming
}
output :
enter number:5
The factorial of a number is:120
Fibonacci Sequence:
Page 16
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: