# HG changeset patch # User Puneeth Chaganti # Date 1291919699 -19800 # Node ID db7b234655723586d1ff68cc3acc66baa0d92179 # Parent 80028e4eee3d1c7661c5a5cc65ba0e45c4ae84ce Fixed day2/session3.tex. diff -r 80028e4eee3d -r db7b23465572 day2/session3.tex --- a/day2/session3.tex Fri Dec 10 00:04:52 2010 +0530 +++ b/day2/session3.tex Fri Dec 10 00:04:59 2010 +0530 @@ -78,7 +78,7 @@ \author[FOSSEE Team] {The FOSSEE Group} \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} -\date[] {1 May, 2010\\Day 2, Session 3} +\date[] {SciPy.in 2010, Tutorials} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} @@ -124,6 +124,75 @@ \end{frame} \section{Functions} + +\begin{frame}[fragile] + \frametitle{Functions} + \begin{itemize} + \item \kwrd{def} - keyword to define a function + \item Arguments are local to a function + \item Functions can return multiple values + \end{itemize} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Functions: example} + \begin{lstlisting} +def signum( r ): + """returns 0 if r is zero + -1 if r is negative + +1 if r is positive""" + if r < 0: + return -1 + elif r > 0: + return 1 + else: + return 0 + \end{lstlisting} + \emphbar{Note docstrings} +\end{frame} + +\begin{frame}[fragile] + \frametitle {What does this function do?} + \begin{lstlisting} +def what( n ): + if n < 0: n = -n + while n > 0: + if n % 2 == 1: + return False + n /= 10 + return True + \end{lstlisting} +\end{frame} + +\begin{frame}[fragile] + {What does this function do?} +\begin{lstlisting} +def what( n ): + i = 1 + while i * i < n: + i += 1 + return i * i == n, i + \end{lstlisting} +\end{frame} + +\begin{frame}[fragile] + \frametitle {What does this function do?} + \begin{lstlisting} +def what( x, n ): + if n < 0: + n = -n + x = 1.0 / x + + z = 1.0 + while n > 0: + if n % 2 == 1: + z *= x + x *= x + n /= 2 + return z + \end{lstlisting} +\end{frame} + \subsection{Default arguments} \begin{frame}[fragile] \frametitle{Functions: default arguments}