Functions in C++
A function can be defined as a set of statement used to achieve a particular task. though we know what it means lets understand what can we use a function for.
Why do you need functions ?
Suppose you are running in a marathon at a point of time during the run you become thirsty and you stop running, take a sip of water and then start running again .
similarly in c++when a program is being executed it is executed sequentially i.e line by line , now when compiler encounters a function call, it pauses the execution (similar to stops running )at that point runs the new function (like drinking water )and when it is complete ,it returns to is original state (starts running again).
similarly in c++when a program is being executed it is executed sequentially i.e line by line , now when compiler encounters a function call, it pauses the execution (similar to stops running )at that point runs the new function (like drinking water )and when it is complete ,it returns to is original state (starts running again).
Declaration
when we write a variable we write int x , where x is the name of the variable and int is its data type , declaration of a function is something similar first we declare what will the data type of the return (to be covered shortly) – in this case int , and second is whatever name we want to give to the function.
Syntax of declaration:
datatype variablename(arguments);
int myfunction(int n);
This prototype specifies that in this program, there is a function named "myfunction" which takes a single integer argument "n" and returns an integer. Elsewhere in the program a function definition must be provided if one wishes to use this function. It's important to be aware that a declaration of a function does not need to include any arguments. The following is a argument-less function declaration, which just declares the function name and its return type, but doesn't tell what parameter types the definition expects.
double myfunction();
Defining a Function:
Syntax of defining:
int myfunction(int n)
{
Function Body
}
Calling a function:
Syntax of calling:
myfunction(int n);
{
Function method
}
Example of a function:
// FunctionFactorial.cc // Program to calculate factorial of a number with function call #include <iostream> using namespace std; // Function declaration (prototype) int Factorial(int M); int main() { int number=0, result; // User input must be an integer number between 1 and 10 while(number<1 || number>10) { cout << "Integer number = "; cin >> number; } // Function call and assignment of return value to result result = Factorial(number); //Output result cout << "Factorial = " << result << endl; return 0; }Program to read two numbers from the keyboard and print the sum using a function
I think it would be enough for the basic understanding of the functions.#include <iostream> using namespace std; int sum(int a, int b); //declare the function int main() { int a = 0, b = 0; cout << "Enter a number "; cin >> a; cout << "Enter another number "; cin >> b; int c = sum(a, b); //call the sum function cout << "sum = " << c << endl; system("pause"); } int sum(int a, int b){ return a + b; }
No comments:
Post a Comment