Beruflich Dokumente
Kultur Dokumente
Lecture - 7
Prof. Sankhadeep Chatterjee
Department of Computer Science & Engineering, UEMK
Lecture - 7
Functions
References
• Byron Gottfried, Schaum's Outline of Programming with C,
McGraw-Hill (Chapter 7)
• Brian W. Kernighan and Dennis M. Ritchie, The C Programming
Language, Pearson
• C How to Program, 8th Ed. Deitel & Deitel, Pearson
• C Learning And Building Business And System Applications,
S.K. Rout, 2nd Ed. Prentice Hall of India
• Computer Fundamentals & C Programming, Sumitabha Das,
McGraw-Hill
• Programming in ANSI C, E. Balagurusamy, McGraw-Hill
Function
• A program segment that carries out some
specific, well-defined task
• Example
– A function to add two numbers
– A function to find the largest of n numbers
main()
void print_msg()
{ {
print_msg(); printf(“Hello”);
}
}
Functions contd.
• Calling function (caller) may pass information
to the called function (callee) as
parameters/arguments
– For example, the numbers to add
• The callee may return a single value to the
caller
– Some functions may not return anything
Function: Example
Calling Function (Caller) Called Function (Callee)
main()
{
fun();
printf(“%d”,a); Output: 1
}
Recursive Functions
• A function can also call itself, either directly or in
a cycle
– A calls B, B calls C, C calls back A.
• Factorial of a number
f(n) = n * f(n-1), n>0
= 0, n = 0
• Fibonacci Series
f(n) = f(n-1) + f(n-2), n>2
= 1, n = 2
= 0, n = 1
Thank You