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

Function 2

The document discusses functions including definition, types of functions, user-defined functions, function prototype, function call, function definition, types of functions based on return type and parameters, calling methods including call by value and call by reference.

Uploaded by

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

Function 2

The document discusses functions including definition, types of functions, user-defined functions, function prototype, function call, function definition, types of functions based on return type and parameters, calling methods including call by value and call by reference.

Uploaded by

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

Function

Contents :

#Definition

#Types :
I) Pre defined( in-built ) function
II) User-defined function

# User- defined function ( Function )


Basics:
i) Function prototype / declarartion / signature
ii) Function call ( invoke)
iii) Function definition

# Types ( Classsification )

#Property :
i) Calling method ( Data passing method ) :
i) call by value
ii) call by reference
ii) Global & local variable
iii) Recursion
Definition :

It is a sub program which is a self- contained block used to perform a


specific task.

Or

It is set of stmt(s) that takes inputs, perform computation ( process ) &


results into an output value .

Types:

There are two types of functions:

i)Pre defined (In-built ) function

ii) User – defined function


i)Pre defined ( Built- in) function

i) It is also called as standard libaray functions .

ii) such as
<stdio.h> --> printf(), scanf() , putchar () , getchar() etc.

<math.h> --> pow( ) , sqrt( )

<string.h> --> strcpy( ) ,strcat( )

Iii) These functions are already defined in header files need to include only
in link section.

ii) user – defined function

i) As the name itself indicating it is user program i.e. user has to create .

ii) Every user defined function must consists of :

I) function prototype ( function declaration )


II) function call
III) function definition
I) Function prototype / declarartion / signature

i) one can create function by using following syntax

ii) syntax :

return _type function _ name ( parameter list );

Where,
return _ type : Here , we declare the data type of the value returned by
functions. It may be char, int ,float or double type.
If no return then it should be void

function_name: It is the name of function .

Parameter list : All the parameters to be passed into the function.


A function may or may not require parameters. Hence, the parameter list
is optional.

Iii) Example :

1) int add ( int , int );


function-name : add
return_type : integer
number of parameters : two
type of parameter 1 & 2 : integer

2) void fun ( int , char );


function-name : fun
return_type : No return : It is an empty return.
number of parameters : 2
type of parameter 1 : int
type of parameter2 : char
ii) function call :
i) When a main() { calling function } calls( invokes ) a function
( known as function call ) then Program ctrl is transferred to the called
function .

A called function performs a defined task & it returns back into


the main () ( calling function ).

ii) syntax
function-name( parameters );

iii) Example
add ( n1 , n2) ;

iii) function definition :

i) It consists of function header { return- type , function name,


parameters} and function's body{ block}.

ii) example

1) int add ( int m1, int m2)


{
printf(“\n %d\n”,m1+m2);
}

2) void fun ( int n, char c)


{
printf(“value of n=%d\n”, n);
printf(“value of c=%c\t”,c);

}
Example :

/* Title : program for addition of two numbers using function ( input :


main() , process : add() ,output : main() )*/

#include<stdio.h>

int add (int , int ); // function declaration

void main( )
{

int n1 , n2 , a;

printf("Enter the value for n1 & n2");


scanf("%d%d",&n1,&n2); // input stmt

a = add ( n1 , n2 ); // function call

printf(" addition = %d",a);// output stmt


}

int add( int m1 ,int m2 ) // function definition


{

return ( m1+m2);
}
Types ( Claasification )

i) A Function can be claasified into four types based upon return type &
parameter

I) A function with return type & with parameter

II) A function with return type & without parameter

III) A function without return type & with parameter

IV) A function without return type & without parameter


Example :
WAP to read a number & ckeck if it is prime number using function

I) A function with return type & with parameter

#include<stdio.h>

int prime ( int ); // function defintion

void main( )
{
int n;

printf(“\n Enter the i/p for n \n”);


scanf(“%d”, & n); // i/p stmt

prime( n) ; function call


}

int prime ( int m)


{
int a;

for( a=2 ; a<=m ; a++)


{
if( m% a == 0 )
break;
}
if( m == a)
printf(“\n given number is prime \n”);

return( 35);
}
O/P : I) first iteration

Enter the i/p for n


5
given number is prime
II) Second iteration

Enter the i/p for n


51

II) A function with return type & without parameter

#include<stdio.h>

int prime ( ); // function defintion

void main( )
{
prime( ) ; function call
}

int prime ( )
{
int a , m ;

printf(“\n Enter the i/p for m \n”);


scanf(“%d”, & m); // i/p stmt

for( a=2 ; a<=m ; a++)


{
if( m% a == 0 )
break;
}
if( m == a)
printf(“\n given number is prime \n”);

return( 35);
}
O/P :
Enter the i/p for m
11
given number is prime
III) A function without return type & with parameter

#include<stdio.h>

void prime ( int ); // function defintion

void main( )
{
printf(“\n Enter the i/p for n \n”);
scanf(“%d”, &n); // i/p stmt

prime( n ) ; function call


}

int prime ( int m )


{
int a ;

for( a=2 ; a<=m ; a++)


{
if( m% a == 0 )
break;
}
if( m == a)
printf(“\n given number is prime \n”);

return( 35);
}

O/P :
Enter the i/p for n
17
given number is prime
IV) A function without return type & without parameter

#include<stdio.h>

void prime ( ); // function defintion

void main( )
{
prime( ) ; function call
}

void prime ( )
{
int a , m ;

printf(“\n Enter the i/p for m \n”);


scanf(“%d”, &m); // i/p stmt

for( a=2 ; a<=m ; a++)


{
if( m% a == 0 )
break;
}
if( m == a)
printf(“\n given number is prime \n”);

O/P :
Enter the i/p for n
29
given number is prime
Calling (Data passing) method

There are two methods by using which one can pass the data into function
I) call by the value
II) call by the reference ( address )

I) call by the value

i) When a function is called by passing the value of the actual


parameters to the dummy ( formal ) parameters then the calling
method is said to be call by the value .

ii) Example

WAP to swap two number using function

#include<stdio.h>

void swap( int , int ); // function declaration

int main()
{
int n1 , n2 ; // declaration stmt

printf(“\n Enter the i/p for n1 & n2 \n”);


scanf(“%d%d”,&n1 , &n2 ); // i/p stmt

swap ( n1 ,n2 ); // function call

return (34) ;
}
void swap( int m1 ,int m2)
{
int t ;
printf(“Before swapping : %d %d”, m1 , m2 );

t = m1 ;
m1=m2;
m2= t ;
printf(“After swapping : %d %d”, m1 , m2 );
}

O/P :
Enter the i/p for n1 & n2
5 8

Before swapping : 5 8

After swapping : 8 5

Here , n1 & n2 are called actual parameters whereas m1 & m2 are called
formal ( dummy) parameters
II) call by the reference ( address )

i) When a function is called by passing the reference ( address ) of the


actual parameters to the dummy ( formal ) parameters which should
be pointers then the calling method is said to be call by the reference .

ii) Example

WAP to swap two number using function

#include<stdio.h>

void swap( int * , int * ); // function declaration

int main()
{
int n1 , n2 ; // declaration stmt

printf(“\n Enter the i/p for n1 & n2 \n”);


scanf(“%d%d”,&n1 , &n2 ); // i/p stmt

swap ( &n1 ,&n2 ); // function call

return (34) ;
}

void swap( int * m1 ,int * m2)


{
int t ;
printf(“Before swapping : %d %d”, *m1 , *m2 );

t = m1 ;
m1=m2;
m2= t ;
printf(“After swapping : %d %d”, *m1 , *m2 );
}
O/P :
Enter the i/p for n1 & n2
5 8

Before swapping : 5 8

After swapping : 8 5
Scope of variable

i) The scope of variables can be defined with their declaration, and


variables are declared mainly in two ways:

i) global variable

ii) local variable

i) global variable

i) A variable which is accessible by more than one function is said to be


a global variable

ii) one cannot declare many variables with same name

iii) Example

#include<stdio.h>

int n ; // global variable

void main()
{
int m=45; // local variable

printf(“\n Enter the value for n \n”);


scanf(“%d”,& n);

printf(“\n value of n =%d \t value of m=%d\n”, n, m );


}
ii) local variable

i) i) A variable which is accessible by the function only where it has


been declared is said to be a local variable

ii) one can declare many variables with same name

iii) Example

#include<stdio.h>

void main()
{
int m=45; // local variable

{
int m=5;
printf(“\n value of m=%d\n”, m );
}
printf(“\n value of m=%d\n”, m );
}

O/P :
value of m= 5
value of m=45

You might also like