Você está na página 1de 6

ESc101: Fundamentals of Computing

2011-12-Monsoon Semester Lecture #13, August 25, 2011

Please switch off your mobile phones.

Announcements

Monday lab scheduled on 22nd August will instead be held on Saturday, 27th August from 2:00 PM to 5:00 PM. Wednesday lab scheduled on 31st August will instead be held on Saturday, 3rd September from 2:00 PM to 5:00 PM. Quiz on lab days from 5th to 9th September in Lab at 2:00 PM.

Lec-13

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap
Arrays y Two dimensional arrays

Lec-13

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Functions
Function is a part of the program that carries out a specific, specific well-defined task. A function has some inputs, better known as arguments and an output, better known as result or return value A program may consist of any number of functions. Any function can call (or use) any other function. We have been writing only one function so far (called, main).
We have used functions, such as, printf, scanf, sqrt, etc.
Lec-13 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 3

Why Use Functions


Some parts of the code are repeated. repeated
It is best to put such code in one place and re-use (call) it as many times from as many places as is needed.
Will ensure that if there is an error or a change is needed in the code, then that change needs to be done only at one place. Hence, improves reliability of the program.

Modular code
Easy to understand
It is easy to understand 5 programs of 20 lines each rather than one program of 100 lines.

Lec-13

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Function - Syntax
<return-type> Function-name (type1 arg1, type2 arg2, , typen argn) Function-name is the name of the function.
This name is needed to use or call this piece of program. We have to follow the same naming convention as for any variable.

Function can have any number of arguments


Each argument is a variable (memory area) whose value is set during the function call. Each argument is of a specific type (such as int, char, float, etc.) int char float etc )

Function carries out some activity and has a result, which is returned to the caller of this function.
The type of this result has to be specified.
Lec-13 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 5

Function (contd.)
Functions are called from other functions as:
variable = function_name (argument list) i bl f ti ( t li t)

Example:
int sum (int a, int b) { return (a + b); } z = sum (x, y);
Lec-13

// Function definition // Return the result

// Calling the function


Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 6

Functions (contd.)
It is not necessary for functions to return anything. anything
In such cases, the return type can be void

Any type of argument can be used


Even arrays can be arguments

Function body may contain any valid C code

Lec-13

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Function to add two integers: Complete Program


#include <stdio.h> int main () { int i t x, y, z; scanf (%d, &x); scanf (%d, &y); z = sum (x, y); printf (%d\n, z); } int sum (int a, int b) { int c; c = a + b; return c; }
Lec-13

// Include standard I/O functions // main function

// Read an integer and store it in x // Read an integer and store it in y // Call sum function with parameters x, y // Print z, which stores the return value of sum // Function declaration two integer arguments // Return value is also an integer R t l i l i t // Need some memory area in this function // Compute the sum // Return the sum

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Parameters and Arguments


Function is called with parameters. Parameters can be any expression. expression These expressions are evaluated before calling the function The values of these parameters are stored in memory areas called arguments. Parameters can be variables, but need not be variables. Even if parameters are variables, their values are not changed when value of arguments is changed inside the function. Arguments are local memory areas of the function.

Lec-13

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Swap: A Function that will not work


#include <stdio.h> int main () { int x = 10, y = 43; 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) { int temp; temp = u; u = v; v = temp; return; }
Lec-13

// Print initial values of x and y // Expectation is that x and y will be swapped // If x and y are swapped, opposite values // should be printed, but does not happen.

// Swap u and v. // Will not swap x and y.

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

10

Parameter Passing
C follows call by value parameter passing scheme:
Only values are passed to the arguments Changes to arguments in the function do not affect the callee

We will, later, find a way to ensure that changes in the function arguments are reflected in the parameters at callee.

Lec-13

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

11

Você também pode gostar