Introduction to User Defined Functions
A function is a group of statements that together perform a task. Every C++ program has at least one function, which is main(), and all the most trivial programs can define additional functions.
We have study about #define in previous lessons. Functions is similar to #define but less secure than functions as it doesn't have any return type or data type of parameters which provide certain level of data security.
Functions usually defined because a certain snippet of code are used again and again. So instead of writing again and again we create a Function which stores the required statement to execute and it may return result according to return type.
Functions are used to provide modularity to a program. Creating an application using function makes it easier to understand, edit, check errors etc.
A function is also known as method or a sub-routine or procedure etc.
Syntax of Function
Example:
#include<iostream>
using namespace std;
void display(int x); //declaring the function
//define the function
int calculate(int x,int y) //int = data type
//calculate = function-name
//int x, int y = function parameter or signature
{
return x+y; //return integer type
}
int main()
{
int x = calculate(2,5);
display(x);
return 0; // https://cplspls.blogspot.com
}
void display(int x) //definition of function which is declared above
{
cout << x<<endl;
}
OUTPUT:
A function is a group of statements that together perform a task. Every C++ program has at least one function, which is main(), and all the most trivial programs can define additional functions.
We have study about #define in previous lessons. Functions is similar to #define but less secure than functions as it doesn't have any return type or data type of parameters which provide certain level of data security.
Functions usually defined because a certain snippet of code are used again and again. So instead of writing again and again we create a Function which stores the required statement to execute and it may return result according to return type.
Functions are used to provide modularity to a program. Creating an application using function makes it easier to understand, edit, check errors etc.
A function is also known as method or a sub-routine or procedure etc.
Syntax of Function
return-type function-name (parameters){
// function-body
}
A C++ function definition consists of a function header and a function body. Here are all the parts of a function:
- Return-Type: The return-type is the data type of the value the function returns. return-type can be of any data type or user defined type also. It can be void when we don't have to return anything.
- Function Name: The function-name is name of function through which we gonna call it.
- Parameters: A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters. Parameters are also known as Function Signature.
- Function Body: The function body contains a collection of statements that define what the function does.
Function Declaration includes
return-type function-name (parameters);
Function declaration is similar to function defination just without function body.
In C++ before calling function we must either define the function or declare the function. If we try to call function from main() and it is defined after the main() method then it will give compile error as compiler doesn't recognize the function as its not defined till now. So we need to tell compiler by declaring earlier or define the function.
Below example contain usage of both defining and declaring the function before main method.
#include<iostream>
using namespace std;
void display(int x); //declaring the function
//define the function
int calculate(int x,int y) //int = data type
//calculate = function-name
//int x, int y = function parameter or signature
{
return x+y; //return integer type
}
int main()
{
int x = calculate(2,5);
display(x);
return 0; // https://cplspls.blogspot.com
}
void display(int x) //definition of function which is declared above
{
cout << x<<endl;
}
OUTPUT:
0 comments:
Post a Comment