functions http vustudents ning com
Download
Skip this Video
Download Presentation
Functions vustudents.ning

Loading in 2 Seconds...

play fullscreen
1 / 24

Functions vustudents.ning - PowerPoint PPT Presentation


  • 61 Views
  • Uploaded on

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Functions vustudents.ning' - valencia-cruz


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
functions http vustudents ning com

Functionshttp://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.

slide2

Function Definitions

#include <iostream.h>

int square ( ); //function prototype

void main( )

{

cout << square( );

}

int square ( ) //function definition

{

return 5 * 5;

}

passing arguments to functions
Passing Arguments to Functions
  • Passing Constants
  • Passing Variables
    • Passing by Value
    • Passing by Reference
slide4

Passing Constants to Function

#include <iostream.h>

int square (int); //function prototype

void main( )

{

cout << square( 5);

}

int square (int y) //function definition

{

return y * y;

}

slide5

Passing Variables - By Value

  • Function creates new variables to hold the values passed.
  • Initialize these variables with the values passed.
  • Effect on actual variables?
slide6

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

}

slide7

Passing 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.
returning values from functions
Returning 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 reference
Passing 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 reference1
Passing 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 reference2
Passing 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 functions
Overloaded Functions
  • Same
    • Function name, return type .
  • Different
    • Number of Arguments or
    • Type of Arguments
overloaded functions1
Overloaded Functions

#include <iostream.h>

void square ( ); //function prototype

void square (int);

void square (int, int);

void main( )

{

square( );

square (10);

square (5,10);

}

overloaded functions2
Overloaded Functions

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

}

inline functions
Inline 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.
inline functions1
Inline Functions

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

}

variables and storage classes
Variables 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 variables
Automatic 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.
external or global variables
External or Global 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.
static variables
Static Variables
  • Defined within a function with keyword static.
  • Visibility of a local variable.
  • Lifetime of an external variable.
  • Initialization takes place only once.
static variables1
Static 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 variables2
Static Variables

float getavg(float newdata)

{

static float total=0;

static int count = 0;

count++;

total += newdata;

return total / count;

}

recursive function function calling itself
Recursive Function - Function calling itself

#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 itself1
Recursive 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

ad