Pages

Showing posts with label user defined function. Show all posts
Showing posts with label user defined function. Show all posts

Wednesday, July 13, 2011

Function in C/C++ Programming

Function is one of the mostly used keywords in C and C++ programming. Function is used to perform a task into a block statement. There are two types of function in C and C++ programming.
  1.  User defined functio
  2.  Built-in function
User Defined Function

User defined function is defined by C and C++ programmer to complete a task into a block statement. The basic structure of a user defined function is given below.

return_data_type function_name(argument1, argument1, …)
{
    return return_value;
}

Example of a user defined function

int sum(int a, int b)
{
   int c;
   c=a+b;
   return c;
}

In the above example, a user defined function named sum() is declared. The sum() function accepts two integer arguments and do summation of  these two  values and then return the summation result to the calling function.

User defined function must be called; otherwise the function will not be executed. The function has to be called with the following rule.

function_name(argument1, argument2, …);

According to function calling rule, you can call the above sum() function like below.

int sum_result=sum(10,15);

Now the sum() function will return the summation of its argument and put the result into sum_result variable.

User defined function must be declared before calling function; otherwise program will show an error message. If you don’t declare user defined function before calling, you must declare the function prototype. The function prototype declaration rule is given below.

return_type function_name(argument1, argument2, …);   

So, the prototype of your sum() function will be like below.

int sum(int, int);

Example of a user defined function without prototype

#include<stdio.h>
#include<conio.h>

// User defined function declration

int sum(int a, int b)
{
  int c;
  c=a+b;
  return c;
}
void main()
{
  clrscr();
  int sum_result=sum(10,15);
  printf("The summation is: %d",sum_result);
  getch();
}

Example of a user define function with prototype

#include<stdio.h>
#include<conio.h>
int sum(int,int);

void main()
{
  clrscr();
  int sum_result=sum(10,15);
  printf("The summation is: %d",sum_result);
  getch();
}
// User defined function declration

int sum(int a, int b)
{
  int c;
  c=a+b;
  return c;
}

Program Output





Built-in function

C/C++ provides a lot of built-in functions which help C/C++ programmer to do their task so easily. Built-in functions are those functions whose definition is predefined and store in the header file. To use built-in function, C/C++ programmers have to know only the function name and functionality and the header file of that function. Do Google and find required function name and functionality and the header file that function. Now you are able to use that function to your program. Some built-in functions those are frequently used in C/C++ programming are given below.

printf(), cprintf(), puts(), putchar(), scanf(), gets(), getchar(), getch(), getche(), clrscr(), abs(), sqrt(), pow(), pow10() ,log(), log10(), sin(), asin(), sinh(), cos(), acos(), cosh() , tan(), atan(), tanh(), rand(), arand(), randomize()