Functions in C++ programming

Spread the love

In C ++ programming Functions is a group of statements. There is a function in every program. for eg. main () function is written in the program. Function calls are made where the function is needed.

INDEX –

  1. Types of function in c++ programming
  2. In-built / Predefined Function
  3. User-defined Function
  4. return_type
  5. function_name
  6. parameter (s) / argument (s)
  7. semicolon
  8. Function Definition
  9. return_type
  10. function_name
  11. parameter(s)/argument(s)
  12. function_body
  13. Function Calling
  14. Call By Value 
  15. Call By Reference
  16. Conclusion
  17. Frequently asked questions

A Function has a specific name. At the beginning of the function, its name is followed by two parentheses () and the statements of the function are two curly braces {}.

The code written in the function does not have to be written again and again.

The Function Programmer saves time and space for the program.

Larger programs can be divided into smaller functions.

there is an error in the program, can be easily removed.

The function can be called again and again where needed.

Two types of function in C++ programming

1.In-built / Predefined Function –

In-built Functions are called predefined or Library Functions.

In-built Functions, separate header file or preprocessor is created for each of the functions.

The declaration of a function occurs in the definition header files.

C ++ has a lot of header files and has different functions grouped into them.

If the programmer wishes, he can create his header files.

Example- cout:- This Object iostream.h comes under this header file. If the programmer #include <iosstream.h> does not include the preprocessor program, then cout cannot be used.

strlen ():- This function string.h comes under this header file. If the programmer #include <string.h> does not include it in the preprocessor program, then strlen cannot be used.

2. User-defined Function –

User define function is a function that is defined by the user.

A user-defined function has three types

1. Function Declaration

2. Function Definition

3. Function Calling

1. Function Declaration –

creating a function, the compiler has to tell what kind of function you want to create, this process is called Function Declaration.

The declaration, called the FUNCTION PROTOTYPE, informs the compiler about the functions to be used in a program, the argument they take, and the type of value they return.

Syntax for Function Declaration-

return_type function_name (parameter (s));

Function Declaration has four departments.

return_type

function_name

parameter (s) / argument (s)

semicolon

1. return_type –

Each function returns a value, be it null (void) or numeric (int, float, double) or character (char). The return_type of the function is void default. The return_type of the function is in the requirement of the program.

2. function_name –

The name of the function should be according to its code. Even there is no, the compiler runs, it is not called Good Programming. The name of the function is not a keyword of C ++. There are parameters inside the parenthesis () of the function of C ++. The function name is case-sensitive. for eg. hello () and hello () are both different functions.

3. parameter(s)/argument(s ) –

Function arguments are data_types or names of their variables with the data type. What type of value a user wants to receive through function calling, it is declared in the argument of the function.

4. Semicolon –

After the Declaration of every function, a semicolon is given part of the function declaration.

2. Function Definition –

The function definition tells the compiler what task the function will be performing. The function prototype and the function definition must be the same on the return type, the name, and the parameters.

The only difference between the function prototype and the function header is a semicolon.

The function definition consists of the function header and its body. The header is EXACTLY like the function prototype, EXCEPT that it contains NO terminating semicolon.

 Syntax for Functionn Definition

return_type function_name(Parameter(s)){

   function_body;

}

There are four parts to a function definition

 return_type

function_name

parameter(s)/argument(s)

function_body

1. return_type –

Each function returns a value, be it null (void) or numeric (int, float, double) or character (char). The return_type of the function is void default. The return_type of the function is in the requirement of the program.

2. function_name –

The name of the function should be according to its code. Even there is no, the compiler runs, it is not called Good Programming. The name of the function is not a keyword of C ++. There are parameters inside the parenthesis () of the function of C ++. The function name is case-sensitive. for eg. hello () and hello () are both different functions.

3. parameter(s)/argument(s) –

Function arguments are data_types or names of their variables with a data type. What type of value a user wants to receive through function calling, it is declared in the argument of the function.

4. function_body –

The Function body has variables, to be inside the function, their scope is Local. There is some statement (s) inside the body and the value returns.

3. Function Calling –

Syntax for Function Calling

function_name(Parameter1 ,Parameter2 ,.Parameter n);

Function calling contains only the function name, arguments, and semicolon of the function. Below given example function’s return type is ‘integer’. The name of the function is ‘add’ and the function is passed with two parameters meaning a and b. The declaration and definition of the function are of no importance the function is called.

There are two types of Function Calling

1. Call By Value 

2.  Call By Reference

1. Call By Value –

In Call By Value, the value of Variable is passed to the function as a parameter.

the value of the actual parameter is copied to the Formal Parameter.

In Call by value method, the called function creates its own copies of original values sent to it. Any changes, that are made, occur on the function’s copy of values, and are not reflected.

Example –

#include <iostream>
using namespace std;
 
void swap(int x, int y);
 
int main(){
 
int a = 99, b = 77;
      cout<<“Before Swapping  a = “<<a<<” and b “<<b<<endl;
      swap(a, b);
}
void swap(int x, int y)
{
int temp;
      temp = x;
      x = y;
      y = temp;
      cout<<“After Swapping  a = “<<x<<” and b “<<y;
}

 
note- this program run in C++ IDE compiler

Output –

Here ‘a’ and ‘b’ are copied to the values ​​’x’ and ‘y’ variable.

read also what is tokens?

2. Call By Reference –

In Call By Reference, the address of Variable is passed to the function as a parameter.

the value of the actual parameter is not copied to the Formal Parameter.

In Call by reference method, the called function accesses and works with the original value using their references.

Any changes, that occur, take place on the original values are reflected in the calling code.

Consider the program will swap the value of two variables.

It only holds the address hold of the variables.

Example –

#include <iostream>
using namespace std;
 
void swap(int *x, int *y);
 
int main(){
 
int a = 42, b = 67;
      cout<<“Before Swapping  a = “<<a<<” and b “<<b<<endl;
      swap(&a, &b);
}
void swap(int *x, int *y)
{
int temp;
      temp = *x;
      *x = *y;
      *y = temp;
      cout<<“After Swapping  a = “<<*x<<” and b “<<*y;
}

note- this program run in C++ IDE compiler

Output –

Here ‘a’ and ‘b’ are not copied to the values ​​’x’ and ‘y’ variable.

It only holds the address hold of the variables.

source – beginnersbook

Difference between call by value and call by reference in c++

Call by valueCall by reference
While calling a function, when you pass values by copying variables, it is known as “Call By Values.”While calling a function, in programming language instead of copying the values of variables, the address of the variables is used it is known as “Call By References.
In this method, a copy of the variable is passed.In this method, a variable itself is passed.
Changes made in a copy of the variable never modify the value of variable outside the function.Change in the variable also affects the value of the variable outside the function.
It Does not allow you to make any changes to the actual variables.It Allows you to make changes in the values of variables by using function calls.
The Values of variables are passed using a straightforward method.Pointer variables are required to store the address of variables.
Original value not modified.The original value is modified.
Actual and formal arguments will be created in different memory locationarguments will be created in the same memory location
Actual arguments remain safe as they cannot be modified accidentally.arguments are not Safe. They can be accidentally modified, so you need to handle arguments operations carefully.
Default in many programming languages like C++.PHP. Visual Basic NET, and C#.It is supported by most programming languages like JAVA, but not as default.

Conclusion –

Types of function -Predefined Function, User-defined Function, and these types all of them Syntax, examples, outputs I gave you above.

Friends, reading this post completely, you must have understood functions in c++ programming. you must have liked it. I try to describe to you in simple language, maybe you must have understood it in this post. I have covered Topics you do not need to read any other post. If you liked this article, then definitely share it with your friends. And there is some doubt. Then comment I will definitely answer your comment.

Frequently asked questions

1. What is the function?

A function is a group of statements that together perform a task.

2. What are the Default Parameters?

Default Parameter is a value assigned to each parameter while declaring a function.

3. What is the Call By Value?

While calling a function, when you pass values by copying variables, it is known as “Call By Values.”

4. What is Call By Reference?

While calling a function, in programming language instead of copying the values of variables, the address of the variables is used it is known as “Call By References.

Leave a Comment