Functions Part 1-Introduction to Computer Programming-Lecture Slides, Slides of Computer Programming

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

2011/2012

Uploaded on 07/13/2012

ekbaal
ekbaal 🇮🇳

3

(1)

30 documents

1 / 35

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 192
Lecture 14
Fall 2011
December 14-15, 2011
Ghufran Ahmed
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23

Partial preview of the text

Download Functions Part 1-Introduction to Computer Programming-Lecture Slides and more Slides Computer Programming in PDF only on Docsity!

CS 192Lecture 14Fall 2011December 14-15, 2011Ghufran Ahmed

Functions

• Building blocks of a C++ program• Each function has a name, which is used tocall the function; functions call each other• You will write your own functions, e.g.^ main()

, and also use pre-written functions

from standard library

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()

  • 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

// Returning a value.#include <iostream.h>int mul(int x, int y);

// mul()'s prototype

int main(){ int answer;answer = mul(10, 11); // assign return valuecout << "The answer is " <<

answer;

return 0;} // This function returns a value.int mul(int x, int y){ return x * y; // return product of x and y}

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

Scope Rules - Local Variables• A variable can be declared inside any block and is thenlocal to that block• Block: {}^ int main(){ {int x = 4;}cout << x;return 0;} • error^ C2065:

'x'^ :^ undeclared

identifier

•^ Memory storage for a local variable created when enteringits block and destroyed when its block exited

Scope Rules

-Local Variables

#include <iostream.h>int main(){ int i, j;i = 10;j = 100;if(j > 0){^ // start of blockint i; // this i is separate from outer ii = j / 2; // outer j is known herecout << “first inner i: " << i << '\n';} if(i < 50){ i += 10; cout << “

nd^ inner i: “ << i << endl;

} cout << "outer i: " << i << '\n';return 0;} 50 20 20 • Declaring within a conditional block also saves memory; see next slide

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; //count and num_right are globalint num_right;int main(){ cout << "How many practice problems: ";cin >> count;num_right = 0;do{ drill();count--;} while(count);cout << "You got " << num_right <<“ right.\n";return 0;}

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;

// i is now 100

return 0;} void f(int *j){ *j = 100; // var pointed to by j is assigned 100}

i^26100 p j 100 100

Passing Pointers to Functions

-^ The pointer variable not necessary. Can generate and pass theaddress of

i^ as such to

f()

#include <iostream.h>void f(int *j);int main(){ int i;f(&i);cout << i;return 0;} void f(int *j){ *j = 100; // var pointed to by j is assigned 100}

i^26100 j^100