Function
Function is a building block of code to do some particular job to the machine. Every function
has a return type & some argument to pass. It can pass any type of data but not the array. And in the
argument parameters receives the value of argument when it called.
General form of function is
ret_type function_name(parameter)
{
body of the function
}
has a return type & some argument to pass. It can pass any type of data but not the array. And in the
argument parameters receives the value of argument when it called.
General form of function is
ret_type function_name(parameter)
{
body of the function
}
Arguments:
To accept the arguments in functions it should be declare properly.
int main(){
char name1 = “r”;
my_name(name1);
return 0;
}
void my_name(char name2){
printf(“%c”,name2);
}
name1 is passing to the my_name function in main(). And it is collecting the argument in my_name()
function by name2 and print the name.
Passing of argument is two types. One is Call by value & another is Call by reference.
Call By Value:
To passing the argument call by value is the first option. In this way the argument is copied into
the formal parameter & passing into the function. Changes made to the parameters have no effect on
the arguments. The all changes are done on the copied item so, the main argument remains unchanged.
int sqr(int x){
x = x*x;
return x;
}
main(){
int t = 10;
printf(“Square of %d is %d”,r,sqr(r));
}Call By Reference:
In this method of passing argument, the address of the argument is copied into the parameter.
Inside the subroutine the address is used to access the actual argument in the call.
void swap(int *x,int *y){
int t;
t = *x; // changing the value of i & j
*x = *y;
*y = t;
}
main(){
int i = 10,j = 20;
swap(&i,%j);
printf(“i = %d;j = %d”,i,j);
}
To accept the arguments in functions it should be declare properly.
int main(){
char name1 = “r”;
my_name(name1);
return 0;
}
void my_name(char name2){
printf(“%c”,name2);
}
name1 is passing to the my_name function in main(). And it is collecting the argument in my_name()
function by name2 and print the name.
Passing of argument is two types. One is Call by value & another is Call by reference.
Call By Value:
To passing the argument call by value is the first option. In this way the argument is copied into
the formal parameter & passing into the function. Changes made to the parameters have no effect on
the arguments. The all changes are done on the copied item so, the main argument remains unchanged.
int sqr(int x){
x = x*x;
return x;
}
main(){
int t = 10;
printf(“Square of %d is %d”,r,sqr(r));
}Call By Reference:
In this method of passing argument, the address of the argument is copied into the parameter.
Inside the subroutine the address is used to access the actual argument in the call.
void swap(int *x,int *y){
int t;
t = *x; // changing the value of i & j
*x = *y;
*y = t;
}
main(){
int i = 10,j = 20;
swap(&i,%j);
printf(“i = %d;j = %d”,i,j);
}
Calling Function with Array:
You can call an array by a function. For that you have to pass the base address of the array
through the function. And in the function its getting the base address by a pointer (*) and use this value
to perform the operation. By a simple example it will be clear to you.
void print(char *string){
register int t;
for(t=0;string[t]; t++)
string[t] = toupper(string[t]);
putchar(string[t]);
}
main(){
char s[10];
printf(“Enter a string:”);
gets(s);
print(s);
printf(“Uppercase = %s”,s);
}
Argument to main( ) :
Till now we have pass the argument through the other supporting functions,but now its time to
pass arguments through main( ). For this we need extra parameters argc and argv. The argc is a
integer type parameter. It holds the number of arguments on the command line. Its always 1 due to the
program name. And argv is a pointer to array. Its a character pointer,it holds each of the element in the
array to the command line argument.Recursion:
Functions are calling through main function. But if a function is called through the function
itself then recursion. Let say the movie “Kartick calling Kartick”,here Kartick is calling himself,just
like that a function is calling by itself. Thats all recursion.
It has a terminal logic(like n == 1 or n == 0) & a stack memory to perform the codes.
A program with recursion :
int fact(int n){
int ans;
if(n == 1)
ans = fact(n-1)*n;
return(ans);
}
Program without recursion:
int fact(int n){
int t,ans = 1;
for(t=1;t<n;t++)
ans = ans * t;
return ans;
}
In the recursion program will be like –
Let, n=3 then
ans = fact(3);
ans = fact(3-1)*3 ;
ans = fact(2-1)*2*3;
ans = fact(1-1)*1*2*3;
ans = 1*1*2*3 = 6;
return 6;
It will print 6.
You can call an array by a function. For that you have to pass the base address of the array
through the function. And in the function its getting the base address by a pointer (*) and use this value
to perform the operation. By a simple example it will be clear to you.
void print(char *string){
register int t;
for(t=0;string[t]; t++)
string[t] = toupper(string[t]);
putchar(string[t]);
}
main(){
char s[10];
printf(“Enter a string:”);
gets(s);
print(s);
printf(“Uppercase = %s”,s);
}
Argument to main( ) :
Till now we have pass the argument through the other supporting functions,but now its time to
pass arguments through main( ). For this we need extra parameters argc and argv. The argc is a
integer type parameter. It holds the number of arguments on the command line. Its always 1 due to the
program name. And argv is a pointer to array. Its a character pointer,it holds each of the element in the
array to the command line argument.Recursion:
Functions are calling through main function. But if a function is called through the function
itself then recursion. Let say the movie “Kartick calling Kartick”,here Kartick is calling himself,just
like that a function is calling by itself. Thats all recursion.
It has a terminal logic(like n == 1 or n == 0) & a stack memory to perform the codes.
A program with recursion :
int fact(int n){
int ans;
if(n == 1)
ans = fact(n-1)*n;
return(ans);
}
Program without recursion:
int fact(int n){
int t,ans = 1;
for(t=1;t<n;t++)
ans = ans * t;
return ans;
}
In the recursion program will be like –
Let, n=3 then
ans = fact(3);
ans = fact(3-1)*3 ;
ans = fact(2-1)*2*3;
ans = fact(1-1)*1*2*3;
ans = 1*1*2*3 = 6;
return 6;
It will print 6.
Q : What does main( ) return?
Ans: The main( ) function returns an integer to the calling process,which generally the OS. Returning
the value of main( ) is equivalent to exit( ). Most C compilers automatically returns 0.
exit(0) – terminate the process.
Exit(1) – terminate the process with an error.
Ans: The main( ) function returns an integer to the calling process,which generally the OS. Returning
the value of main( ) is equivalent to exit( ). Most C compilers automatically returns 0.
exit(0) – terminate the process.
Exit(1) – terminate the process with an error.