Functions

 

 Functions in c

Function are the blocks of C program which perform specific task, which is called from other function, we can divide a large program into the basic building blocks known as function. The function contains the set of programming statements enclosed by {}. A function can be called multiple times to provide re usability and modularity to the C program. In other words, we can say that the collection of functions creates a program. The function is also known as procedure or subroutine in other programming languages.
Advantage of functions in C
There are the following advantages of C functions.
  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.
  4. Re usability is the main achievement of C functions.
  1. However, Function calling is always a overhead in a C program.
  1. Library Functions or Pre-define Function or Inbuilt Function: are those function which is provide to us by programming language. But we use this function we need to include some header files like stdio.h, conio.h. These header files provide to us many function like, scanf, printf, clrscr, getch etc.
  1. User-defined functions: are the functions which are created by the user or C programmer, so that he/she can use it many times. It reduces the complexity of a big program and optimizes the code. User define function are define by the user are according to their requirement, known as user define function. User define function have four types:-
No
C function
Syntax
1
Function declaration
return_type function_name (argument or parameter list);
2
Function call
function_name (argument list or parameter list)
3
Function definition
return_type function_name (argument list) {function body;}
        
Types of Functions

There are two types of functions in C programming:

1)Without Return Without Arguments
2)Without Return With Arguments
3)With Return Without Arguments
4) )With Return With Arguments            
For creating user define function we have to follow three basic steps. These are given below:-

1)    Function declaration in c:- Function declaration is also known as function prototyping. Before using function in your program it must be declared and defined. Function declaration or prototyping tells the compiler that is function is used later in the programming. It also tells the compiler their namereturn type, and arguments of the function. No function can be called from any other function that hasn’t first been declared. The declaration of a function is also called function prototype. A prototype always ends with a semicolon(;). Function is declaration same as we declare variable before using them.
2)    Function definition in c:- In function definition we can define the body of the function. It means we can write the number of statement inside the opening and closing braces of the function, which is executed when the function is called by other function A function definition must match (their name, return type, parameter list) with its declaration. The definition tells the compiler how the function works.
3)    Function calling in c:- Function calling means to execute the function. After declaration and definition of the function we can call the function for their execution. When you call function, execute begins with first statement to last statement of the function. Function can also call other function and can even call themselves but the main function call the sub function.


Categories of user defined functions:

Without Return Without Argument: - This is the first type of user define function and also known as no return no argument. No return means that function start with 'void' keyword. It tells the compiler that the function cannot return any value. No arguments mean we cannot pass anything from the calling function to called function. It means we can leave the parentheses blank. In C, the declaration of the show () function can be something like this:

                              void show();
We can also write it as;
void show(void);

 Note that the keyword 'vold' is used in the declaration to indicate to the compiler that no argument is passed to the function. The compiler will produce an error message if an argument is passed to show ()
Function later in a program when this function is called.
Example 1 WAP to add and subtract two number using function with no return no argument

#include<stdio.h>
#include<conio.h>
void sum();
void sub();
void main()
{    
clrscr();
sum();
sub();
getch();
}
                void sum()
{
                int a,b,c;
                printf(“enter the first number”);
                scanf(“%d”,&a);
                printf(“enter the second number”);
                scanf(“%d”,&b);
                c=a+b;
                printf(“sum=%d”,c);
}
void sub()
{
        int a,b,c;
        printf(“enter the first number”);
        scanf(“%d”,&a);
        printf(“enter the second number”);
        scanf(“%d”,&b);
        c=a-b;
        printf(“sub=%d”,c);
}

Simple Program
                
#include<conio.h>
#include<stdio.h>
void show();  
void main ()  
{  
clrscr();
printf("Hello ");  
    show(); 
    getch(); 
}  
void show()  
{  
        printf("Tech JCC");  
}  
Output

Example another
#include<stdio.h>  
void sum();  
void main()  
{  
printf("\ncalculate the sum of two numbers:");  
sum();  
}  
void sum()  
{  
int a,b;   
 printf("\nEnter two numbers");  
 scanf("%d %d",&a,&b);   
printf("The sum is %d",a+b);  
}  
Output
 calculate the sum of two numbers:

Enter two numbers 12
14

The sum is 26


3.   Without Return with argument:- This is the second type of user define functions. This function starts with void keyword, which tells the compiler that function hasn't any return type. It can't return any thing to its calling program. But with argument means we must pass information to the calling function. In this we should declare variables inside the parentheses we cannot leave the parentheses blank. The argument of a function is placed between the parentheses after the function name. If a function has more than one argument then arguments must be separated by commas. We can declare a function with a fixed number of arguments, but then you need to specify the data type of each argument. Arguments are of two types: -

1)Formal arguments
                  2) Actual arguments

1) Formal Arguments:-Formal arguments are those arguments which are declared at function declaration and function definition time. For example:
void sum(int a, int b); Here a and b are the formal arguments which are passed Into the sum function.
2) Actual arguments:-Actual arguments are those arguments which are passed to function at calling time. For example
sum(3,5);
Here 3 and 5 are actual arguments, which are passed into the sum() function
example 2
#include<stdio.h>
#include<conio.h>
void sqr(int n);
void cube(int n);
void main()
{
          int a;
          clrscr();
          printf(“enter any number”):
          scanf(“%d”,&a):
          sqr(a);
          cube(a);
          getch();
}
void sqr(int n)
{
int m;
m=n*n;
printf(“the square =%d”,m);
}
void cube(int n)
{
int m;
m=n*n*n;
printf(“the cube =%d”,m);
}


local variables: - local variables are those variables, which are declared inside a block or function. these variables are accessible only within the block and are passed as argument into the function or declared inside the function. these are also known as function level variable because they are accessible only within the function.
example of local variable:-
#include<stdio.h>
#include<conio.h>
void show();
void main()
{
int a=9;//local variable
show();
printf(“%d”,a);
getch();
}
void show()
{
int b=23;//local variable
printf(“%d”,b);
}
Global variables: - global variables are those variables, which are declared outside the block or function. these are known as global variables. which is accessible by each block of the program. 
example of Global variable:-
#include<stdio.h>
#include<conio.h>
int a;//gloable variable
void show();
void main()
{
 a=9;// gloable variable
show();
getch();
}
void show()
{
printf(“global variable=%d”,a);
}





with return without argument: - return type functions are those functions, which return a value to the calling function. these functions start with a specific data type in place of void keyword. these type of functions use return keyword to send the data back to the calling function.

as shown in the example:
example 3. wap to find the factorial of a given number using function with return type.
#include<stdio.h>
#include<conio.h>
int fact();
void main()
{
int f;
clrscr();
f=fact();
printf(“the factorial =%d”,f);
getch();

}
int fact()
{
int a,b=1,n;
printf(“enter any number”);
scanf(“%d”,&n);
for(a=1;a<=n;a++)
{
b=b*a;
}
return b;
}



4)   with return with argument:- this function contains argument and also return the value to its calling function, as show in the given example:-
#include<stdio.h>
#include<conio.h>
int reverse(int n);
void main()
{
int a,b;
clrscr();
printf(“please enter any number”);
scanf(“%d”,&a);
b=reverse(a);
printf(“reverse of a number=%d”,b);
getch();
}
        int reverse(int n)
{
int r,s=0;
while(n>0)
{
r=n%10;
s=s*10+r;
n=n/10;
}
return s;
}






Recursive Function:- Recursive function are those function, which call itself again and again. Normally function is called by other function , but recursive function it call itself repeatedly. This process known recursion.

as shown in the example below:

#include<stdio.h>
#include<conio.h>
int fact(int n);
void main()
{
        int n,f;
        clrscr();
        printf(“enter any number”);
        scanf(“%d”,&n);
        f=fact(n);
        printf(“factorial number=%d”,f);
        getch();
}
int fact(int n)
{
        if(n==1)
        {
                return 1;
}
else
{
return n*fact(n-1);
}
}

Example : WAP to print the Fibonacci series .

#include<stdio.h>           
#include<conio.h>
void fibo(int a,int b);
void main()
{
        clrscr();
        fibo(0,1);
        getch();
}
void fibo(int a,int b)
{
        printf(“%d”,a);
        if(a>100)
        {
                return;
}
fibo(b,a+b);
}



Program print table using recursion function in c/c++.

#include<stdio.h>
#include<conio.h>
void fact(int n,int m);
void main()
{
int n,f;
clrscr();
printf("enter any number");
scanf("%d",&n);
fact(n,1);
getch();
}
void fact(int n,int m)
{
printf("\n%d",n*m);
if(m==10)
return;
m++;
fact(n,m);
}


Print a to z using recursive function c 

#include<stdio.h>
#include<conio.h>
void abc(char);
void main ()
{
        clrscr();
        abc('a');
        getch();
}
void abc (char a)
{
        printf("\t%c",a);
        if (a=='z')
        return;
        a++;
        abc(a);
}

Print z to a using recursive function c

#include<stdio.h>
#include<conio.h>
void abc (char);
void main()
{
        clrscr();
        abc('z');
        getch();
}
void abc (char a)
{
        printf("\t%c",a);
        if(a=='a')
        return;
        a--;
        abc(a);
}







Call by Value :-   In C language by default, the arguments are passed to the function by value. It means the actual arguments copy their value into the formal arguments. In call by value, we can pass the value. If any change is done into the formal arguments, it doesn't affect the actual arguments because both the arguments use the different memory location.

As show in the example

#include<stdio.h>
#include<conio.h>
void show(int a);
void main()
{           
        clrscr();
        int x;
        printf(“enter the number”);
        scanf(“%d”,&x);
        show(x);
        printf(“now value of x=%d”,x);
        getch();
}
void show(int a)
{
        a=a+1;
        printf(“value of a=%d”,a);
}
output:-
enter any number 5
the value of a=6
now value of x=5


Callby reference  :-In call by reference we can pass the address of a variable in place of value. It means we can pass the address of actual argument to the formal argument. If any change is done in to the formal arguments ,it also affects the actual arguments, because the address of the actual arguments are passed to the formal arguments. In call by reference we can pass pointer variable as formal
As show in the example:-
#include<stdio.h>
#include<conio.h>
void show(int *a);
void main()
{      
        clrscr();
        int x;
        printf(“enter the number”);
        scanf(“%d”,&x);
        show(&x);
        printf(“now value of x=%d”,x);
        getch();
}
void show(int *a)
{
        *a=*a+1;
        printf(“value of a=%d”,*a);
}

output:-

enter any number 5
the value of a=6
now value of x=6






Passing array as argument into the function :-Sometimes it's difficult to call a function that requires number of arguments. One way around this is to store the variables into an array So we can pass an array as argument into the function. 
As shown in the example:

#include<stdio.h>
#include<conio.h>
void show(int a[]);
void main()
{
int x[5],i;
clrscr();
for(i=0;i<5;i++)
{
printf(“enter any number”);
scanf(“%d”,&x[i]);
}
show(x);
getch();
}

void show(int a[])
{
int i;
for(i=0;i<5;i++)
{
printf(“\n%d”,a[i]);
}

}




Passing structure as argument into the function:-
We can also pass structure as an argument into the function same as any other variable.
 As shown in the example:

#include<stdio.h>
#include<conio.h>
struct student
{
char name[20];
int roll,marks;
};
void show(struct student);
void main()
{
struct student s;
clrscr();
printf(“enter the student name”);
scanf(“%s”,&s.name”);
printf(“enter the student roll number”);
scanf(“%d”,&s.roll”);
printf(“enter the student marks”);
scanf(“%d”,&s.marks”);
show(s);
getch();
}
void show(struct student s)
{
printf(“\student name=%s”,s.name);
printf(“\student roll=%d”,s.roll);
printf(“\student marks=%d”,s.marks);
}


Return Structure from function , a simple program

#include<stdio.h>
#include<conio.h>
struct abc
{

          int a;
};
abc show();
void main()
{

          clrscr();
          abc obj;
          obj=show();
          printf("%d",obj.a);
          getch();
}
abc show()
{
         abc obj1;
         obj1.a=17;
         return obj1;
}


Return Structure from function and pass structure to function , a simple program

#include<stdio.h>
#include<conio.h>
struct abc
{

          int a;
};
abc show(abc);
void main()
{

          clrscr();
          abc obj,obj1;
          obj.a=16;
          obj1=show(obj);
          printf("%d",obj1.a);
          getch();
}
abc show(abc obj2)
{
          obj2.a=obj2+13;
         return obj2;
}
           

           

Nested Function :-
Nested function means a function inside a function. But C does not allow nested functions. But we can call one function into another function. It is also known as nested function. As shown in the example:
#include<stdio.h>
#include<conio.h>
 void sum(int a, int b);
void input(int a, int b);
void input(int a, int b)
{
sum(a,b);
}
void sum(int a, int b)
{
int c;
c=a+b;
printf("the sum=%d”,c);
}
void main()
{
int x,y;
clrscr();
printf("enter the value of x");
scanf("%d",&x);
printf("enter the value of y");
scanf("%d",&y);
input(x,y);
getch();
}




0 comments:

Post a Comment