Slideset 3 Uta 003
Slideset 3 Uta 003
Slideset 3 Uta 003
Topics Covered
Function Definition
ret_type func_name (data_type par1,data_type par2…data_type parn)
{
}
Function Call
func_name (par1, par2… parn);
Function declaration/prototype
A prototype statement helps the compiler to check the return
type and arguments type of the function.
A prototype function consist of the functions return type,
name and argument list.
Example:
void sum( int x, int y);
How does function
work?
Example
#include<stdio.h>
void sum(int, int); // function declaration/prototype
int main()
{
int a, b;
printf("enter the two no");
scanf("%d %d", &a, &b);
sum(a,b); // function calling
}
void sum( int x, int y) // function definition
{
int c;
c=x+y;
printf ("sum is %d", c);
}
Example
#include<stdio.h>
int sum(int, int);
int main()
{
int a=10,b=20, c;
c=sum(a,b); /*actual arguments
printf(“sum is %d”, c);
}
}
void print() //func definition
{
for(int i=1;i<=30;i++)
{
printf(“*”);
}
printf(“\n”);
}
A function with no parameter and no return value
(Contd…)
}
void mul(int x, int y) //formal arguments
{
int s;
s=x*y;
printf (“mul is %d”, s);
}
A function with no parameters and with return
value
#include<stdio.h>
int sum();
int main()
{
int c=sum();
printf(“sum is %d”, c);
}
int sum()
{
int x=10,y=30;
return(x+y); //return value
}
A function with parameter and with return value
#include<stdio.h>
int max(int, int);
int main()
{
int a=10,b=20,c;
c=max(a,b);
printf (“greatest no is %d”, c);
}
int max(int x, int y)
{
if(x>y)
return(x);
else
return(y);
}
• Discuss few more programs of functions….
Argument passing techniques
Call By Value
Call By Reference
Call By Value
• It is a default mechanism for argument passing.
• When an argument is passed by value then the copy of argument is
made know as formal arguments which is stored at separate memory
location
• Any changes made in the formal argument are not reflected back to
actual argument, rather they remain local to the block which are lost
once the control is returned back to calling program
Example
void swap(int,int);
int main()
{
int a=10,b=20;
printf(“before function calling a =%d b= %d”, a, b); Output:
swap(a,b); before function calling a=10 b=20
printf(“after function calling a= %d b=%d”, a, b); value of x is 20 and y is 10
return 0;
} after function calling a=10 b=20
void swap(int x, int y)
{
int z;
z=x;
x=y;
y=z;
printf(“Value of x is %d and y is %d ”, x, y);
}
Call By Reference
In this instead of passing value, address are passed.
Here formal arguments are pointers to the actual arguments
Hence change made in the argument are permanent.
The address of arguments is passed by preceding the address
operator(&) with the name of the variable whose value you want to
modify.
The formal arguments are processed by asterisk (*) which acts as a
pointer variable to store the addresses of the actual arguments
Example
void swap(int *,int *);
int main()
{
int a=10 ,b=25; Output:
printf(“before function calling a =%d b= %d”, a, b);
swap(&a, &b); before function calling a= 10 b= 25
printf(“after function calling a= %d b= %d”, a, b); value of x is 25 and y is 10
return 0;
after function calling a=25 b= 10
}
void swap(int *x, int *y)
{
int z;
z=*x;
*x=*y;
*y=z;
printf(“Value of x is %d and y is %d ”, *x, *y);
}
• Call by value: This method copies the actual value of an
argument into the formal parameter of the function. In this
case, changes made to the parameter inside the function
have no effect on the argument.
#include<stdio.h>
int g = 20;
int main () {
printf("g = %d\n",g); //global
int g = 10; //local
printf("g = %d\n",g); //local
}
Storage Classes in C
auto
Storage memory
Initial value Garbage value
Scope Local
Life Till within the scope
auto – example 1
main()
{
auto int i, j;
printf(“%d, %d”, i, j);
}
main( ){
register int i ;
for ( i = 1 ; i <= 10 ; i++ )
printf ( "\n%d", i ) ;
}
static– value persists and shared among
functions
STATIC
Storage memory
Scope Local
EXTERN
Storage memory
Initial value zero
Scope global
Life as long as the program
extern – example 1
int i ;
main( ){
printf ( "\n i = %d", i ) ;
increment( ) ; increment( ) ;
decrement( ) ; decrement( ) ;
}
extern – 1 variable and 2 files
//file 1.c
//file2.c
#include<stdio.h> #include<stdio.h>
int a; #include"file1.c"
void fun() int main()
{ {
a=a+2; extern int a;
printf("%d",a); a=7;
} fun();
}
#include<stdio.h>
main(){
extern int i;
printf("%d",i);
}