The basic form of a function definition is this:
It's called a function definition because we are defining the function. We are saying, "This is a function named function_name, whose inputs are input_1, input_2, etc., and whose output is output. When it is called, the function will execute the code in between its curly braces (output function_name (input_1, input_2, input_3, input_...) { // code to execute inside function }
{}
).At this point, let's refine our sample function definition. When programmers talk about functions, instead of the word input they usually use the word parameter. A parameter to a function is nothing more than an input to a function. At the same time, instead of using the word output, programmers generally refer to the return of a function. A particular function "returns" a value. So, here is our updated function definition:
Notice that in place ofreturn_type function_name (parameter_1, parameter_2, parameter_3, parameter_...) { // code to execute inside function }
output
, the function definition says return_type. That's because when we are actually writing a function definition, we'll put the return type there, immediately preceding the name of the function. The return type is nothing more than a plain old variable type, such as int
, or double
, etc.Similarly, parameters use variable types also. If the first input to a function is an
int
, then the first parameter will be something like int my_number
. We'll see what my_number
does in just a moment.
No comments:
Post a Comment