In this video i will explain what are the function or method in c++
Functions / Methods in C++
A function is a block of code that performs a specific task. A function only runs when it is called.
Dividing a complex problem into smaller chunks makes our program easy to understand and reusable.
Functions are used to perform certain actions, and they are important for reusing code: Define the code once, and use it many times.
We can have any number of functions in a program.
There are two types of function:
• Standard Library Functions: Predefined in C++ in header files.
• User-defined Function: Created by users as per need.
Right now, we will focus on user defined functions.
C++ User-defined Function
C++ allows the programmer to define their own function and use the function when needed.
When the function is invoked from any part of the program, it executes the code defined in the body of the function.
Every user defined function have three parts:
a. Function declaration
b. Function definition
c. Function call
Function Declaration
The function declaration have following syntax:
Returntype functionname(Datatypeofparameter);
Note: The function declaration is terminated by semicolon. The declaration can appear inside other function.
Function definition
The function definition has following syntax:
Returntype functionname(datatype parametername)
{ //code goes here
Return value;
}
The definition of function must not appear inside any other function.
Function call
The function call is used to activate the function definition. The function call looks like as follows:
Functionname(argumentifany);
The function is always called from another function.
Depending on the type, a function can take argument or can return value using return keyword. We will discuss them later.
We can create user defined function in following four ways:
1. Function without argument, without return value
2. Function with argument, without return value
3. Function without argument, with return value
4. Function with argument, with return value
Function without argument, without return value
It is the simplest type of UDF. In this type of function all input, calculation and output is done in function body(definition).
The declaration of this type of function looks like as follows:
void functionname(void);
The definition of this type of function looks like as follows:
void functionname(void)
{
//code goes here
}
The call of this type of function looks as follows:
functionname();
WAP to create a function which prints sum of two numbers.
www.tarunsir.com
www.cinstitute.org.in
Connect me on linkedin
www.linkedin.com/in/tarrunverrma
Follow on instagram
www.instagram.com/the_ultimate_coding_stuff/
Follow on twitter
twitter.com/Tarrunverrma
#tarunsir #cpptutorial #learnprogramming #coding #function #udf
コメント