Previous | Home | Next |
# Defining a Function
Following is the source code for a function called add(). This function takes two parameters a and b and returns the addition of them:
/* Simple value addition program*/ int add( int a, int b) { int c;/* local variable declaration */ c = a + b; return c; }
* Return type is int, Function name is add and Parameters are a, b is also called Argument for a function.
# Function Declarations
Actual body of the function can be defined separately as show above. The function declaration tells the compiler about function name and how to call the function.
return_type function_name( parameter list );
For the above defined function add(), following is the function declaration:
int add( int a, int b); /* Parameter names are not important in function declaration only their type is required, so following is also valid declaration: */ int add( int, int);
# Calling a Function
To call a function, you simply need to pass the required parameters along with function name, and if function returns a value, then you can store returned value.
For example:#include <stdio.h> #include <conio.h> /* function declaration */ int addition( int, int); main() { int i=1; /* local variable definition */ i = add(1, 1); clrscr(); } /* Simple value addition program*/ int add( int a, int b) { int c; /* local variable declaration */ c = a + b; return c; }
Output : 2
Functions that return multiple values using Array
Previous | Home | Next |