A Program to understand:-


(a) the concept of user defined function.


# its declaration, definition, calling

#include<iostream.h>
#include<conio.h>

void sum();
int a=3,b=7,c;
void sum()
{
c=a+b;
cout<<"Sum:"<<c;
}
void main()
{
clrscr();
sum();
getch();
}

Output:


Sum:10

			


1. Function Declaration:-


You have seen that I have written the same program in two ways, in the first program I didn’t have any function declaration and in the second program I have function declaration at the beginning of the program. The thing is that when you define the function before the main() function in your program then you don’t need to do function declaration but if you are writing your function after the main() function like we did in the second program then you need to declare the function first, else you will get compilation error.



2. User-defined functions:-


We have already seen user-defined functions, the example we have given at the beginning of this tutorial is an example of user-defined function. The functions that we declare and write in our programs are user-defined functions. Lets see another example of user-defined functions.

Previous Next