Vous êtes sur la page 1sur 10

SJES 2271/ SJEM 2231

10/10/2012

What is a function?

SJES 2271 : Scientific Computing 1


SJEM 2231 : Structured Programming
LECTURE NOTES (WEEK 5)
BY
DR NOOR FADIYA MOHD NOOR

Example

static the function


Twice () will only be
available to a caller in the
file where it is declared.
Not suitable for functions
need to be called from
another file/function.

DR NOOR FADIYA MOHD NOOR, UM

 A group of statements that is executed when the


corresponded identifier is called.
 Used to separate block of codes
 Used to create structure in a C++ program
 Execution begins when the main function is
called
 The format:
datatype identifiername (parameters)
{ block of statements;}
 Parameters can be called by value/reference.

Example

num and result are local


to the function.
return computes the
return value and exits the
function. If not included, it
causes bugs.

#include <iostream>
using namespace std;
int Addition (int x, int y)
{
int z;
z=x+y;
return(z);
}
int main ()
{
int a=2, b=1, Sum;
Sum=Addition(2, 10);
cout<< Sum<< endl;
cout<<Addition(a,b);
return 0;
}

SJES 2271/ SJEM 2231

10/10/2012

Example

#include <iostream>
using namespace std;
Message ();
main ()
{
Message ();
return 0;
}
int Message ()
{
cout<< This function<< endl;
cout<<returns nothing;
return 0;
}

What is data type void?


 A data type formalized in ASCII which means
nothing.
 Indicates absence of data type.
 Indicates a function does not return anything.
 Conventionally used to declare a pointer
which does not point to any data type.
 Can be used to replace empty list of
parameters of a function type void.

Example
 The format:
1) void identifiername (parameters)

{block of statements;}
2) void identifiername ()
{block of statements;}
3) void identifiername (void)
{block of statements;}

DR NOOR FADIYA MOHD NOOR, UM

#include <iostream>
using namespace std;
void main ()
{
cout<< This function<< endl;
cout<<returns nothing;
}

SJES 2271/ SJEM 2231

10/10/2012

Example

#include <iostream>
using namespace std;
void Message ()
{
cout<< This function<< endl;
cout<<returns nothing;
}
int main ()
{
Message ();
return 0;
}

Variable: Global Vs Local


GLOBAL

LOCAL

 Defined outside a
function.

 Defined inside a
function.

 Recognized by the
entire program.

 Recognized by the
particular function
only.

DR NOOR FADIYA MOHD NOOR, UM

What is Variable Scope?


 An important concept to write function.
 Determine how a function recognizes a variable
and how a variable is visible to that particular
function.
 Distinguish variables in a function from other
variables in other functions that might overwrite
them.
 Includes:
a) Global & local variables
b) Static & automatic variables
c) Passing arguments by value/copy
d) Passing arguments by address/reference

Example: Global Vs Local


GLOBAL variable

LOCAL variable

SJES 2271/ SJEM 2231

10/10/2012

Multiple Local Variables

Example: Static Vs Automatic


#Always assign value
to static variables

Variable: Static Vs Automatic


STATIC

AUTOMATIC

 All global variables


are static by
default.
 All static variables
retain their values.

 All local variables


are auto by
default.
 All auto variables
erase their values
when the function
ends.

Example: Error

STATIC variable

AUTOMATIC variable

DR NOOR FADIYA MOHD NOOR, UM

SJES 2271/ SJEM 2231

10/10/2012

Why we need to pass local


variable to other functions?
 Two or more functions can share the same
variable.
 Global variable cannot be reserved for certain
functions.
 To pass local arguments to other functions

Passing An Argument
 One or more arguments can be passed from
one function to another by value or by
address.
 A function that passes an argument is known
as the calling function.
 Another function that receives the argument
is known as the receiving function.
 The local variables must be placed in
parentheses () in both the calling and
receiving functions.

DR NOOR FADIYA MOHD NOOR, UM

Example
Note#:
Always
declare the
variable
data type
in the
receiving
function.

SJES 2271/ SJEM 2231

10/10/2012

Example

Example

DR NOOR FADIYA MOHD NOOR, UM

SJES 2271/ SJEM 2231

10/10/2012

Passing By Values
 A copy of the variables value is sent to the
receiving function parameter.
 The receiving function can modify the copied
value but not the calling functions variable.
 By default, all arguments or parameters are
passed by value.
 By default, all arrays are passed by address.

Example

DR NOOR FADIYA MOHD NOOR, UM

SJES 2271/ SJEM 2231

10/10/2012

Example: Renaming Passed


Variable

Passing By Address
 A variables address is copied and sent to the
receiving function parameter.
 All variables are stored in RAM memory
address assigned by C++ each time a variable
is defined.
 When the variable is called, C++ refers to that
address and uses whatever it needs.
 If changes was made to that variable in the
receiving function, changes will also affect
the variable in the calling function.

DR NOOR FADIYA MOHD NOOR, UM

Passing Non-Arrays By Address


 By default, non-array is passed by value.
 If a non-array has to change in both the
receiving and calling functions, non-array
must be passed by address.
 To pass a non-array by address, the argument
must be preceded with an ampersand (&) in
the receiving function.

SJES 2271/ SJEM 2231

10/10/2012

Example

#include <iostream>
#include <string>
using namespace std;
string getName();
void displayName(string & name);

string getName()
{
string nameF1;
cout << "From function<<
" \"string getName()\"\nWhat is your name?: ";
getline(cin, nameF1);
return (nameF1);
}

int main()
{
string name;
name = getName(); cout << "\n";
displayName(name); cout << "\n\n";
return 0;
}

void displayName(string & name)


{
cout << "Hello " << name <<
", this line is being displayed from\n
<< "function: \"void
displayName(string & name)\".";
}

Example

DR NOOR FADIYA MOHD NOOR, UM

SJES 2271/ SJEM 2231

10/10/2012

Output
______________________________________
From function "string getName()"
What is your name?: Justin
Hello Justin, this line is being displayed from
function: "void displayName(string & name)".
Press any key to continue . . .

DR NOOR FADIYA MOHD NOOR, UM

10

Vous aimerez peut-être aussi