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

The concept of functions in programming. It introduces modular programming and explains the benefits of using functions. The document also covers function prototypes, function signature, and function header. It provides examples of user-defined functions and library functions. The document also explains the internal workings of function calls and return mechanisms. It covers storage classes, scope, and linkage. The document also provides reading material for further study.

Typology: Slides

2021/2022

Available from 11/16/2022

razaroghani
razaroghani 🇵🇰

4.5

(4)

151 documents

1 / 76

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Programming Fundamentals
Chapter 05 Functions
Part-B
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
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c

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