C Functions
C Functions
A function is defined as a group of statements or a block of code that carries out specific tasks.
Function - a subprogram that can act on data accepted as an input in the form of parameter, dose
some processing and return a value
Every C++ program has at least one function, main(), where program execution begins and
Terminates and it might contain more than one function.
Why Use Functions in C++?
Functions are used to minimize the repetition of code, as a function allows you to write the code
inside the block. And you can call that block whenever you need that code segment, rather than
writing the code repeatedly. It also helps in dividing the program into well-organized segments.
Functions in C++ come in two
varieties:
Functions could be:
1. Pre-defined(built-in) library functions: Function which is pre-defined in the library. E.g:
pow(), sqrt(), cin, sin, tan etc..
2. Programmer-defined functions: the function which is made by the user (e.g., my_printf,area)
Elements of Function
These functions require three elements:
◦ Function declaration: used to rate the function
◦ Function calls: Invoke the function to execute the function
◦ Function definition: In this program will defines that how the function will perform there task.
Function Syntax
Here, the return type is the data type of the value that the function will
return. Then there is the function name, followed by the parameters which
are not mandatory, which means a function may or may not contain
parameters.
Example:
Int Average(int x, int y, int z)
{
//function body
Declaration:
A function can be declared by writing its return type, the name of the function, and the
arguments inside brackets. It informs the compiler that this particular function is present. In C+
+, if you want to define the function after the main function, then you have to declare that
function first
Int avg(int s1,int s2,int s3);
Definition:
A function definition specifies the body of the function. The declaration and definition of a
function can be made together, but it should be done before calling it.
Int avg(int s1,int s2,int s3);
{
return(s1+s2+s3)/3;
}
Calling:
When you define a function, you tell the function what to do and to use that function; you have to
call or invoke the function. When a function is called from the main function, then the control of the
function is transferred to the function that is called. And then that function performs its task. When
the task is finished, it returns the control to the main function.
There are two types of functions in C++
Built-in functions
User-defined functions
Built-in Functions:
These are functions that are already present in C++; their definitions are already provided in the
header files. The compiler picks the definition from header files and uses them in the program.
User-defined Functions:
These are functions that a user creates by themselves, wherein the user gives their own
definition.
You can put them down as the following types:
No argument and no return value
No argument but the return value
Argument but no return value
Argument and return value
No argument and no return value: In this type, as the name suggests, it passes no arguments to
the function, and you print the output within the function. So, there is no return value.
No argument but return value: In this type, it passes no arguments to the function, but there is a
return value.
Argument but no return value: In this type of function, it passes arguments, but there is no
return value. And it prints the output within the function.
Argument and return value: In this type of function, it passes arguments, and there is also a
return value.
Function with no arguments and a return value: