Functions and Parameters C++ - Reading Assignment

What is a function parameter?
A function parameter is a connection between the function variables and the entry values for the function.

Why do functions need parameters?
If we didn’t have parameters we would need to modify each function every time we would like to change a value. Since we can define all values with letters, we can write the code and it will work with all the inputs the user desires.

1 Like

1 - A function parameter is one or more inputs to a function (input data to be operated on)

2 - Functions need parameters as values are likely not to be known in advance. Also, generic calculation functions do not store input values (or they aren’t generic/reusable)

1 Like

1- A function parameter is a variable used in a function. They are always initialized with a value provided by the caller of the function.

int add(int x, int y){} 

int x and int y are the parameters.

2- Function needs parameters, because when it is called all of the parameters of the function are created as variables. And the values of the each arguments directly copied into the matching parameter. So that function executes and returns the desired value.

1 Like
  1. A function parameter - Information can be passed to functions as a parameter. Parameters act as variables inside the function. Parameters are specified after the function name, inside the parentheses.

  2. The purpose of parameters is to allow passing arguments to the function from the location where it is called from. - statements is the function’s body. It is a block of statements surrounded by braces { } that specify what the function actually does.

1 Like
  1. It is basically an input value for the function to work with.
  2. Because this way functions can use values from other functions or values from input, which lets us build more complex programs.
1 Like
  1. What is a function parameter?

A function parameter is a variable that is used in a function. The caller of the function can initialize the value of parameters upon calling the function by providing an argument for each parameter of the function. The value of the argument is then copied to the parameter and this value is then used when the function is executed.

  1. Why do functions need parameters?

Parameters provide a function with much more flexibility with regard to making the value of specific values of a function flexible. Variables that are defined as parameters do not have a fixed value, instead they can be modified by the function caller each time the function is called and this makes functions more effective and more “dynamic” in reusing them for the same task but with different input factors.

1 Like
  1. A function parameter is variable used in a function.
  2. They are used to give the function the values necessary to complete the task.
1 Like
  1. A function parameter is a variable used in a function.

  2. To pass information to the function being called so it knows which values to work with.

1 Like

FORUM QUESTIONS

  1. What is a function parameter?
    A function parameter is a placeholder value which is used within functions to perform instructions on an arbitrary level. When the program calls the function, the parameter/s will be defined and the function will perform with regards to the set parameters at the point where it is called.

  2. Why do functions need parameters?
    Functions need parameters in the interest of scalability. The beauty of functions is their ability to be repeated as many times as required. A function will require parameters if it is to take in executable data from the program. A function does not always need parameters, however if it is to use values and data from the program, parameters are important in defining a function.

ARTICLE QUESTIONS

  1. What’s wrong with this program fragment?
    The multiply function type is defined as void, and therefore can’t return a value back to main() when it is being called. It should be returning an int, and so the computer will spit out a compiler error.

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

    • The first problem is that the function is initialized with 2 function parameters, and only 1 function parameter has been called in the program.
    • The second problem is that the multiply function doesn’t have a return statement, and therefore a value can’t be passed back to the program where is has been called.
    • It’s also worth noting that the product function has been called inside of the multiply function, without being initialized.
  3. What value does the following program print?
    The function gets reduced to std::cout << multiply(6, 4) << '\n';
    And therefore the program prints 24

  4. Write a function called doubleNumber() that takes one integer parameter. The function should return double the value of the parameter.

int doubleNumber(int num)
{
return num * 2;
}
  1. Write a complete program that reads an integer from the user, doubles it using the doubleNumber() function you wrote in the previous quiz, and then prints the doubled value out to the console.
#include <iostream>

int getUserNumber()
{
std::cout << "Enter your number: ";
int input{};
std::cin >> input;  
return input;
}

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

int main()
{
std::cout << doubleNumber(getUserNumber) << '\n';
return 0;
}
1 Like
  1. What is a function parameter?
    Variable used in a function. Always initialized with a value provided by the caller of the function.

  2. Why do functions need parameters?
    It is useful to be able to pass information to a function being called, so that the function has data to work with.

1 Like
  1. What is a function parameter?

Function parameters are variables used in a function. These particular variables are always initialized with a value provided by the caller of the function.

  1. Why do functions need parameters?

Function parameters are used to pass information to a function.

1 Like

Double should square the number instead of multiply it by 2. :slight_smile:

Hey mate, thanks for auditing me! :slight_smile:
Why exactly should double square the number? Am I missing something?
Assuming you’re referring to Article Question #4?

Cheers!

Actually I was wrong, sorry :sweat_smile:

Haha all good :+1: I spent longer than I should have checking to see where I was wrong… :skull_and_crossbones: Thanks anyway!

  1. values passed to the function, variables are assigned for use in the function in the order they are passed from the calling function and of the type defined in the signature.
  2. so the function has input variables to work on
1 Like
  1. A function parameter is a variable used in a function.
  2. Functions have parameters so they can have data to work on, though parameters are not always neccesary in functions.
1 Like

1. What is a function parameter?
A function parameter is an input to the function. A function can take multiple parameters or inputs.

2. Why do functions need parameters?
Not all functions need parameters, but some functions do because they need data to operate on to perform their intended task.
For example, if a function is designed to multiply any two numbers together then it needs two inputs so that the user of the function can call the function and provide any two numbers.

1 Like

Questions:
. Function parameters are variables that are used in functions, which are initialized with a value provided by the caller.
. Parameters are not essential but they may provide additional variables.

1 Like
  1. A function parameter is a variable used in a function. They are initialized with a value given by the caller of the function.

  2. Sometimes functions need to be given some kind of input data to work with.

1 Like