\documentclass{beamer}
%
%
\makeatletter
\@ifundefined{lhs2tex.lhs2tex.sty.read}%
  {\@namedef{lhs2tex.lhs2tex.sty.read}{}%
   \newcommand\SkipToFmtEnd{}%
   \newcommand\EndFmtInput{}%
   \long\def\SkipToFmtEnd#1\EndFmtInput{}%
  }\SkipToFmtEnd

\newcommand\ReadOnlyOnce[1]{\@ifundefined{#1}{\@namedef{#1}{}}\SkipToFmtEnd}
\usepackage{amstext}
\usepackage{amssymb}
\usepackage{stmaryrd}
\DeclareFontFamily{OT1}{cmtex}{}
\DeclareFontShape{OT1}{cmtex}{m}{n}
  {<5><6><7><8>cmtex8
   <9>cmtex9
   <10><10.95><12><14.4><17.28><20.74><24.88>cmtex10}{}
\DeclareFontShape{OT1}{cmtex}{m}{it}
  {<-> ssub * cmtt/m/it}{}
\newcommand{\texfamily}{\fontfamily{cmtex}\selectfont}
\DeclareFontShape{OT1}{cmtt}{bx}{n}
  {<5><6><7><8>cmtt8
   <9>cmbtt9
   <10><10.95><12><14.4><17.28><20.74><24.88>cmbtt10}{}
\DeclareFontShape{OT1}{cmtex}{bx}{n}
  {<-> ssub * cmtt/bx/n}{}
\newcommand{\tex}[1]{\text{\texfamily#1}}	% NEU

\newcommand{\Sp}{\hskip.33334em\relax}
\newcommand{\NB}{\textbf{NB}}
\newcommand{\Todo}[1]{$\langle$\textbf{To do:}~#1$\rangle$}

\EndFmtInput
\makeatother
%
\author{Piyush P Kurur\\
  Office no: 224\\
  Dept. of Comp. Sci. and Engg.\\
  IIT Kanpur}


\newcommand{\Token}[2][]{\node(#2#1)[token]{#2};}
\newcommand{\Symbol}[2][]{\node(#2#1)[symbol]{#2};}
\newcommand{\Point}[1]{\node(#1)[point]{#1};}


\title{Fundamentals of Computing: Lecture 7}
\date{August 10, 2009}
\begin{document}

\begin{frame}
\maketitle
\end{frame}

\begin{frame}
  \frametitle{Summary of last class}
  \pause
  \begin{itemize}
  \item We studied arrays,
    \pause
  \item Character type and String type (character array).
    \pause
  \item \text{\tt printf} and \text{\tt scanf}.
    \pause
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Functions}
  \begin{block}{Motivation}
    \pause
    \begin{itemize}
    \item Program fragments are used may times
      \pause
    \item Better organisation of code (refactoring).
    \end{itemize}
  \end{block}
  
  \pause

  \begin{block}{Function definition}
    \text{\tt type~function\char45{}name\char40{}arg1\char44{}arg2\char44{}\char46{}\char46{}\char46{}\char44{}argn\char41{}~statement\char45{}block}
    
    \pause
    eg.
    \begin{tabbing}\tt
~~~~~~~double~square\char40{}double~x\char41{}\\
\tt ~~~~~~~\char123{}\\
\tt ~~~~~~~~~return~x~\char42{}~x\char59{}\\
\tt ~~~~~~~\char125{}
\end{tabbing}
  \end{block}
\end{frame}

\begin{frame}
  \frametitle{A complete example}
  \begin{tabbing}\tt
~~~~\char35{}~include~\char60{}stdio\char46{}h\char62{}\\
\tt ~~~~~void~swap\char40{}int\char44{}~int\char41{}\char59{}~\char47{}\char42{}~declaration~\char42{}\char47{}\\
\tt ~~~~~int~main\char40{}\char41{}\\
\tt ~~~~~\char123{}\\
\tt ~~~~~~~int~x~\char61{}~15\char44{}~y~\char61{}~42\char59{}\\
\tt ~~~~~~~printf\char40{}\char34{}x~\char61{}~\char37{}d\char44{}~y~\char61{}~\char37{}d\char92{}n\char34{}\char44{}~x\char44{}~y\char41{}\char59{}\\
\tt ~~~~~~~swap\char40{}x\char44{}y\char41{}\char59{}\\
\tt ~~~~~~~printf\char40{}\char34{}x~\char61{}~\char37{}d\char44{}~y~\char61{}~\char37{}d\char92{}n\char34{}\char44{}~x\char44{}~y\char41{}\char59{}\\
\tt ~~~~~\char125{}\\
\tt ~~~~~\\
\tt ~~~~~void~swap\char40{}int~u\char44{}~int~v\char41{}\char47{}\char42{}~definition~\char42{}\char47{}\\
\tt ~~~~~\char123{}\\
\tt ~~~~~~~int~temp\char59{}\\
\tt ~~~~~~~temp~\char61{}~u\char59{}\\
\tt ~~~~~~~u~\char61{}~v\char59{}\\
\tt ~~~~~~~v~\char61{}~temp\char59{}\\
\tt ~~~~~~~return\char59{}\\
\tt ~~~~~\char125{}
\end{tabbing}
\end{frame}

\begin{frame}
  \frametitle{Argument passing scheme: Call by value}
  
  C follows the \emph{call by value} argument passing scheme.
  \pause
  \begin{itemize}
  \item Only values are passed to the arguments,
  \item Changes to parameters in the function does \emph{not} affect
    the callee
  \end{itemize}
  \pause
  \begin{block}{Call by reference}
    \pause
    \begin{itemize}
    \item C has only call by value.
      \pause
    \item Fortran has only call be reference.
      \pause
    \item Pascal and C++ has both call be value and reference.
      \pause
    \item Java as usual is muddled up. Basic values are call by value.
      Objects are a call by reference.
    \end{itemize}
  \end{block}

\end{frame}

\begin{frame}
  \frametitle{The swap function C++ version}
  \begin{tabbing}\tt
~~~~\char35{}~include~\char60{}stdio\char46{}h\char62{}\\
\tt ~~~~~void~swap\char40{}int~\char38{}\char44{}~int~\char38{}\char41{}\char59{}~\char47{}\char47{}~declaration\\
\tt ~~~~~int~main\char40{}\char41{}\\
\tt ~~~~~\char123{}\\
\tt ~~~~~~~int~x~\char61{}~15\char44{}~y~\char61{}~42\char59{}\\
\tt ~~~~~~~printf\char40{}\char34{}x~\char61{}~\char37{}d\char44{}~y~\char61{}~\char37{}d\char92{}n\char34{}\char44{}~x\char44{}~y\char41{}\char59{}\\
\tt ~~~~~~~swap\char40{}x\char44{}y\char41{}\char59{}\\
\tt ~~~~~~~printf\char40{}\char34{}x~\char61{}~\char37{}d\char44{}~y~\char61{}~\char37{}d\char92{}n\char34{}\char44{}~x\char44{}~y\char41{}\char59{}\\
\tt ~~~~~\char125{}\\
\tt ~~~~~void~swap\char40{}int~\char38{}u\char44{}~int~\char38{}v\char41{}~\char47{}\char47{}~definition\\
\tt ~~~~~\char123{}\\
\tt ~~~~~~~int~temp\char59{}\\
\tt ~~~~~~~temp~\char61{}~u\char59{}\\
\tt ~~~~~~~u~\char61{}~v\char59{}\\
\tt ~~~~~~~v~\char61{}~temp\char59{}\\
\tt ~~~~~~~return\char59{}\\
\tt ~~~~~\char125{}
\end{tabbing}
\end{frame}


\begin{frame}
\frametitle{The \text{\tt void} type}

\begin{itemize}
\item When the the function does not return any value.
\item In certain places where any other type does not make sense.
\end{itemize}

\begin{tabbing}\tt
~~~void~main\char40{}void\char41{}\\
\tt ~~~\char123{}\\
\tt ~~~~~printf\char40{}\char34{}hello~world\char92{}n\char34{}\char41{}\char59{}\\
\tt ~~~~~return\char59{}\\
\tt ~~~\char125{}
\end{tabbing}

\end{frame}

\begin{frame}
  \frametitle{Recursion}
  Functions can call other functions and even itself
  
  \begin{tabbing}\tt
~~~~~int~factorial\char40{}~int~n~\char41{}\\
\tt ~~~~~\char123{}\\
\tt ~~~~~~~if~\char40{}~n~\char60{}~2\char41{}\\
\tt ~~~~~~~\char123{}\\
\tt ~~~~~~~~~return~1\char59{}\\
\tt ~~~~~~~\char125{}\\
\tt ~~~~~~~else\\
\tt ~~~~~~~\char123{}\\
\tt ~~~~~~~~~return~n~\char42{}~factorial\char40{}~n~\char45{}~1\char41{}\char59{}\\
\tt ~~~~~~~\char125{}\\
\tt ~~~~~\char125{}
\end{tabbing}
\end{frame}

\begin{frame}
  \frametitle{The \text{\tt main} function}
  
  \begin{itemize}
  \item Every statement in a C program has to be part of some function,
    \pause
  \item The program execution starts by calling the \text{\tt main} function,
    \pause
  \item The return type of \text{\tt main} can be either \text{\tt int} or \text{\tt void},
    \pause
  \item If return type is \text{\tt int}, the return value is a way of indicating
    to the shell if the command has succeeded,
    \pause
  \item It is recommended that you declare \text{\tt main} with return type
    \text{\tt int} and return meaningful status message.
  \end{itemize}
  
\end{frame}



\end{document}





\begin{frame}
  \frametitle{How to design loops?}
  
  \begin{definition}
    An invarient of a loop is a condition that is
    true always during the execution of the loop.
  \end{definition}
  
  Eg.
  
  \begin{tabbing}\tt
~~~~~int~sum~\char61{}~0\char59{}\\
\tt ~~~~~int~i~~~\char61{}~0\char59{}\\
\tt ~~~~~while\char40{}~i~\char60{}~n\char41{}\\
\tt ~~~~~\char123{}\\
\tt ~~~~~~~i\char43{}\char43{}\char59{}\\
\tt ~~~~~~~sum~\char43{}\char61{}~i\char59{}\\
\tt ~~~~~\char125{}
\end{tabbing}

  A nice invarient is $sum = \sum_{j=0}^i i$.
  
\end{frame}



