Functions and Parameters C++ - Reading Assignment

What is a function parameter?

A function parameter is a parameter between brackets after the identifier and is provided by the caller of a function.
If there is more than one parameter they are seperated by a comma.

Why do functions need parameters?

They allow us to create functions that take data as input, do some calculation with it, and return the value to the caller.

1) What’s wrong with this program fragment?

Void functions cannot return anything.

2) What two things are wrong with this program fragment?

Nothing is returned from the function.
Only one parameter is used in main()

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.

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

5) 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.

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

int main()
{
int x;
std::cin >> x;
std::cout << doubleNumber(x) << std::endl;
return 0;
}

1 Like

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. Function parameters are placed in between the parenthesis after the function identifier, with multiple parameters being separated by commas.

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.

2 Likes
  1. What is a function parameter?
    Function parameters are values sent when function is called
  2. Why do functions need parameters?
    is a way to dynamically interact with them
1 Like
  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. Function parameters are placed in between the parenthesis after the function identifier

  2. Why do functions need parameters?
    Parameters provide a means for the caller to pass information to the function. This allows us to pass different arguments to a function each time it is called.

Quiz

  1. The function multiply has a return type of void which tells the compiler that the function does not return a value but multiply does return a value at line 5: return (x + y). I expect this to cause a compiler error. In any event the function will not return a value when it is called.

  2. The function multiply does not have a return statement and when main calls the function multiply it needs to include 2 arguments but only included 1 argument.

  3. 24

int doubleNumber(int x) {

return 2*x;

}

#include

using namespace std;

int doubleNumber(int x) {

return 2*x;

}

int main()
{
std::cout << “Enter a number:” << std::endl;
int a;
std::cin >> a;
std::cout << doubleNumber(a) << endl;
return 0;
}

1 Like

Not sure how to change my post but I entered #include but only #include is showing up in the post

1 Like

it appears that anything in angle brackets does not display on I had #include ‘angle bracket’ iostream ‘angle bracket’

1 Like

1. What is a function parameter?
It’s a variable that is used in a function that is called on by the caller of the function. It performs specific actions within the function.
2. Why do functions need parameters?
They’re there to tell the caller what the specific task the function is to perform.

1 Like
  1. A function parameter is specific data that a user can enter into a function. In the instance of int add(int x, int y): x and y are the parameters. It allows for re usability and greater flexibility.

  2. Functions need parameters to give them more re usability and flexibility. It allows the function to do a wider variety of applications. It both gives depth to the complexity of the what the function can do and also adds simplicity for other instances.

1 Like

1. What is a function parameter?

A function parameter is data that has been passed into the function for use by the function. E.g. a function call: add(2) is passing 2 as a parameter to the function.

2. Why do functions need parameters?

Functions can run without parameters however it is often useful to be able to pass data into a function.

Quiz Answers

1. The function void multiply() has a return value but is defined as a void.
2. i. The int multiply() function does not have a return value, yet it is defined as an int function
2. ii. Only one argument is passed to the function when called, the function required 2 values to be passed as arguments.
3. 24 is printed to the screen.
4. int doubleNumber(int x){
return x * 2; }

5.<-- #include iostream //(chevrons ommited)

using namespace std;

int doubleNumber(int y){
return y * 2;
}

int main()
{
int x;
cout << "Enter a number: ";
cin >> x;
cout << doubleNumber(x) <<endl;

return 0;

}

1 Like

1- A parameter is a value that can be assigned by the function’s caller prior to executing that function
2- Parameters make functions useful as reusable snippets of code. If we weren’t able to input parameters in the form of arguments, there would be very limited use for them.

1 Like
    1. What is 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.
    1. Why do functions need parameters?
      So the function has input variables to work on
1 Like
  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. Function parameters are placed in between the parenthesis after the function identifier, with multiple parameters being separated by commas.

  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. Those input values are passed in as arguments by the caller.

1 Like
  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. Function parameters must be placed in between the parenthesis after the function identifer.
  2. Why do functions need parameters?

    • Parameters are the way in 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.
1 Like
  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. You have to be able to pass data through
1 Like

1.A function parameter is a variable used in a function when the value is provided by the caller of the function. function parameters are placed in the parenthesis after the function identifier, with multiple parameter being separated by commas.
2. Parameters are the key mechanisms by which functions can be written in a reusable way, as it allows them to perform tasks without knowing the specific values ahead of time.

1 Like
  1. What is a function parameter?
    When you call a function you can give it parameters between the parenthesis. These parameters can be used in the function.

  2. Why do functions need parameters?
    If function are to be used values in a program, they must be passed onto the function as arguments. These arguments are assigned to the parameters so they can be used as variables within the scope of the function.

1 Like

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.
1 Like

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. Function parameters are placed in between the parenthesis after the function identifier, with multiple parameters being separated by commas.

2. Why do functions need parameters?

parameters are necessary to in order to be able to call a function with different values without having to rewrtie the entire function again.

1 Like
  1. One or more variable that are passed from caller function to called function. They are separated by commas
  2. It is the way a caller function sends variables to called function
1 Like

. What is a function parameter?
A function parameter is a variable that contains a value called an argument. The value is passed by the caller of the function, and with this value, the function has the information it needs to perform its task.

2. Why do functions need parameters?
Must functions are like formulas. To find out the result of the formula, it is crucial to know the values of the variables in the formula.

1 Like