\documentclass{beamer} %include lhs2TeX.fmt \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 |printf| and |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} |type function-name(arg1,arg2,...,argn) statement-block| \pause eg. \begin{spec} double square(double x) { return x * x; } \end{spec} \end{block} \end{frame} \begin{frame} \frametitle{A complete example} \begin{code} # include void swap(int, int); /* declaration */ int main() { int x = 15, y = 42; printf("x = %d, y = %d\n", x, y); swap(x,y); printf("x = %d, y = %d\n", x, y); } void swap(int u, int v)/* definition */ { int temp; temp = u; u = v; v = temp; return; } \end{code} \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{code} # include void swap(int &, int &); // declaration int main() { int x = 15, y = 42; printf("x = %d, y = %d\n", x, y); swap(x,y); printf("x = %d, y = %d\n", x, y); } void swap(int &u, int &v) // definition { int temp; temp = u; u = v; v = temp; return; } \end{code} \end{frame} \begin{frame} \frametitle{The |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{spec} void main(void) { printf("hello world\n"); return; } \end{spec} \end{frame} \begin{frame} \frametitle{Recursion} Functions can call other functions and even itself \begin{spec} int factorial( int n ) { if ( n < 2) { return 1; } else { return n * factorial( n - 1); } } \end{spec} \end{frame} \begin{frame} \frametitle{The |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 |main| function, \pause \item The return type of |main| can be either |int| or |void|, \pause \item If return type is |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 |main| with return type |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{spec} int sum = 0; int i = 0; while( i < n) { i++; sum += i; } \end{spec} A nice invarient is $sum = \sum_{j=0}^i i$. \end{frame}