% Created 2011-07-08 Fri 14:25
\documentclass[presentation]{beamer}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fixltx2e}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{float}
\usepackage{wrapfig}
\usepackage{soul}
\usepackage{textcomp}
\usepackage{marvosym}
\usepackage{wasysym}
\usepackage{latexsym}
\usepackage{amssymb}
\usepackage{hyperref}
\tolerance=1000
\usepackage[english]{babel} \usepackage{ae,aecompl}
\usepackage{mathpazo,courier,euler} \usepackage[scaled=.95]{helvet}
\usepackage{listings}
\lstset{language=Python, basicstyle=\ttfamily\bfseries,
commentstyle=\color{red}\itshape, stringstyle=\color{darkgreen},
showstringspaces=false, keywordstyle=\color{blue}\bfseries}
\providecommand{\alert}[1]{\textbf{#1}}

\title{}
\author{FOSSEE}
\date{}

\usetheme{Warsaw}\usecolortheme{default}\useoutertheme{infolines}\setbeamercovered{transparent}
\begin{document}











\begin{frame}

\begin{center}
\vspace{12pt}
\textcolor{blue}{\huge Getting started with \texttt{functions}}
\end{center}
\vspace{18pt}
\begin{center}
\vspace{10pt}
\includegraphics[scale=0.95]{../images/fossee-logo.png}\\
\vspace{5pt}
\scriptsize Developed by FOSSEE Team, IIT-Bombay. \\ 
\scriptsize Funded by National Mission on Education through ICT\\
\scriptsize  MHRD,Govt. of India\\
\includegraphics[scale=0.30]{../images/iitb-logo.png}\\
\end{center}
\end{frame}
\begin{frame}
\frametitle{Objectives}
\label{sec-2}

  At the end of this tutorial, you will be able to, 


\begin{itemize}
\item Define a function.
\item Define functions with arguments.
\item Learn about docstrings.
\item Learn about function return value.
\item Read and understand code.
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Pre-requisite}
\label{sec-3}

Spoken tutorial on -

\begin{itemize}
\item Conditionals.
\item Loops.
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Function}
\label{sec-4}


\begin{itemize}
\item Eliminate code redundancy
\item Help in code reuse
\item Subroutine
\begin{itemize}
\item relatively independent of remaining code
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Define \verb~f(x)~ in Python}
\label{sec-5}

\lstset{language=Python}
\begin{lstlisting}
def f(x):
    return x*x
\end{lstlisting}


\begin{itemize}
\item \verb~def~ - keyword
\item \verb~f~ - function name
\item \verb~x~ - parameter / argument to function \verb~f~
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Exercise 1}
\label{sec-6}



\begin{itemize}
\item Write a python function named \verb~cube~ which computes the cube of a given
  number \verb~n~.
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Exercise 2}
\label{sec-7}



\begin{itemize}
\item Write a python function named \verb~avg~ which computes the average of
  \verb~a~ and \verb~b~.
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Docstring}
\label{sec-8}



\begin{itemize}
\item Documenting/commenting code is a good practice
\lstset{language=Python}
\begin{lstlisting}
def avg(a,b):
    """ avg takes two numbers as input 
    (a & b), and returns the average 
    of a and b"""
    return (a+b)/2
\end{lstlisting}
\item Docstring
\begin{itemize}
\item written in the line after the \verb~def~ line.
\item Inside triple quote.
\end{itemize}
\item Documentation
\begin{verbatim}
     avg?
\end{verbatim}

\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Exercise 3}
\label{sec-9}


\begin{itemize}
\item Add docstring to the function f.
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Solution 3}
\label{sec-10}

\lstset{language=Python}
\begin{lstlisting}
def f(x):
    """Accepts a number x as argument and,
    returns the square of the number x."""
    return x*x
\end{lstlisting}
\end{frame}
\begin{frame}
\frametitle{Exercise 4}
\label{sec-11}


\begin{itemize}
\item Write a python function named \verb~circle~ which returns the area and
  perimeter of a circle given radius \verb~r~.
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{\verb~what~}
\label{sec-12}

\lstset{language=Python}
\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]
\frametitle{\verb~even\_digits~}
\label{sec-13}

\lstset{language=Python}
\begin{lstlisting}
def even_digits( n ):
   """returns True if all the digits of number 
   n is even returns False if all the digits 
   of number n is not even"""
    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]
\frametitle{\verb~what~}
\label{sec-14}

\lstset{language=Python}
\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{\verb~is\_perfect\_square~}
\label{sec-15}

\lstset{language=Python}
\begin{lstlisting}
def is_perfect_square( n ):
    """returns True and square root of n, 
    if n is a perfect square, otherwise 
    returns False and the square root 
    of the next perfect square"""
    i = 1
    while i * i < n:
        i += 1
    return i * i == n, i
\end{lstlisting}
\end{frame}
\begin{frame}
\frametitle{Summary}
\label{sec-16}

 In this tutorial, we have learnt to,


\begin{itemize}
\item Define functions in Python by using the keyword ``def''.
\item Call the function by specifying the function name.
\item Assign a docstring to a function by putting it as a triple quoted string.
\item Pass parameters to a function.
\item Return values from a function.
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Evaluation}
\label{sec-17}


\begin{enumerate}
\item What will the function do?
\lstset{language=Python}
\begin{lstlisting}
def what(x)
    return x*x
\end{lstlisting}
\begin{itemize}
\item Returns the square of x
\item Returns x
\item Function doesn't have docstring
\item Error
\end{itemize}
\vspace{3pt}
\item How many arguments can be passed to a python function?
\begin{itemize}
\item None
\item One
\item Two
\item Any
\end{itemize}
\vspace{3pt}
\item Write a function which calculates the area of a rectangle.
\end{enumerate}
\end{frame}
\begin{frame}[fragile]
\frametitle{Solutions}
\label{sec-18}


\begin{enumerate}
\item Error
\vspace{16pt}
\item Any
\vspace{12pt}
\item \lstset{language=Python}
\begin{lstlisting}
def area(l,b):
    return l * b
\end{lstlisting}
\end{enumerate}
\end{frame}
\begin{frame}

  \begin{block}{}
  \begin{center}
  \textcolor{blue}{\Large THANK YOU!} 
  \end{center}
  \end{block}
\begin{block}{}
  \begin{center}
    For more Information, visit our website\\
    \url{http://fossee.in/}
  \end{center}  
  \end{block}
\end{frame}

\end{document}