



























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Dr. Mehandi Nandakumar delivered this lecture at Baddi University of Emerging Sciences and Technologies for Introduction to Computer Programming course. Its main points are: Functions, C , Program, Standard, Library, Prototype, Returning, Value, Compiler, Scope, Rules
Typology: Slides
1 / 35
This page cannot be seen from the preview
Don't miss anything!




























Function Prototype void^ myfunc();
//^ myfunc's
Protoype
-^ Like variable declaration; tells the compiler about thereturn type of the function and the number and typeof parameters it needs from the calling function:^ return_type
functionName (
parameter list
);
-^ So, place prototypes before
main()
is predefined in C++ and does not need a prototype• Can’t the compiler look ahead and get definitions?• Prototype can be omitted if whole function placedbefore it is called; is that a good practice?
Returning a Value
Scope Rules
-^ Scope rules tell that a variable is visible to which parts of yourprogram; also define variable’s lifetime•^ 3 types of variables: local, global, formal parameters
'x'^ :^ undeclared
identifier
Scope Rules
-Local Variables
Scope Rules
-Local Variables
int main(){ int choice;cout << "(1) add numbers or ";cout << "(2) concatenate strings?: ";cin >> choice;if(choice == 1){ int a, b;
/* activate two integer vars */cout << "Enter two numbers: ";cin >> a >> b;cout << "Sum is " << a+b << '\n'; } else{ char s1[80], s2[80];
/* activate two strings */ cout << "Enter two strings: ";cin >> s1;cin >> s2;strcat(s1, s2);cout << "Concatenation is " << s1 << '\n';} a = 10; // *** Error *** -- a not known here!return 0;}
Scope Rules
-Formal Parameters
-^ Formal parameters:
variables that receive values passed to a function
-^ Scope local to the function^ #include
<iostream.h>int mult(int,^ int);int main(){ int a =^ 10,^ b^ =
cout^ <<^ mult(a,
b); //cout^ <<^
x^ <<^ y;^ //
***^ Error
***^ --unknown
identifiers
x,^ y
return^ 0;} int mult(int^
x,^ int^ y)//
can^ have^
different^
names^ here
{ return^
x*y; }
Scope Rules-Global Variables• Usually declared outside any function; have life aslong as the program runs• Can be used by all following functions• Usually placed at the beginning of program• Initialized only at the start of the program;uninitialized default to zero• An identically named local variable masks globalone
Scope Rules-global variables #include <iostream.h>#include
void drill(){ int count; / This count is local. /int a, b, ans;// Generate two numbers between 0 and 99.a = rand() % 100;b = rand() % 100;// The user gets three tries to get it right.for(count=0; count<3; count++){ cout << "What is " << a << " + " << b << "? ";cin >> ans;if(ans==a+b){ cout << "Right\n";num_right++;return;} } cout << "You've used up all your tries.\n";cout << "The answer is " << a+b << '\n';}
Scope resolution operator :: (C++
only)
-^ The :: (scope resolution) operator is used toqualify hidden names so that you can still usethem. You can use the unary scope operator if anamespace scope or global scope name is hiddenby an explicit declaration of the same name in ablock or class. For example:
Passing Pointers to Functions
-^ No big deal. Just declare parameter as type _______?^ #include <iostream.h>void f(int *j);//or void f(int *);int main(){ int i;int *p;p = &i; // p now points to if(p);cout << i;
Passing Pointers to Functions
-^ The pointer variable not necessary. Can generate and pass theaddress of
i^ as such to
f()