24
Functions Functions http://vustudents.ning.com http://vustudents.ning.com •Modules in C++ are called functions and classes. •Main reason to use functions is : – get aid in conceptual organization of program. – reduce program size. –avoid repetition of code. •A function is invoked by a function call. –Using function name and parameters. •Library Functions and User Defined Functions.

Functions vustudents.ning

Embed Size (px)

DESCRIPTION

Functions http://vustudents.ning.com. Modules in C++ are called functions and classes . Main reason to use functions is : get aid in conceptual organization of program. reduce program size. avoid repetition of code. A function is invoked by a function call. - PowerPoint PPT Presentation

Citation preview

FunctionsFunctionshttp://vustudents.ning.comhttp://vustudents.ning.com

•Modules in C++ are called functions and classes.•Main reason to use functions is :

– get aid in conceptual organization of program.– reduce program size.–avoid repetition of code.

•A function is invoked by a function call.–Using function name and parameters.

•Library Functions and User Defined Functions.

#include <iostream.h>int square ( ); //function prototype

void main( ){

cout << square( );}

int square ( ) //function definition{

return 5 * 5;}

Function DefinitionsFunction Definitions

Passing Arguments to FunctionsPassing Arguments to Functions

• Passing Constants

• Passing Variables– Passing by Value– Passing by Reference

#include <iostream.h>int square (int); //function prototype

void main( ){

cout << square( 5);}

int square (int y) //function definition{

return y * y;}

Passing Constants to FunctionPassing Constants to Function

• Function creates new variables to hold the values passed.

• Initialize these variables with the values passed.

• Effect on actual variables?

Passing Variables - By ValuePassing Variables - By Value

#include <iostream.h>int square (int); //function prototype

void main( ){

int x ;cout << “\nEnter a number : ”;cin > x;cout << square(x);

}

int square (int y) //function definition{

return y *y;}

Passing Variables - By ValuePassing Variables - By Value

• Rewrite the previous program to get square of numbers from 1 to 10.

• Write a program using a function max( ) to determine and return the largest of three integers entered by the user.

Passing Variables - By ValuePassing Variables - By Value

Returning Values from FunctionsReturning Values from Functions

• The return Statement.

• Default return type - int.

• No value returned, if return type is void.

• If many arguments sent to functions - number of values returned ?

Passing Variables - By ReferencePassing Variables - By Reference

• A reference to original value is passed.

• Function has an access to actual variables in calling program.

• Provides a mechanism to return more than one value from the function.

Passing Variables - By ReferencePassing Variables - By Reference

# include <iostream.h>

void frac (float, float&, float&);

void main( )

{

float num, intpart, fracpart;

cout<<“\nEnter number ”; cin >> num;

frac(num, intpart, fracpart);

cout << “\nInteger part is “ <<intpart

<<“\nFraction part is “ <<fracpart;

}

Passing Variables - By ReferencePassing Variables - By Reference

void frac (float n, float& intp, float& fracp)

{

intp = float( long(n) );

fracp = n - intp;

}

// ampersand (&) is not used in function call

// In function call,no difference in value or

// reference parameters.

Overloaded FunctionsOverloaded Functions

• Same– Function name, return type .

• Different– Number of Arguments or– Type of Arguments

Overloaded FunctionsOverloaded Functions

#include <iostream.h>void square ( ); //function prototypevoid square (int);void square (int, int);

void main( ){

square( );square (10);

square (5,10);}

void square ( ){

cout << endl<<5 * 5;}

void square (int y) {

cout << endl<<y * y;}

void square (int s, int f) { for (; s <=f; s++)

cout << endl <<(s * s);}

Overloaded FunctionsOverloaded Functions

Inline FunctionsInline Functions

• Written like a normal function, but compiles into inline code instead of into a function.

• Compiler must have seen the function(not just the declaration) before first function call.

• Definition of inline function before main( ).

• inline keyword is a request to compiler, which may be ignored.

#include <iostream.h>

inline float lbstokg ( float pounds)

{ return 0.453592 * pounds;

}

void main ( )

{ float lbs;

cout << “\nEnter weight in pounds : “;

cin >> lbs;

cout <<“Weight in kilograms is “ << lbstokg(lbs);

}

Inline FunctionsInline Functions

Variables and Storage ClassesVariables and Storage Classes

• Lifetime– The time period between the creation and

destruction of a variable is called its lifetime or duration.

• Visibility– A variable’s visibility describes where within a

program it can be accessed.• Scope

– is that part of the program where the variable is visible.

Automatic VariablesAutomatic Variables

• An automatic variable is not created until the function in which it is defined is called.

• The word automatic is used as variables are automatically created when a function is called and auto destroyed when it returns.

• Lifetime of auto var coincides with the time when function is executing.

• Visible only within the function in which they are defined.

• No initialization of auto/local variables.

• Defined outside of any function.

• Can be accessed by any function.

• Exist for life of the program.

• Visible in the file in which they are defined.

External or Global VariablesExternal or Global Variables

• Defined within a function with keyword static.

• Visibility of a local variable.

• Lifetime of an external variable.

• Initialization takes place only once.

Static VariablesStatic Variables

# include <iostream.h>

float getavg(float);

void main ( )

{

float data=1, avg;

while (data != 0)

{

cout << “Enter a number ”; cin >> data;

avg = getavg(data);

cout << “New average is “<<avg<<endl;

}

}

Static VariablesStatic Variables

float getavg(float newdata)

{

static float total=0;

static int count = 0;

count++;

total += newdata;

return total / count;

}

Static VariablesStatic Variables

#include <iostream.h>

unsigned long factorial(unsigned long);

void main ( )

{

for (int I=0; I <=10; I++)

cout <<endl << “Factorial of “ << I

<< “ = “ <<factorial(I);

}

Recursive Function - Function calling itselfRecursive Function - Function calling itself

unsigned long factorial(unsigned long num)

{

if (num <=1)

return 1;

else

return (num * factorial (num-1));

}

http://vustudents.ning.com

Recursive Function - Function calling itselfRecursive Function - Function calling itself