A function is a group of commands that go together to perform a task. Each C / C ++ program has at least one function, main () , and all most normal programs define additional functions.

You can break your code into separate functions. The way you divide your code into different functions depends on you, but logically, a function usually has a certain task.

A function declaration informs the compiler about the name of the function, the return type and the parameter. A function definition provides the body of a function.

The standard libraries of C / C ++ language provide many functions available for your program to call. For example, strcat () can concatenate two strings, memcpy () uses to copy a memory to another memory area and many other functions.

A function is known for different names such as a method, a sub-route, or a procedure.

Defining a function in C / C ++

The general pattern of the function definition in C / C ++ Language is as follows:

 Kieu_tra_ve Ten_ham ( Danh sach tham so ) { Than ham } 

A function definition in C / C ++ language consists of a function head and a function body. Here are the parts of a function:

Return type : A function can return a value. Kieu_tra_ve is the data form of the value returned by the function. Some functions provide operations and do not return any values. That is the void function.

Function name : This is the actual name of the function. Function name and parameter list make up the function sign.

Parameter list : When the function is called, you must pass the parameter list. A value towards an actual parameter. The parameter list has the type, order, and number of function parameters. The parameters in the function are optional, meaning a function may have no parameters.

Function body : The body of a function includes a set of commands that determine what the function does.

For example

The following code for a function called max () . This function has 2 parameters: so1 and so2 and returns the largest value between functions:

 // ham tra ve so lon nhat cua hai so int max ( int so1 , int so2 ) { // Khai bao bien cuc bo int result ; if ( so1 > so2 ) result = so1 ; else result = so2 ; return result ; } 

Declare the function in C / C ++

A function declaration informs the compiler of the function name and function call. The body function can be defined in a discrete way.

A function declaration has the following sections:

 kieu_tra_ve ten_ham ( danh sach tham so ); 

For example, when defining the max () function, here is the function declaration:

 int max ( int so1 , int so2 ); 

The parameter names are not important in the function declaration, and the type below is a valid declaration:

 int max ( int , int ); 

A function declaration is required when you define a function and source and when calling a function from another source file. In this case, you should declare the function before calling that function.

Call the function in C / C ++

While creating a function, you define what the function must do. To use a function, you must call that function to perform a specific task.

When a program calls a function, the control part is passed to the called function. A called function performs defined tasks and returns the value after executing the program.

To call a function, you simply need to pass the required parameters along with the function's name and if the function returns the values, you can reserve these return values, for example:

 #include using namespace std ; // khai bao ham int max ( int so1 , int so2 ); int main () { // Khai bao bien cuc bo: int a = 100 ; int b = 200 ; int ketqua ; // goi ham de tim gia tri lon nhat. ketqua = max ( a , b ); cout << "Gia tri lon nhat la: " << ketqua << endl ; return 0 ; } // ham tra ve so lon nhat cua hai so int max ( int so1 , int so2 ) { // Khai bao bien cuc bo int result ; if ( so1 > so2 ) result = so1 ; else result = so2 ; return result ; } 

I keep the max () function value in the main function into the ketqua variable. When running the above C / C ++ program, the following results are available:

 Gia tri lon nhat la : 200 

Parameters of functions in C / C ++:

A function that uses parameter lists, it must declare variables and accept the values ​​of these variables. These variables are called official variables.

Official variables are like other local variables inside the function.

When you call the function, there are 2 ways to pass the values ​​to the function:

Call type Description

Call the function by value in C / C ++

This method copies the actual value of the parameter into the official parameter of a function. In this case, the changes of the parameters themselves within the function do not affect the parameters.

Call the function by pointer in C / C ++

This method copies the address of the parameter into the official variable. Within this function, this address is used to access the actual parameter used in the function call.

Call the function by reference in C / C ++

This method copies the address of the parameter into the official parameter. Inside the function, the address used to access the parameter is actually used when calling the function. This means that changes to parameters make the parameter change.

By default, C / C ++ uses calling by value to pass parameters. In general, that code in a function cannot change the parameters used to call that function and in the example above, when calling the max () function, use the same method.

Default value for parameters in C / C ++

When you define a function, you can specify a default value for each final parameter. This value will be used if the corresponding parameter is left blank when calling that function.

This is done using the assignment operator and assigning values ​​to the parameters in the function definition. If a value for that parameter is not passed when the function is called, the provided default value is used, but if a value has been specified, the default value is ignored and, instead, the passed value is used. You follow the following example:

 #include using namespace std ; int sum ( int a , int b = 20 ) { int ketqua ; ketqua = a + b ; return ( ketqua ); } int main () { // Khai bao bien cuc bo: int a = 100 ; int b = 200 ; int ketqua ; // goi ham de tinh tong hai so. ketqua = sum ( a , b ); cout << "Tong gia tri la: " << ketqua << endl ; // goi ham mot lan nua. ketqua = sum ( a ); cout << "Tong gia tri la: " << ketqua << endl ; return 0 ; } 

Running the above C / C ++ program will produce the following results:

 Tong gia tri la : 300 Tong gia tri la : 120 

According to Tutorialspoint

Previous article: Control flow in C ++

Next lesson: Number in C ++

4 ★ | 1 Vote | 👨 162 Views

Above is an article about: "Function in C / C ++". Hope this article is useful to you. Don't forget to rate the article, like and share this article with your friends and relatives. Good luck!

« PREV POST
NEXT POST »