function sum(a,b) { sum=a+b; } When you type sum(2,3), the function will return 5. When you type sum(10,5) the function will return 50. Functions help you minimize the work that you do. For example if you want to read an array, you can read it as usual. But what if you have to read 100 arrays? You write a function and call it for every array.
Basically function is not unique to C++. It is derived from C Language. Function: A piece of code (eg: to add 2 numbers, c = a + b which is defined as a block with a unique name for the block which can be used to call it multiple times for different values whenever required. Functions in C++ are of 2 types. 1) Pre defined functions (Library functions) 2) User defined Functions Pre Defined Functions: I hope you are aware of functions like clrscr(), getch(), getchar(), etc. These functions are already created and their definitions are stored into the headerfiles like conio.h, stdio.h, string.h. So you can re use it whenever you need it. User Defined Functions: These are functions that programmer writes for the usage in that particular program. While defining a new function, user has to declare it, define it and then call it whenever required. Function Declaration: It is just to inform the compiler that a function is written in the program with that function name which will receive the so and so arguments, will pass that type of data to the calling program. Returntype Function_name (datatype 1, datatype2,.......); Returntype ==> Valid C++ Data type Function_name ==> Name given by the user to that function datatype 1, datatype 2 ==> Argument list (A valid C++ data type) Eg: int sum (int, int); Note the semicolon at the end. Function Definition: In the function definition part, we will mention the actual process that has to be done in that function. (Compare with Clrscr, its function definition will contain code to clear the output screen) Returntype Function_name (datatype1 variable1, datatype2 variable2,.......) Returntype ==> Valid C++ Data type Function_name ==> Name given by the user to that function datatype 1, datatype 2 ==> Argument list (A valid C++ data type) variable1, variable2 ==> A valid C++ variable name (identifier) Eg: int sum (int x, int y) Note the semicolon is not there at the end. Function Call: This the part to use the function. Eg: C = sum(2,3); I hope you can understand the details mentioned. Incase you need any clarification, feel free to PM me.
A function (also called a procedure, subroutine, etc) is a place you go to do something then return to where you came from. Often, but not always, a function will either modify something or bring something back to where you came from. For example, a shopping trip is a function. You leave home, go to the store, and return home with something you bought. A second example is going to a cafe. You leave home, go to the cafe, eat, then return home with nothing, except maybe a stomach ache and an empty wallet. Functions in any computer language work the same way. You leave your current code, go to the function, do something, then maybe bring something back to the your original location in your current code.
functions: functions are to be used in large programs where a block of statements are used again and again. function reduces the complexity of the program. main() is also a function from which execution begins. and other functions like 1. no return value and no arguments void sum() { int a,b,sum; sum=a+b; cout<<sum; } 2.return value but no arguments int sum() { int a,b,sum; sum=s+b; return(sum); } 3.no return value but pass arguments void sum(int a,int b) { sum=a+b; cout<<sum; } 4.return value and return arguments int sum(int a,int b) { sum=a+b; return(sum); }
functions are a part of code that perform some task or some job in order to fulfill the requirements.