Download presentation
Presentation is loading. Please wait.
Published byAnthony Singleton Modified over 8 years ago
1
FUNCTIONS (C) KHAERONI, M.SI
2
OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare function and its prototype to define function and call it with or without transmitting parameters To understand scope of each variables between functions
3
OUTLINE User defined function Function declaration/ prototypes Function definition Function calling Parametric transmission Variables scope
4
USER DEFINED FUNCTION
5
REVIEW In mathematics, a function computes a result from a given value; for example, from the function definition f (x) = 2x+3, we can compute f (5) = 13 and f (0) = 3 A function in C++ works like a mathematical function. In C++, a function is a named sequence of code that performs a specific task. A program itself consists of a collection of functions. One example of a function is the mathematical square root function Such a function, named sqrt, is available to C and C++ programs
6
EXAMPLE The square root function accepts one numeric value and produces a double value as a result; for example, the square root of 16 is 4, so when presented with 16.0, sqrt responds with 4.0 the programmer using the sqrt function within a program, the function is a black box; the programmer is concerned more about what the function does, not how it does it.
7
C++ USER DEFINED FUNCTIONS Consider a function in C++ called sqrt This function used to compute square root of a number. Can we create another ‘function’ in C++ that has similar use? Can we create another ‘function’ in C++ that using existing function? A function that we create in C++ called USER DEFINED FUNCTION
8
USER DEFINED FUNCTIONS Although C++ is shipped with a lot of standard functions, these functions are not enough for all users, therefore, C++ provides its users with a way to define their own functions (or user-defined function) For example, the library does not include a standard function that allows users to round a real number to the i th digits, therefore, we must declare and implement this function ourselves
9
PROBLEMS... How to create our defined function? How to call/use our defined function?
10
DEFINING A FUNCTION Generally speaking, we define a C++ function in two steps (preferably but not mandatory) function signature Step #1 – declare the function signature in either a header file (.h file) or before the main function of the program Step #2 – Implement the function in either an implementation file (.cpp) or after the main function
11
The function definition The function implementation
12
DEFINE A FUNCTION IN C++ SYNTAX A function definition has the following syntax: ( ){ } The function header The function body
14
FUNCTION INPUT AND OUTPUT
15
FUNCTION WITHOUT PARAMETERS The simplest function accepts no parameters and returns no value to the caller.
16
EXAMPLE #include using namespace std; // Definition of the prompt function void prompt() { cout << "Please enter an integer value: "; } int main() { int value1, value2, sum; cout << "This program adds together two integers." << endl; prompt(); // Call the function cin >> value1; prompt(); // Call the function again cin >> value2; sum = value1 + value2; cout << value1 << " + " << value2 << " = " << sum << endl; } Predict, what is the output?
17
EXAMPLE - BETTER PROMPT() #include using namespace std; int prompt() { int result; cout << "Please enter an integer value: "; cin >> result; return result; } int main() { int value1, value2, sum; cout << "This program adds together two integers." << endl; value1 = prompt(); // Call the function value2 = prompt(); // Call the function again sum = value1 + value2; cout << value1 << " + " << value2 << " = " << sum << endl; } Predict, what is the output?
18
EXAMPLE - EVEN BETTER PROMPT() #include using namespace std; int prompt(int n) { int result; cout << "Please enter integer no-" << n << " : "; cin >> result; return result; } int main() { int value1, value2, sum; cout << "This program adds together two integers." << endl; value1 = prompt(1); // Call the function value2 = prompt(2); // Call the function again sum = value1 + value2; cout << value1 << " + " << value2 << " = " << sum << endl; } Predict, what is the output?
19
WARNING! A function’s definition requires that all formal parameters be declared in the parentheses following the function’s name. A caller does not provide actual parameter type declarations when calling the function. Given the square_root function defined above, the following caller code fragment is illegal: double number = 25.0; //Legal, pass the variable's value to the function cout << square_root(number) << endl; //Illegal, do not declare the parameter during the call cout << square_root(double number) << endl; The function definition is responsible for declaring the types of its parameters, not the caller.
20
USING FUNCTIONS The general form of a function definition is type name( parameterlist ) { body }
21
USING FUNCTIONS The type of the function indicates the type of value the function returns. The special type void indicates that the function does not return a value. The name of the function is an identifier. The function’s name should indicate the purpose of the function. The parameterlist is a comma separated list of pairs of the form type name where type is a C++ type and name is an identifier representing a parameter. The body is the sequence of statements, enclosed within curly braces, that define the actions that the function is to perform The statements may include variable declarations, and any variables declared within the body are local to that function.
22
EXERCISE Use this function int factorial( int n) { int product=1; for (int i=1; i<=n; i++) product *= i; return product; } 1.Rewrite this function on your C++ code editor. Make sure that you have added header, etc as a complete C++ programme. 2.Use this function to compute factorial for first 10 natural numbers
23
PARAMETRIC TRANSMISSION & VARIABLES SCOPE
24
SHARING DATA AMONG USER-DEFINED FUNCTIONS There are two ways to share data among different functions Using global variables (very bad practice!) Passing data through function parameters Value parameters Reference parameters Constant reference parameters 24
25
I. USING GLOBAL VARIABLES #include using namespace std; int x = 0; //global variable, identified by all //scope of programme void f1() { x++; } void f2() { x+=4; f1(); } int main() { f2(); cout << x << endl; }
26
I. USING GLOBAL VARIABLES #include int x = 0; void f1() { x++; } void f2() { x+=4; f1(); } void main() { f2(); cout << x << endl; } x 0
27
I. USING GLOBAL VARIABLES x 0 void main() { f2(); f2(); cout << x << endl ; cout << x << endl ;} 1 #include int x = 0; void f1() { x++; } void f2() { x+=4; f1(); } void main() { f2(); cout << x << endl; }
28
I. USING GLOBAL VARIABLES #include int x = 0; void f1() { x++; } void f2() { x+=4; f1(); } void main() { f2(); cout << x << endl; } x 0 void main() { f2(); f2(); cout << x << endl ; cout << x << endl ;} 1 void f2() { x += 4; x += 4; f1(); f1();} 2 4
29
45 I. USING GLOBAL VARIABLES #include int x = 0; void f1() { x++; } void f2() { x+=4; f1(); } void main() { f2(); cout << x << endl; } x void main() { f2(); f2(); cout << x << endl ; cout << x << endl ;} 1 void f2() { x += 4; x += 4; f1(); f1();} 3 void f1() { x++; x++;} 4
30
45 I. USING GLOBAL VARIABLES #include int x = 0; void f1() { x++; } void f2() { x+=4; f1(); } void main() { f2(); cout << x << endl; } x void main() { f2(); f2(); cout << x << endl; cout << x << endl;} 1 void f2() { x += 4; x += 4; f1(); f1();} 3 void f1() { x++; x++;} 5
31
31 45 I. USING GLOBAL VARIABLES #include int x = 0; void f1() { x++; } void f2() { x+=4; f1(); } void main() { f2(); cout << x << endl; } x void main() { f2(); f2(); cout << x << endl; cout << x << endl;} 1 void f2() { x += 4; x += 4; f1(); f1();} 6
32
45 I. USING GLOBAL VARIABLES #include int x = 0; void f1() { x++; } void f2() { x+=4; f1(); } void main() { f2(); cout << x << endl; } x void main() { f2(); f2(); cout << x << endl; cout << x << endl;} 7
33
45 I. USING GLOBAL VARIABLES #include int x = 0; void f1() { x++; } void f2() { x+=4; f1(); } void main() { f2(); cout << x << endl; } x void main() { f2(); f2(); cout << x << endl; cout << x << endl;} 8
34
I. USING GLOBAL VARIABLES #include int x = 0; void f1() { x++; } void f2() { x+=4; f1(); } void main() { f2(); cout << x << endl; }
35
WHAT IS BAD ABOUT USING GLOBAL VARIABLES? Not safe! If two or more programmers are working together in a program, one of them may change the value stored in the global variable without telling the others who may depend in their calculation on the old stored value! Against The Principle of Information Hiding! Exposing the global variables to all functions is against the principle of information hiding since this gives all functions the freedom to change the values stored in the global variables at any time (unsafe!) 35
36
LOCAL VARIABLES Local variables are declared inside the function body and exist as long as the function is running and destroyed when the function exit You have to initialize the local variable before using it If a function defines a local variable and there was a global variable with the same name, the function uses its local variable instead of using the global variable 36
37
EXAMPLE OF DEFINING AND USING GLOBAL AND LOCAL VARIABLES #include using namespace std; int x; // Global variable void fun(); // function signature int main() { x = 4; fun(); cout << x << endl; } void fun() { int x = 10; // Local variable cout << x << endl; } x 0 Global variables are automatically initialized to 0
38
EXAMPLE OF DEFINING AND USING GLOBAL AND LOCAL VARIABLES #include using namespace std; int x; // Global variable void fun(); // function signature int main() { x = 4; fun(); cout << x << endl; } void fun() { int x = 10; // Local variable cout << x << endl; } x 0 void main() { x = 4; x = 4; fun(); fun(); cout << x << endl; cout << x << endl;} 1
39
EXAMPLE OF DEFINING AND USING GLOBAL AND LOCAL VARIABLES #include using namespace std; int x; // Global variable void fun(); // function signature int main() { x = 4; fun(); cout << x << endl; } void fun() { int x = 10; // Local variable cout << x << endl; } x 4 void main() { x = 4; x = 4; fun(); fun(); cout << x << endl; cout << x << endl;} 2 void fun() { int x = 10; int x = 10; cout << x << endl; cout << x << endl;} x ???? 3
40
EXAMPLE OF DEFINING AND USING GLOBAL AND LOCAL VARIABLES #include using namespace std; int x; // Global variable void fun(); // function signature int main() { x = 4; fun(); cout << x << endl; } void fun() { int x = 10; // Local variable cout << x << endl; } x 4 void main() { x = 4; x = 4; fun(); fun(); cout << x << endl; cout << x << endl;} 2 void fun() { int x = 10; int x = 10; cout << x << endl; cout << x << endl;} x 10 3
41
EXAMPLE OF DEFINING AND USING GLOBAL AND LOCAL VARIABLES #include using namespace std; int x; // Global variable void fun(); // function signature int main() { x = 4; fun(); cout << x << endl; } void fun() { int x = 10; // Local variable cout << x << endl; } x 4 void main() { x = 4; x = 4; fun(); fun(); cout << x << endl; cout << x << endl;} 2 void fun() { int x = 10; int x = 10; cout << x << endl; cout << x << endl;} x 10 4
42
EXAMPLE OF DEFINING AND USING GLOBAL AND LOCAL VARIABLES #include using namespace std; int x; // Global variable void fun(); // function signature int main() { x = 4; fun(); cout << x << endl; } void fun() { int x = 10; // Local variable cout << x << endl; } x 4 void main() { x = 4; x = 4; fun(); fun(); cout << x << endl; cout << x << endl;} 2 void fun() { int x = 10; int x = 10; cout << x << endl; cout << x << endl;} x 10 5
43
EXAMPLE OF DEFINING AND USING GLOBAL AND LOCAL VARIABLES #include using namespace std; int x; // Global variable void fun(); // function signature int main() { x = 4; fun(); cout << x << endl; } void fun() { int x = 10; // Local variable cout << x << endl; } x 4 void main() { x = 4; x = 4; fun(); fun(); cout << x << endl; cout << x << endl;} 6
44
EXAMPLE OF DEFINING AND USING GLOBAL AND LOCAL VARIABLES #include using namespace std; int x; // Global variable void fun(); // function signature int main() { x = 4; fun(); cout << x << endl; } void fun() { int x = 10; // Local variable cout << x << endl; } x 4 void main() { x = 4; x = 4; fun(); fun(); cout << x << endl; cout << x << endl;} 7
45
II. USING PARAMETERS Function Parameters come in three flavors: Value parameters Value parameters – which copy the values of the function arguments Reference parameters – which refer to the function arguments by other local names and have the ability to change the values of the referenced arguments Constant reference parameters – similar to the reference parameters but cannot change the values of the referenced arguments
46
VALUE PARAMETERS This is what we use to declare in the function signature or function header, e.g. int max (int x, int y); Here, parameters x and y are value parameters When you call the max function as max(4, 7), the values 4 and 7 are copied to x and y respectively When you call the max function as max (a, b), where a=40 and b=10, the values 40 and 10 are copied to x and y respectively When you call the max function as max( a+b, b/2), the values 50 and 5 are copies to x and y respectively Once the value parameters accepted copies of the corresponding arguments data, they act as local variables! 46
47
x 0 void main() { x = 4; x = 4; fun(x/2+1); fun(x/2+1); cout << x << endl; cout << x << endl;} 1 EXAMPLE OF USING VALUE PARAMETERS AND GLOBAL VARIABLES #include using namespace std; // Global variable int x; // Global variable void fun(int x) { cout << x << endl; x=x+5; } int main() { x = 4; fun(x/2+1); cout << x << endl; }
48
x 4 void main() { x = 4; x = 4; fun(x/2+1); fun(x/2+1); cout << x << endl; cout << x << endl;} 2 void fun(int x ) { cout << x << endl; cout << x << endl; x=x+5; x=x+5;} 3 3 EXAMPLE OF USING VALUE PARAMETERS AND GLOBAL VARIABLES #include using namespace std; // Global variable int x; // Global variable void fun(int x) { cout << x << endl; x=x+5; } int main() { x = 4; fun(x/2+1); cout << x << endl; }
49
x 4 void main() { x = 4; x = 4; fun(x/2+1); fun(x/2+1); cout << x << endl; cout << x << endl;} 2 void fun(int x ) { cout << x << endl; cout << x << endl; x=x+5; x=x+5;} 3 4 EXAMPLE OF USING VALUE PARAMETERS AND GLOBAL VARIABLES #include using namespace std; // Global variable int x; // Global variable void fun(int x) { cout << x << endl; x=x+5; } int main() { x = 4; fun(x/2+1); cout << x << endl; } 8
50
x 4 void main() { x = 4; x = 4; fun(x/2+1); fun(x/2+1); cout << x << endl; cout << x << endl;} 2 void fun(int x ) { cout << x << endl; cout << x << endl; x=x+5; x=x+5;} 38 5 EXAMPLE OF USING VALUE PARAMETERS AND GLOBAL VARIABLES #include using namespace std; // Global variable int x; // Global variable void fun(int x) { cout << x << endl; x=x+5; } int main() { x = 4; fun(x/2+1); cout << x << endl; }
51
x 4 void main() { x = 4; x = 4; fun(x/2+1); fun(x/2+1); cout << x << endl; cout << x << endl;} 6 #include using namespace std; // Global variable int x; // Global variable void fun(int x) { cout << x << endl; x=x+5; } int main() { x = 4; fun(x/2+1); cout << x << endl; } EXAMPLE OF USING VALUE PARAMETERS AND GLOBAL VARIABLES
52
x 4 void main() { x = 4; x = 4; fun(x/2+1); fun(x/2+1); cout << x << endl; cout << x << endl;} 7 #include using namespace std; // Global variable int x; // Global variable void fun(int x) { cout << x << endl; x=x+5; } int main() { x = 4; fun(x/2+1); cout << x << endl; } EXAMPLE OF USING VALUE PARAMETERS AND GLOBAL VARIABLES
53
REFERENCE PARAMETERS As we saw in the last example, any changes in the value parameters don’t affect the original function arguments Sometimes, we want to change the values of the original function arguments or return with more than one value from the function, in this case we use reference parameters A reference parameter is just another name to the original argument variable We define a reference parameter by adding the & in front of the parameter name, e.g. double update (double & x);
54
54 #include using namespace std; void fun(int &y) { cout << y << endl; y = y+5; } int main() { int x = 4; //Local variable fun(x); cout << x << endl; } void main() { int x = 4; int x = 4; fun(x); fun(x); cout << x << endl; cout << x << endl;} 1 x ? x 4 EXAMPLE OF REFERENCE PARAMETERS
55
55 void main() { int x = 4; int x = 4; fun(x); fun(x); cout << x << endl; cout << x << endl;} 2 x ? x 4 void fun( int & y ) { cout<<y<<endl; cout<<y<<endl; y=y+5; y=y+5;} 3 #include using namespace std; void fun(int &y) { cout << y << endl; y = y+5; } int main() { int x = 4; //Local variable fun(x); cout << x << endl; } EXAMPLE OF REFERENCE PARAMETERS
56
56 void main() { int x = 4; int x = 4; fun(x); fun(x); cout << x << endl; cout << x << endl;} 2 x ? x 4 void fun( int & y ) { cout<<y<<endl; cout<<y<<endl; y=y+5; y=y+5;} 4 9 #include using namespace std; void fun(int &y) { cout << y << endl; y = y+5; } int main() { int x = 4; //Local variable fun(x); cout << x << endl; } EXAMPLE OF REFERENCE PARAMETERS
57
57 void main() { int x = 4; int x = 4; fun(x); fun(x); cout << x << endl; cout << x << endl;} 2 x ? x 9 void fun( int & y ) { cout<<y<<endl; cout<<y<<endl; y=y+5; y=y+5;} 5 #include using namespace std; void fun(int &y) { cout << y << endl; y = y+5; } int main() { int x = 4; //Local variable fun(x); cout << x << endl; } EXAMPLE OF REFERENCE PARAMETERS
58
58 void main() { int x = 4; int x = 4; fun(x); fun(x); cout << x << endl; cout << x << endl;} 6 x ? x 9 #include using namespace std; void fun(int &y) { cout << y << endl; y = y+5; } int main() { int x = 4; //Local variable fun(x); cout << x << endl; } EXAMPLE OF REFERENCE PARAMETERS
59
59 void main() { int x = 4; int x = 4; fun(x); fun(x); cout << x << endl; cout << x << endl;} x ? x 9 7 #include using namespace std; void fun(int &y) { cout << y << endl; y = y+5; } int main() { int x = 4; //Local variable fun(x); cout << x << endl; } EXAMPLE OF REFERENCE PARAMETERS
60
CONSTANT REFERENCE PARAMETERS Constant reference parameters are used under the following two conditions: The passed data are so big and you want to save time and computer memory The passed data will not be changed or updated in the function body For example void report (const string & prompt); The only valid arguments accepted by reference parameters and constant reference parameters are variable names It is a syntax error to pass constant values or expressions to the (const) reference parameters
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.