Introduction To Computer Programming: Functions
Introduction To Computer Programming: Functions
1A-
2
Parts of function definition
Return type: The return type is the data type of the value that is
sent from the function.
Name: You should give each function a descriptive name. Same
rules as variable names
Parameter list: The program can send data into a function. The
parameter list is list of variables that hold the values being
passed to the function
Body: The body of a function is the set of statements that
perform the function’s operation. They are enclosed in a set of
braces.
Function call
• A function call is a statement that causes a function to
execute.
Function definition
1A-
4
Function definition
Void Functions
1A-
14
• void displayValue (int num)
• { cout << “The value is ” <<num << endl;
•}
• Int variable num is defined inside the parenthesis (int
num)
• The variable num is a parameter. The integer value is
accepted as an argument.
• Parameters are special purpose variables that are defined
inside the parenthesis of the function definition. They are
separate from the arguments of function call.
1A-
15
Some ground rules of functions
• In the function call you must not write the data type of
the argument variable , simply write the variable name.
• displayVaule (int x); //Error
• displayValue ( x ); Page 311
• In the function prototype its not necessary to write the
name of the parameter variable, only its data type is
required.
• void displayValue (int num) ; could be written as
• void displayValue ( int ) ;
• In essence parameter variables are initialized to the value1A-
16
1A-
17
Function rules
1A-
19
1A-
20
Passing data by value
1A-
21
1A-
22
Even though the parameter variable myValue is changed in the
changemMe function, the argument number is not modified. myValue
is only a copy of the number variable.
1A-
23
The return statement
1A-
24
1A-
25
Returning a value from a function
• A function may send a value back to the part of the
program that called the function.
• Although several arguments may be passed into a
function, only one value may be returned from it.
1A-
26
Defining a value returning function
• You have seen void functions. They don’t return a value.
Its mentioned by key word ‘void’ in the function header.
• Local variable result is defined in the function.
1A-
27
Calling a Value-Returning Function
1A-
28
More about value returning
function
1A-
29
Returning a boolean value
1A-
30
Thank You!