Functions and Parameters C++ - Reading Assignment

  1. A function parameter is a variable that can be changed within a function.

  2. functions need parameters to be versatile, it allows the function to be used in more places.

  1. The inputs that a function will use to work.
  2. To pass information through different processes.
  1. It is some value that can be given to the function when it is called.
void say(int a){
    std::cout<<a<<std::endl;
}

here β€˜int a’ is the parameter for say()

  1. Functions do not need parameters but it is useful for passing data that they can use in their processing.
1 Like

What is a function parameter?
value sent to a function for processing
Why do functions need parameters?
so you can pass references and values from main into them

What is a function parameter?
Are values that a function might need to compute its return value.
But not all functions need parameters.
For instance in:

void doPrint()
{
    std::cout << "In doPrint()" << std::endl;
} 

the function has no parameters.
In the function

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

has one parameter, namely, x.

Why do functions need parameters?

Not always need parameters, but I guess they usually do because need data to compute
their return value in order to pass it to its caller.

Hi everyone,

I have a couple of question about the quiz on parameters.

  1. For the function that asks the user for a number and returns its double I got
    (inside the main() function and with doubleNumber as a function declared beforehand)

     int a;
     cout<< "Give me a number " << (cin >> a) << endl;
     cout<< "The double of your number is "<< doubleNumber(a)<< endl;
    

my problems is that, for example when a=9, it returns:

Give me a number 9
1
The double of your number is 18

I am puzzled for the number 1 that appears.

Note: if I change the function so that cin is in a separate line the program runs well (no mysterious number 1).

  1. In this program, should one initialized int a=0 at the beginning or just int a is fine? For me the two options seem to work ok.

@tfrancisco
I think it might be a return value from (cin>>a) .
Have a look at these:

  1. A parameter is a variable that a function uses to complete its task. The caller of that function also needs to specify these parameters in order to make the right use of the function.
  2. Functions need parameters so that other parts of the program can make a specific use of the function using their parameters. For example, an addition function, one part of the program may want to add 10 and 2 while another part of the program needs to add 20 and 4.

Thanks Zaid. Yes, you are right, it is a return value. When I input an integer, the return is 1 (true); if I input a non integer value it returns 0 (false).

1 Like

1. What is a function parameter?

A function parameter is a variable used in a function in which the value is designated by the caller of the function.

2. Why do functions need parameters?

Functions need parameters because to utilize the values from the program, a parameter will provide the input for the function that is reusable to perform automated tasks.

What is a function parameter?
The parameter or parameters of a function is the values that the function take like input , do the function operations and return the value after that. A function can have or not parameters.

Why do functions need parameters?
when a function take parameters , the caller function pass this parameters like arguments and return the value . this parameters are inputs that the function need to execute the code and return a value like output.

  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?
    By using both parameters and a return value, we can create functions that take data as input, do some calculation with it, and return the value to the caller. 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. What is a function parameter?

A function parameter is a variable used in a function, whereby the value is given by the caller of the function.

2. Why do functions need parameters?

Functions need parameters so we can return the value to the caller.

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.
Quiz

  1. Will produce a compile error as multiply is defined as void and wont return a value, the function should return an int
  2. There are two issues a) only value is passed to the functin instead of two and b) there is no return statement with the variable.
  3. 24

#include

using namespace std;

// multiply user value supplied in function by two

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

int main()
{
std::cout << multiply(2) << std::endl;
return 0;
}

#include

using namespace std;

// multiply user value supplied by user by a factor of two

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

int main()
{
int number;

cout << "Enter a number " << endl;
std::cin >> number;


std::cout << multiply(number) << std::endl;
return 0;

}

1 Like
  1. It is a variable that are used in the function, it is written inside the paranthesis where you declare your function. When the caller calls the function then a value is put inside the function instead of the variable you put inside when you declared the function.

  2. Because it can be used for different things.
    Lets say you create a function for measuring the area of a square:

double area(double a, double b)
{
return a * b;
}

// which gives us the area of a square based on giving two side values instead of a and b.

1 Like
  1. It is an additional piece of information, some data, that we want to equip that function with. It is very handy, when we want to use that function multiple times, with different data
  2. To be more flexible, and to save us time and effort

What is a function parameter?

  • a function parameter is a variable passed into the function and exhibit the process
    Why do functions need parameters?
  • the function is reused the same set of instruction to different variables.
  1. Parameters are values that are passed to functions when they are called in the program execution.

  2. Functions need parameters to be able to operate on different values each time that they are called.

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?
In many cases, it is useful to be able to pass information to a function being called, so that the function has data to work with. For example, if we wanted to write a function to add two numbers, we need a way to tell the function which two numbers to add when we call it.

Source: Learncpp.com

  1. Function parameter is a pre-set variable type which gets value from a function call arguments.
  2. If you want your function to manipulate data you need to pass it to the function. Thus parameters were invented.
1 Like