Friday, 16 March 2012

Functions


Definition
Features of functions
Types of functions
Syntax
Category of Functions

Functions Definition:

It is a collection of executable statements which performs a specific operation is called functions

Features of Functions:
  • We can achieve Modularity by using functions. Modularity means Process of taking a larger problem (software or program), understanding it, and then breaking it into smaller, more manageable parts.
  • Each part, called a module (task i.e function), has its own well-refined task.
  • All modules work through a central module, called main.
  • Reusability Code.
Types of functions:

1. Predefined Functions (Library Functions or Built-in Functions)
2. User Defined Function




Predefined functions are provided by the system (at the time of C software installation will be available automatically) and available in Library.

Ex: scanf(), printf(), sqrt(),exit(0),...

How to use Predefined Functions in your program?
Include appropriate header files before calling the functions.
Ex:
#include<stdio.h> //this is header
void main()
{
 printf("I'm predefined function\n");
}


double sqrt(double) ;

present in <math.h> that computes the square root of the argument passed to it.

2. User Defined Function:

Now we are talking about the user defined functions. Which is defined by the user or programmer is called user defined function.

Parts of a function

1. Function Declaration or Function Prototype
2. Function Definition
3. Function Call

1. Function Declaration or Function Prototype

Syntax (or) Function defination:

return_type  function_name(arguments (optional))
{
  //body of the function
  statements;
  statements;
  return statement; //optional
}

Ex:
int add(int a,int b)
{
  int c;
  c=a+b;
  return(c);
}

3. Function Call:

void main()
{
 int add(int,int);
 int a,b,c;
 a=2;
 b=3;
 c=add(a,b); //calling function
 printf("c=%d\n",c);
}

There are two ways of calling the function or
Communication with Function:

When we are calling the function, we can pass (send) arguments or parameters in function call to the function definition in two ways:

1. Call by Value 
2. Call by Reference

Call By Value:

Actual parameters are to the function. Whatever the modifications are done in function, those changes are not reflects in calling function.

Ex: add(a,b);
Example Program:
void main()
{
 int add(int,int);
 int a,b,c;
 a=2;
 b=3;
 c=add(a,b); //calling function
 printf("c=%d\n",c);
}
int add(int a,int b)
{
  int c;
  c=a+b;
  return(c);
}

Call By Reference:

Addresses of the parameters are copied to the function. Whatever the modifications are done in function, those changes are reflects in calling function.

Use & (ampersand) operator is used in the actual parameter.
Advantages of the call by reference we can send more than one value from the function to calling function.

Ex: swap(&a,&b);

Example Program:
void main()
{
 void swap(int *,int*);
int a,b;
a=2;
b=3;
swap(&a,&b);
printf("a=%d\nb=%d\n",a,b);
}
void swap(int *x,int *y)
{
 int temp=x;
 x=y;
 y=temp;
}




Interview Questions

Is it possible to execute code even after the program exits the main() function?              
What is the difference between the functions rand(),random(),srand() and randomize()?           
How to see return value of main function?         
How do we get Square root of any number Without using sqrt() function?          
Write a Program to convert decimal to binary no.             
Can you use the function fprintf()to display the output on the screen? 
What is a static function?             
How to print a statement without using printf() in c?      
How would you use bsearch()function to search a name stored in array of pointers to string?    
Does there exist any other function which can be used to convert an integer or a float to a string          
Have you heard of "mutable" keyword?              
What is an argument ? differentiate between formal arguments and actual arguments?               
Differentiate between a linker and linkage?       
Is using exit() the same as using return?               
Why should I prototype a function?       
What is meant by malloc function           
How can send unlimited no of arguments to a function, eg printf function can take any no of arguments              
What is the purpose of main( ) function?             
How would you use the functions randomize()and random()?   
How to convert decimal to octal and hexadecimal in c program?

No comments:

Post a Comment