0% found this document useful (0 votes)
4 views3 pages

Functions and Simple Python Guide

The document discusses functions in programming, comparing them to mathematical functions and outlining their syntax in Python. It covers the anatomy of function definitions and calls, as well as the concept of Simple Python, which simplifies expressions for easier reasoning. Key takeaways include the structure of function definitions and the behavior of function calls in programming.

Uploaded by

Sarma Vvs
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Functions and Simple Python Guide

The document discusses functions in programming, comparing them to mathematical functions and outlining their syntax in Python. It covers the anatomy of function definitions and calls, as well as the concept of Simple Python, which simplifies expressions for easier reasoning. Key takeaways include the structure of function definitions and the behavior of function calls in programming.

Uploaded by

Sarma Vvs
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Functions in Programming Syntax Simple Python Summary Functions in Programming Syntax Simple Python Summary

Outline

Functions and Simple Python Functions in Programming

Venkatesh Choppella Syntax


IIIT Hyderabad India

International Institute of Information Technology, Hyderabad Simple Python

Summary

Venkatesh Choppella ©2024 Venkatesh Choppella IIIT Hyderabad Functions and Simple Python – 1 / 12 Venkatesh Choppella ©2024 Venkatesh Choppella IIIT Hyderabad Functions and Simple Python – 2 / 12

Functions in Programming Syntax Simple Python Summary Functions in Programming Syntax Simple Python Summary

Topic Functions in Mathematics vs. Functions in Programming

f :A→B
Functions in Programming
Mathematics Programming
Syntax
• For each a : A, f (a) • For each a : A, f (a)
Simple Python • returns a unique value b : B, or
• returns a unique b : B.
Summary • causes an error to be raised, or

• runs forever

Venkatesh Choppella ©2024 Venkatesh Choppella IIIT Hyderabad Functions and Simple Python – 3 / 12 Venkatesh Choppella ©2024 Venkatesh Choppella IIIT Hyderabad Functions and Simple Python – 4 / 12
Functions in Programming Syntax Simple Python Summary Functions in Programming Syntax Simple Python Summary

Topic Anatomy of a Function Definition and Function Call


keyword
function name
formals list
Functions in Programming 0

def f (x , y ): definition head


1
Syntax a = ( x + y ) // (x - y )
2 function body
Simple Python return a
3

ans = f (3 , 3+2) function call


Summary 4
operands
print ( " ans = " , ans )
5

# done

Venkatesh Choppella ©2024 Venkatesh Choppella IIIT Hyderabad Functions and Simple Python – 5 / 12 Venkatesh Choppella ©2024 Venkatesh Choppella IIIT Hyderabad Functions and Simple Python – 6 / 12

Functions in Programming Syntax Simple Python Summary Functions in Programming Syntax Simple Python Summary

Abstract Syntax Topic

Stmt ::= Exp | Assign | If | While


Fndef | Return Functions in Programming
Assign ::= Id = Rhs

FnDef ::= def Id Formal ∗ Body Syntax


Rhs ::= Exp

Formal ::= Id Simple Python


Exp ::= . . . | FnCall

Body ::= Block Summary


FnCall ::= Id Operand ∗

Return ::= return Exp


Operand ::= Exp

Venkatesh Choppella ©2024 Venkatesh Choppella IIIT Hyderabad Functions and Simple Python – 7 / 12 Venkatesh Choppella ©2024 Venkatesh Choppella IIIT Hyderabad Functions and Simple Python – 8 / 12
Functions in Programming Syntax Simple Python Summary Functions in Programming Syntax Simple Python Summary

Abstract Syntax (Simple Python) Why Simple Python?

Stmt ::= Exp | Assign | If | While


Fndef | Return 1. Simple Python is Python in which all 3. An arbitrary expression may be
expressions are simple, i.e., do not converted to a sequence of assignment
Assign ::= Id = Rhs involve any function calls. statements each of whose RHS is a
FnDef ::= def Id Formal ∗ Body simple expression.
Rhs ::= Exp | FnCall
Formal ::= Id
2. Simple expressions leave the
FnCall ::= Id Operand ∗ environment unchanged when 4. It is easier to trace and reason with
Body ::= Block
evaluated. programs in Simple Python.
Operand ::= Exp
Return ::= return Exp

Venkatesh Choppella ©2024 Venkatesh Choppella IIIT Hyderabad Functions and Simple Python – 9 / 12 Venkatesh Choppella ©2024 Venkatesh Choppella IIIT Hyderabad Functions and Simple Python – 10 / 12

Functions in Programming Syntax Simple Python Summary Functions in Programming Syntax Simple Python Summary

Topic Key Takeaways

Functions in Programming
3. The definition head has a function
name and formal parameters.
Syntax 1. In programming, calling a function may
either cause a value to be returned, an
error to be raised, or neither, in which
Simple Python case the call never returns. 4. A function call has an identifier and
operand expressions.

Summary

2. A function definition has a definition 5. In simple python, the right hand side
head and a definition body. of an assignment is either an
expression or a function call.
Venkatesh Choppella ©2024 Venkatesh Choppella IIIT Hyderabad Functions and Simple Python – 11 / 12 Venkatesh Choppella ©2024 Venkatesh Choppella IIIT Hyderabad Functions and Simple Python – 12 / 12

You might also like