Download presentation
Presentation is loading. Please wait.
Published byKory McCormick Modified over 9 years ago
1
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif
2
Global VS Local variables local variable ▫ Variables that declared inside a function ▫ Declared inside any block of code Global variables ▫ Opposite of local the known throughout the entire program
3
Global VS Local variables #include Using namespace std;
4
Global VS Local variables #include Using namespace std; void main() { int i=4; int j=10; i++; if (j > 0) cout<<“ i is “<<i<<endl; if (j > 0) { int i=100; /* 'i' is defined and so local to * this block */ Cout << "i is” << i; } //end if cout<<"i is “<<i; } //end main I is 5 I is 100 I is 5
5
Function reusable The function are reusable. ProceduresRoutines Methods functions may be called Procedures or Routines and in object oriented programming called Methods. Save programmers’ time.
6
Prototype VS. Declaration prototype : n ormally placed before the start of main() but must be before the function definition. General form : function_return_type function_name (type parameter1, type parameter2,…, type parameterN) ; EX: void Factorial (int x); // prototype -- x is a parameter
7
Prototype VS. Declaration Prototype : void foo(int x); // prototype -- x is a parameter Declaration void foo(int x) // declaration -- x is a parameter { Statement. }
8
EX: #include using namespace std; int square (int x) { return x*x; } Void main ( ) { int number; cout<<"Enter any number to Calculate the square of this number "; cin>>number; cout<<endl; cout<<"the square of "<<number<<" is " <<square(number)<<endl; }
9
EX: #include using namespace std; int square (int x) ; Void main ( ) { int number; cout<<"Enter any number to Calculate the square of this number "; cin>>number; cout<<endl; cout<<"the square of "<<number<<" is " <<square(number)<<endl; } int square (int x) {return x*x;}
10
Finding Errors in Function Code int sum(int x, int y) { int result; result = x+y; } this function must return an integer value as indicated in the header definition (return result;) should be added this function must return an integer value as indicated in the header definition (return result;) should be added
11
Finding Errors in Function Code int sum (int n) { if (n==0) return 0; else n+(n-1); } the result of n+sum(n-1) is not returned; sum returns an improper result, the else part should be written as:- else return n+(n-1); the result of n+sum(n-1) is not returned; sum returns an improper result, the else part should be written as:- else return n+(n-1);
12
Finding Errors in Function Code void f(float a); { float a; cout<<a<<endl; } ; found after function definition header. redefining the parameter a in the function void f(float a) { float a2 = a + 8.9; cout <<a2<<endl; } ; found after function definition header. redefining the parameter a in the function void f(float a) { float a2 = a + 8.9; cout <<a2<<endl; }
13
Finding Errors in Function Code void product(void) { int a, b, c, result; cout << “enter three integers:”; cin >> a >> b >> c; result = a*b*c; cout << “Result is” << result; return result; } According to the definition it should not return a value, but in the block (body) it did & this is WRONG. Remove return Result; According to the definition it should not return a value, but in the block (body) it did & this is WRONG. Remove return Result;
14
Parameters vs Arguments Up until now, we have not differentiated between function parameters and arguments. In common usage, the terms parameter and argument are often interchanged. ▫A function parameter is a variable declared in the prototype or declaration of ▫An argument is the value that is passed to the function in place of a parameter.
15
methods of passing There are 2 primary methods of passing arguments to functions: ▫pass by value, ▫pass by reference,
16
Passing arguments by value By default, arguments in C++ are passed by value. When arguments are passed by value, a copy of the argument is passed to the function. void foo(int y) { cout << "y = " << y << endl; } int main() { foo(5); // first call int x = 6; foo(x); // second call foo(x+1); // third call return 0; }
17
Passing arguments by value Because a copy of the argument is passed to the function, the original argument can not be modified by the function. 1.void foo(int y) 2.{ 3. cout << "y = " << y << endl; 4. 5. y = 6; 6. 7. cout << "y = " << y << endl; 8.} // y is destroyed here 9. 10.int main() 11.{ 12. using namespace std; 13. int x = 5; 14. cout << "x = " << x << endl; 15. 16. foo(x); 17. 18. cout << "x = " << x << endl; 19. return 0; 20.}
18
Adv. And DisAdv. Of Passing by value AdvantagesDisadvantages ▫Arguments passed by value can be: variables (eg. x), literals (eg. 6), or expressions (eg. x+1) or (eg foo()). ▫Arguments are never changed by the function being called, which prevents side effects. Copying large structs or classes can take a lot of time to copy, and this can cause a performance penalty, especially if the function is called many times.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.