Docsity

Programming Fundamentals Chapter 05 – Functions Part-B, Slides of Programming Languages

Partial preview of the text

Download Programming Fundamentals Chapter 05 – Functions Part-B and more Slides Programming Languages in PDF only on Docsity!

Programming Fundamentals

Chapter 05 – Functions

Part-B

Functions

• Collection of Statements that perform

specific task

• Introduces Modular programming

  • The breaking up of a program into a set of

manageable functions

Functions

• Defining/Calling Functions

  • Function Call is a statement that causes a

function to execute

• Function Call will give a name and may

pass an argument

• So far, we see main() as a function … but

without any argument.

Functions

• Benefits

  • Makes coding manageable
  • Software re-usability (use existing blocks to

create new programs)

  • Avoid repeating code in a program
  • Saves time
  • Reduces coding complexities

Functions

  • Example: Math Library Functions
    • Perform common mathematical calculations
    • cout << sqrt( 900.0 );
      • Name
      • Open Parenthesis
      • Argument
      • Close Parenthesis
    • Math functions always take argument of type double and always returns result of type double
    • #include
    • Alternate → cout << sqrt (c1 + d * f );
      • i.e., arguments can be constants, variables, or even expressions

Functions

  • Example: Math Library Functions
    • ceil(x) → rounds x to smallest integer not less than x
    • cos(x) → cosine of x
    • exp(x) → exponential function e x
    • floor(x) → rounds x to larger integer not greater than x
    • fmod(x,y) → remainder of x/y as float
    • log(x) → natural log of x
    • log10(x) → log of x (base 10)
    • pow(x,y) → x ^ y
    • sin(x) → sign of x
    • sqrt(x) → square root of x
    • tan(x) → Tangent of x

Moving Ahead

• So far we have seen Functions in two

forms:

  • As the main() function (of every program)
  • As a call to a library function (e.g., sqrt and

pow)

• Next Step → Creating our own functions

Function Usage (

st

Example)

Our User Defined Function = Square() Calculates Square of No.s from 1 to 10 integer values are passed to and forth Function square is invoked from within main() with call square(x) The actual call is the (). Don’t forget this.

  • Actual Function Definition
  • square receives value of x in parameter y
  • If function is not returning anything to main(), then at least it should simply pass control back with return; Function Prototype Declaration int tells compiler that square returns integer void means nothing returned Omitting type would be syntax error

Function This Function That

• Function Prototype

• Function Signature

• Function Header

Function Prototype/Function

Declaration

  • int square (int)
    • Right int informs compiler that square expects an integer value from caller
    • Left int informs compiler that square returns an integer result to caller
  • Compiler checks:
    • Correct return type?
    • Correct number of arguments?
    • Correct argument types?
    • Correct order of arguments?
  • Not required if function body appears BEFORE

first use in program

Function Signature

• Signature or Function Signature is the

portion of function prototype containing:

  • Name of function
  • Type of arguments Signature portion of the function prototype

Function Header

• The declaration preceding the actual

Function Block

Header Preceding the Function Body

General Format

Return-value-type function-name(parameter-list) { declaration and statements; }

  • Function-name → Can be any valid identifier
  • Return-value-type → data type of result returned from function to caller
  • Parameter-list → comma separated. If no arguments passed, then will be left empty.
  • Decl. & statements → Form the function body (or Block)

Return of Control

1 Caller Function 2