Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1201: Programming Language 2

Similar presentations


Presentation on theme: "CS1201: Programming Language 2"— Presentation transcript:

1 CS1201: Programming Language 2
By:Nouf Aljaffan Edited by : Nouf Almunyif Function II

2 Scope of variables

3 C++ Variables A variable is a place in memory that has
A name or identifier (e.g. income, taxes, etc.) A data type (e.g. int, double, char, etc.) A size (number of bytes) A scope (the part of the program code that can use it) Global variables – all functions can see it and using it Local variables – only the function that declare local variables see and use these variables A life time (the duration of its existence) Global variables can live as long as the program is executed Local variables are lived only when the functions that define these variables are executed

4

5 Global VS Local variables
Variables that declared inside a function Declared inside any block of code Global variables Opposite of local the known throughout the entire program

6 Global VS Local variables
#include < iostream> Using namespace std;

7 Global VS Local variables
I is 5 I is 100 I is 5 #include < iostream> Using namespace std; void main() { int i=4; int j=10; i++; if (j > 0) cout<<“ i is “<<i<<endl; { int i=100; /* 'i' is defined and so local to * this block */ Cout << "i is” << i; } //end if cout<<"i is “<<i; } //end main

8 Scope of Local Variables, cont.

9 Variable Scope In fact, you can reuse names in a scope which is nested inside another scope int main ( ) { int i = 5, j = 0; for (j = 0; j < 10; j++) { int i = j; // OK, this is new i int k = 5; doSomething (i); } int sum = k; // compile error, no k in scope j = i; // sets j to 5 for (j = 0; j < 100; j++ ) { int i = j; // yet another new i int i = 0; // compile error –redefined variable void doSomething(int i){ cout<<++i;}// OK, this is new i

10 Variable Scope Local variables of same name can be nested inside global variables #include<iostream> using namespace std; int i = 10; int main ( ) { cout<<i<<endl;//10 for (int j = 0; j < 10; j++ ) { int i = 20; cout<<i<<endl;//20 } int i = 30; cout<<i<<endl;//30 return 0;} int total = 5; int main ( ) { int total = 4; // OK, this is nested scope …. } int sub1 ( ) { int i = total; // OK, i set to 5

11 Function The function are reusable.
functions may be called Procedures or Routines and in object oriented programming called Methods . Save programmers’ time.

12 Prototype VS. Declaration
prototype : normally 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

13 Prototype VS. Declaration
void foo(int x); // prototype -- x is a parameter Declaration void foo(int x) // declaration -- x is a parameter { Statement . }

14 EX: #include<iostream> 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; }

15 EX: #include<iostream> 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; }

16 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

17 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);

18 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; }

19 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;

20 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.

21 methods of passing There are 2 primary methods of passing arguments to functions: pass by value, pass by reference,

22 Passing arguments by value
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; 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.

23 Passing arguments by value
void foo(int y) {     cout << "y = " << y << endl;     y = 6; } // y is destroyed here int main()     using namespace std;     int x = 5;     cout << "x = " << x << endl;     foo(x);     return 0; } Because a copy of the argument is passed to the function, the original argument can not be modified by the function.

24 Adv. And DisAdv. Of Passing by value
Advantages Disadvantages 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.


Download ppt "CS1201: Programming Language 2"

Similar presentations


Ads by Google