Functions and Parameters C++ - Reading Assignment


#21

1. What is a function parameter?
Function parameters are variables whose values are passed to the function by the caller.

2. Why do functions need parameters?
Parameters are the key mechanism by which functions can be written in a reusable way, as it allows them to perform tasks without knowing the specific input values ahead of time.


#22
  1. A parameter is a variable with a value that is defined by the caller.
  2. They allow functions to perform tasks without know the input values beforehand.

#23

1. What is a function parameter? A function parameter is a variable used in a function where the value is provided by the caller of the function.

2. Why do functions need parameters? Parameters allow functions to perform tasks without knowing the specific input values ahead of time, increasing the likelihood that the function can be reused.


#24

1. What is a function parameter?
It is a variable used in a function where the value is provided by the caller of the function.
2. Why do functions need parameters?
Because most of the time functions work with variables specified in the runtime of the program were functions are called.
Quiz

  1. It doesn’t compile because the multiply function must return an integer and it is defined as void. Furthermore, in main() cout must print a value and not a void.
  2. multiply function doesn’t return any value so there is nothing to print in main(). In main() multiply call need two arguments.
  3. Output 24.
  4. int doubleNumber(int x)
    {
     return 2*x;
    }
    
  5. #include <iostream>
    int doubleNumber(int x);
    
    main()
    {	
        int num;
    
        std::cout << "Type an integer number: " ;
        std::cin >> num;
        std::cout << "The double of " << num << " is : " << doubleNumber(num);  
        
        return 0;
    }
    
    int doubleNumber(int x)
    {
        return 2*x;
    }

#25

What is a function parameter?
- A function parameter is a variable that the function uses and is provided by the caller of the function.
Why do functions need parameters?
- Without parameters the function does not have data to work with.


#26

What is a function parameter?
A function parameter is a variable used in a function where the value is provided by the caller of the function.
Why do functions need parameters?
Parameters are the key mechanism by which functions can be written in a reusable way, as it allows them to perform tasks without knowing the specific input values ahead of time. Those input values are passed in as arguments by the caller.


#27

Functions & Parameters in C++

  1. A function Parameter is a variable used inside a function where the value is given by the caller
    ex.: add(parameter1, parameter2)

  2. To give arguments from the caller and execute some code with those values inside the function. The proces that gives arguments of the caller to the parameters of the functions, =Pass By Value


#28
  1. A function parameter is a variable used in a function where the value is provided by the caller of the function.
  2. Functions need parameters to pass information to a function being called as arguments so the function has data to work with.

#29
  1. They are values that the function will use.
  2. So that they can use them to perform calculations or task in a specific way.

#30
  1. What is a function parameter?

A function parameter is a variable passed to a function.

  1. Why do functions need parameters?

Parameters let functions work with outside data passed on to them.


#31
  1. What is a function parameter?
    

Variables that are defined in a function. You can then insert values of the pre-defined data type into the function when you call it.

  1. Why do functions need parameters?
    

because sometimes you want to pass data into a function and have the function manipulate it and spit something back out.


READING QUZ

  1. What’s wrong with this program fragment?
    multiply() has a void return type but it should be an int

  2. What two things are wrong with this program fragment?
    multiply() does not return product
    or
    product should be defined outside the function and the function should have a return value of void rather than int

  3. What value does the following program print?
    24

  4. Write a function called doubleNumber() that takes one integer parameter and returns twice the value passed in.

double doubleNumber(int x){
return x * 2;
}

  1. Write a complete program that reads an integer from the user (using cin, discussed in lesson 1.3a – A first look at cout, cin, and endl), doubles it using the doubleNumber() function you wrote for question 4, and then prints the doubled value out to the console.
#include <iostream>

using namespace std;

double doubleNumber(int x){
     return x * 2;
}

int main() {

int inputInteger;

cin >> inputInteger;

cout << doubleNumber(inputInteger) << endl;

return 0;
}

#32
  1. A function parameter is a variable, placed between parenthesis, used in a function where the value comes from the caller of the function.
  2. Functions do need parameters so you can perform tasks without initially having to know the specific input values.

#33
  1. A function parameter is a variable used in a function where the value is provided by the caller of the function. Function parameters are placed in between the parenthesis after the function identifier, with multiple parameters being separated by commas.
  2. In many cases, functions need parameters so that they would have data to work with. Parameters are the key mechanism by which functions can be written in a reusable way.

Quiz:

  1. The multiply function is void, yet it has a return value. An error will prevail.
  2. int product is defined in multiply, but the function is given no return value.
    Furthermore, there is a missing parameter in the funciton call to multiply, and thus a compilation error will occur.
  3. The program prints 24
  4.  int doubleNumber(int x){
         return 2*x;
     }
    
  5.  #include <iostream>
     
     int doubleNumber(int x){
         return 2*x;
     }
    
     
     int main()
     {
         using namespace std;
         int x = 0;
         cout << "Please enter a number: ";
         cin >> x;
         cout << doubleNumber(x) << endl;
         return 0;
     }
    

#34

What is a function parameter?

Data that the function can receive from the caller

Why do functions need parameters?

To take decisions according to that values


#35

1. What is a function parameter?
A variable used in a function where the value is provided by the caller of the function.

2. Why do functions need parameters?
To give us a way to dynamically interact with them.


#36
  1. A function parameter is a variable between the parenthesis of the function which defines the type of the value passed.
  2. A function needs a parameter to take or use the argument given to it.

#37

What is a function parameter?
A function parameter is a variable used in a function where the value is provided by the caller of the function.

Why do functions need parameters?
so that the function has data to work with.


#38

1. What is a function parameter?
It’s a variable used in a function where the value is provided by the caller of the function.

2. Why do functions need parameters?
Because the functions don’t need know about the caller environment, the caller will pass the parameters value that are necessary for the functions do their work. Thus, with parameters, the functions are more reusable.


#39

What is a function parameter?
It is a variable needed to pass some value into the function from outside the function to let the function do something with the value or values.
Why do functions need parameters?
To pass values to a function so that the function can do something with it


#40

1. What is a function parameter?
Is a value passed to function by variable.
2. Why do functions need parameters?
To execute the specified logic - the logic might demand specified value to work properly.