#8 User-defined Functions

#8 User-defined Functions - Lecture #7 User-defined...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture #7 User-defined Functions User-defined Functions The objective of this lecture is to learn on how to write our own user defined functions in C++. How do you define a function in C++? The syntax for a function definition is <data type> <function name> (<parameter list>) { [<local variable declaration>] [<statement>] [<return value>] } Example: We have been writing functions for quite some time, the main() function, for example #include <iostream.h> void main(void) { int x; x=1; cout<<x<<endl; } In this case: • void is the <data type> of the value returned by the function – void denotes that fact that the function does not return anything • main is the <function name> • void is the <parameter list> – void denotes the fact that it does not have any parameter • int x is the <local variable declaration> and • x = 1; cout<<x<<endl; are the [<statements>]
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Example: The following shows a program with two functions, one is main(), the other one is hello(). #include <iostream.h> void hello(void) { cout<<“Hello\n”; } void main(void) { hello( ); // function call or invoking the function } How do you invoke a function that has already been defined? A function that has been defined by the user is invoked/executed by giving its name and its parameters. In the example above, the hello() function is invoked inside the main(). We will also use the phrase “call the function hello()” to mean invoke/execute the statements inside the function. A function can be called inside the main() function several times. For example, if we want to print the word “Hello” three times, the main() function can be written as: void main(void) { hello(); // call the function the 1st time hello(); // call the function the 2nd time hello(); // call the function the 3rd time } However, if we want to call the hello() function 500 times, it is better to write it as: void main(void) { for (int i = 0; i < 500; i++) hello(); // call inside the body of the loop }
Background image of page 2
Exercise: 1. Notice that we wrote the implementation of the function hello() is written before main(). What will happen if the function hello() was written after the implementation of main()? That is, what will happen if we have written the codes as follows: #include <iostream.h> void main(void) { hello(); // call (invoke) the function } void hello(void) { cout<<“Hello\n”; } 2. Add another function that will simply print the word “Goodbye”. Use the name goodbye for the function. Call it inside main(). Note: In exercise 1, definitely there will be a syntax error: undeclared identifier hello. This is because we are trying to call/invoke the function hello without first defining the function. It is like using a variable without declaring it. With this problem, an alternative way of solving it is to use a function declaration.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 10

#8 User-defined Functions - Lecture #7 User-defined...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online