0% found this document useful (0 votes)
5 views

C Programming Week6

The document discusses the concept of functions in programming, emphasizing their role in modular programming and the benefits of breaking down tasks into smaller functions. It covers various types of functions, including recursive functions, and explains how to define and call functions in C programming. Additionally, it addresses memory management and the differences between call by value and call by reference in function arguments.

Uploaded by

ABHISHEK GOUTAM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

C Programming Week6

The document discusses the concept of functions in programming, emphasizing their role in modular programming and the benefits of breaking down tasks into smaller functions. It covers various types of functions, including recursive functions, and explains how to define and call functions in C programming. Additionally, it addresses memory management and the differences between call by value and call by reference in function arguments.

Uploaded by

ABHISHEK GOUTAM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

04/10/17

Functions = Outsourcing
• Break large computing tasks into small ones
• Helps you to build on what others have done
– You and others write functions
CS1100
– When you want to build a program, find out how to
Introduction to Programming use the function and use it
• Using standard functions provided by the library
Functions
– You are hidden from the implementation
Madhu Mutyam – Example – you don’t have to worry about how
Department of Computer Science and Engineering
Indian Institute of Technology Madras pow(m, n) is implemented
• As engineers from different disciplines you will
Course Material – SD, SB, PSK, NSN, DK, TAG – CS&E, IIT M 1
use and develop different set of functions
SD, PSK, NSN, DK, TAG – CS&E, IIT M 2

Modular Programming Example of Function Sets


• Subprograms • String manipulation
– functions in C, C++, procedures and functions in Pascal • Mathematical
– facilitate modular programming • Finite Element Method
• Overall task is divided into modules
– Used in structural analysis by Mechanical, Civil,
• Each module - a collection of subprograms
Aero, etc. for stress calculations etc.
– a subprogram may be invoked at several points
• A commonly used computation
• Most function libraries cost a lot
– hiding the implementation – Business opportunity – identify functions that are
useful to your area of study, create libraries
– changes can be incorporated easily
• Functions for use in different software
– Say, functions for web services
SD, PSK, NSN, DK, TAG – CS&E, IIT M 3 SD, PSK, NSN, DK, TAG – CS&E, IIT M 4

Power Function Calling Power Function with i=3


#include <stdio.h> function prototype printf(“%d %d %d\n”, i, power(3,i), power(-4,i));
int power (int, int); Computes the nth power of
main( ) { base (1st parameter) -64
for ( int i = 0; i < 20; i ++ )
27
printf(“%d %d %d\n”, i, power(3,i), power(-4,i));
}
int power (int base, int n) { int power (int base, int n){ int power (int base, int n){
int i, p = 1; Invocation with int i, p = 1; int i, p = 1;
arguments for ( i = 1; i <= n ; i ++) for ( i = 1; i <= n ; i ++)
for ( i = 1; i <= n ; i ++) A block
p = p * base; p = p * base; p = p * base;
return p; return p; return p;
} } }
SD, PSK, NSN, DK, TAG – CS&E, IIT M 5 SD, PSK, NSN, DK, TAG – CS&E, IIT M 6

1
04/10/17

Recursive Function Example Recursive Function Example


power(3, 13) return 1594323
int power (int num, int exp) {
int p; return power(3, 6)*power(3,6)*3 return 729*729*3
The base case exp = 1
if (exp = = 1) return num;
Guarantees termination
p = power(num, exp/2); return power(3, 3)*power(3,3) return 27*27
if (exp % 2 = = 0) return p*p;
else return p*p*num;} return power(3, 1)*power(3,1)*3 return 3*3*3

return 3

SD, PSK, NSN, DK, TAG – CS&E, IIT M 7 SD, PSK, NSN, DK, TAG – CS&E, IIT M 8

Factorial (n) Factorial (n) – Recursive Program


n! = 1 * 2 * 3 * .... * (n-2) * (n-1) * n n! = n * (n-1)!
Iterative version
In practice int may int fact(int n)
not be enough! {
int fact(int n){
if (n == 0) return(1);
int i;
return (n*fact(n - 1));
int result;
result = 1; }
for (i = 1; i <= n; i++)
• Shorter, simpler to understand
result = result * i;
• Uses fewer variables
return result;
• Machine has to do more work running this one!
}
SD, PSK, NSN, DK, TAG – CS&E, IIT M 9 SD, PSK, NSN, DK, TAG – CS&E, IIT M 10

Basics Basics
• Function is a part of your program • Transfer of control is affected by calling a
– It cannot be a part of any other function function
– main( ) is a function: it is the main function – With a function call, we pass some parameters
• Execution starts there or the control flow starts there – These parameters are used within the function
– From there it can flow from one function to another, – A value is computed
return after a computation with some values, probably, – The value is returned to the function which initiated
and then flow on the call
– The calling function can ignore the value returned
– It could use it in some other computation
– A function could call itself, these are called recursive
function calls
SD, PSK, NSN, DK, TAG – CS&E, IIT M 11 SD, PSK, NSN, DK, TAG – CS&E, IIT M 12

2
04/10/17

Add Functions to Your Program Features


• A program is a set of variables, and assignments • C program -- a collection of functions
to variables – function main ( ) - mandatory - program starts here.
• Now we add functions to it • C is not a block structured language
– Set of variables – a function cannot be defined inside another function
– Some functions including main( ) – only variables can be defined in functions / blocks
– Communicating values to each other • Variables can be defined outside of all functions
– Computing and returning values for each other – global variables - accessible to all functions
• Instead of one long program, we now write a – a means of sharing data between functions - caution
structured program composed of functions • Recursion is possible
– a function can call itself - directly or indirectly
SD, PSK, NSN, DK, TAG – CS&E, IIT M 13 SD, PSK, NSN, DK, TAG – CS&E, IIT M 14

Function – General Form Function Definition in C


• return-type function-name (argument declarations)
return-type function-name (argument declarations) {variable/constant declarations and statements}
{ • Arguments or parameters: No function
declarations here!
declaration and statements – the means of giving input to the function
return expression; – type and name of arguments are declared
• names are formal - local to the function
}
• Return value: for giving the output value
Matching the
– return ( expression ); -- optional number and type
• To invoke a function of arguments

function-name(exp1,exp2,…,expn);
SD, PSK, NSN, DK, TAG – CS&E, IIT M 15 SD, PSK, NSN, DK, TAG – CS&E, IIT M 16

Function Prototype More on Functions


• Used by the compiler to check the usage • To write a program
– prevents execution-time errors – You could create one file with all the functions
• Defines – You could/are encouraged to identify different modules
– the number of parameters, type of each parameter, and write functions for each module in a different file
– type of the return value of a function – Each module will have a separate associated header file
with the variable declaration global to that module
• Ex: function prototype of power function:
– You could compile each module separately and a .o file
int power ( int, int ); will be created
– no need for naming the parameters – You can then cc the different .o files and get an a.out
• Function prototypes are given in the beginning file
– This helps you to debug each module separately
SD, PSK, NSN, DK, TAG – CS&E, IIT M 17 SD, PSK, NSN, DK, TAG – CS&E, IIT M 18

3
04/10/17

Running with Less Memory Call by Value


• Functions • In C, function arguments are passed “by value”
– Provided to break up our problem into more basic – values of the arguments given to the called function
units in temporary variables rather than the originals
– Control flow – flows from function to function, – the modifications to the parameter variables do not
saving the current context, changing contexts, then affect the variables in the calling function
returning….. • “Call by reference”
– Helps the program to run with lesser memory, but – variables are passed by reference
slightly slower than a monolithic program without • subject to modification by the function
functions
– achieved by passing the “address of” variables
• Typically functions communicate using the
arguments and return values
SD, PSK, NSN, DK, TAG – CS&E, IIT M 19 SD, PSK, NSN, DK, TAG – CS&E, IIT M 20

Call by Value – An Example Call by Reference


main( ) { #include <stdio.h>
int p = 1, q = 2, r = 3, s; Function prototype void quoRem(int, int, int*, int*); /*addresses or pointers*/
int test(int, int, int); main( ){
Function call
int x, y, quo, rem; Passing
…; addresses
scanf(“%d%d”, &x, &y);
s = test (p, q, r); … /* s is assigned 9 */
quoRem(x, y, &quo, &rem);
} /* p,q,r don’t change, only their copies do */
printf(“%d %d”, quo , rem);
Does not return
Function definition }
int test( int a, int b, int c){ anything
a ++; b ++; c ++;
return (a + b + c); void quoRem(int num, int den, int* quoAdr, int* remAdr){
} *quoAdr = num / den; *remAdr = num % den;
}
SD, PSK, NSN, DK, TAG – CS&E, IIT M 21 SD, PSK, NSN, DK, TAG – CS&E, IIT M 22

int fact(int n)
Pending Computations {
if (n == 1) return 1;
Tail Recursion
• In this recursive version the calling }
return n * fact(n - 1);

version still has pending work after int fact(n)


{ return fact_aux(n, 1); }
it gets the return value.
(fact 4)
4 * (fact 3) Auxiliary variable The recursive call is
in the return
3 * (fact 2)
It needs to save int fact_aux(int n, int result) statement. The
2 * (fact 1) function simply
some values for { returns what it gets
future use 1
if (n == 1) return result; from the call it
2*1 =2 makes. The calling
return fact_aux(n - 1, n * result) version does not
3*2 = 6
} have to save any
4*6 = 24 values!

SD, PSK, NSN, DK, TAG – CS&E, IIT M 23 SD, PSK, NSN, DK, TAG – CS&E, IIT M 24

You might also like