Pages

Friday, September 29, 2017

Easy tutorial for C / C ++ Function 2 (Example)

Easy tutorial for C / C ++ Function 2 (Example)

Example

- In the previous chapter, I created a function that has no arguments and no return value. Let's look at a simple example with functions that have arguments and return values.

Example Code

#include <iostream>
using namespace std;

void EXf1();
void EXf2(int);
int  EXf3(int, int);

int main() {
     int n1, n2, n3 ;
     EXf1();
     cout << "Enter a natural number between 0 and 10. : ";
     
     cin  >> n1 ;
     EXf2(n1);
     
     n2 = 10 ;
     n3 = EXf3(n1, n2);
     cout << n1 <<" + "<< n2 <<" = "<< n3 << endl;
     
     return 0;
}

void EXf1(){
cout << " No return values and parameters. " << endl;
}

void EXf2(int a){
            if(a > 3){
             cout<< a << " > 3 is true. "<<endl;
           }else{
             cout<< a << " <= 3 is true. "<<endl;
           }
}

int EXf3(int a, int b){
     int c ;
     c = a + b ;
     return c ;
}

- Three types of functions were declared  (prototype declaration).

(1) void EXf1 (): Function without arguments and return values.

(2) void EXf2 (int): function with one integer argument and no return value.

(3) int EXf3 (int, int): A function with integer type of return value and two integer arguments.

- The above functions were defined after the main function.

(1) The function 'EXf1 ()' is a function that prints the statement "No parameters and return values."

(2) The function 'EXf2' is a function that performs an if-else statement with one integer argument 'a'.

If the value of a received as a parameter is greater than 3, a statement that the value of 'a' is greater than 3 is printed. Otherwise, a statement that is less than or equal to 3 is printed.

(3) The function 'EXf3' is a function that has two integer arguments and whose return type is an integer type. This function stores the sum of two values 'a' and 'b' as arguments into the integer variable 'c' and returns 'c'.

- Check the main function as follows. First, call the EXf1 function. Simple output is expected.

- Integer variables 'n1', 'n2' and 'n3' were declared. Then assigned the value entered on the keyboard to 'n1'. 

- Then called the function 'EXf2' and passed 'n1' as an argument. You can expect different results depending on the number you enter.

- Assigned 10 to 'n2' and substituted the return value of function 'EXf3' for 'n3'. When calling function 'EXf3', it passes 'n1' and 'n2' as arguments. Then print a sentence that represents the sum of the two digits.

results:

No return values and parameters.
Enter a natural number between 0 and 10. :

- As a result of function 'EXf1', the statement "No parameters and return values." was printed. And a statement to input a natural number between 0 and 10 was also printed. Type 5 here and press Enter.

No return values and parameters.
Enter a natural number between 0 and 10. : 5
5 > 3 is true.
5 + 10 = 15

- We can see the sentence "5 > 3 is true.". And we can see that the 5 + 10 is 15.

No comments:

Post a Comment