1. C Programming
Language
By:
Yogendra Pal
yogendra@learnbywatch.com
Dedicated to My mother and Father
2. t
y
Keep your notebook with you.
Write important point and questions that comes in your mind
Solve Mind band exercise.
C
Rewind when not clear
Ask Questions by call or SMS or by mail
Keep Watching Keep Learning
THIS IS FUNCTIONS
2
3. Function
• Allow us to group commonly used code into a
compact unit that can be used repeatedly.
• main() function is must in each program.
• Other functions call directly or indirectly from
main().
4. Need
• Consider a problem:
– Write a program that take 3 numbers x, y, z as input
from user and calculate and print x5+y5+z5
5. General form
Return_type function_name(parameters)
{
statements;
return value;
}
• Return type : any data type.
• Function name : used to call the function.
• Parameters : zero or more variables.
6. An Example
int square(int num)
{
return num*num;
}
• This function accept an integer value and return its
square.
7. Function-Call
• Pass function_name and Parameters.
• Function performs a specific task that is given
to it.
• Result will return.
• Default return type is int.
• If you want to return noting use void.
10. More about function
• All variable defined inside functions are local
and known only in function defined.
• Parameters are used to communicate between
functions.
11. Why use functions?
• Make your problem modularize.
• Increase reusability.
• Avoid code repetition.
12. Function Prototype
• Used to validate function.
• Prototype only needed if function definition
comes after use in program.
– Ex: int maximum(int, int, int);
• Write a function that return the maximum value
among three integers.
13. Problems
• Write a function to calculate the factorial value
of an integer entered through the keyboard.
• Write a function power(a, b), to calculate the
value of ab
• Any year is entered through the keyboard.
Write a function to determine whether the
year is a leap or not.
14. Global Variable
• Global variables are defined outside of any
function.
• Available to many functions.
• Functions are always external.
• C does not allow functions to be defined inside
other functions.
• Any function may access an external variable
by referring to it by name.
14
15. Scope Rules
• File scope
– Identifier defined outside function, known in all
functions.
– Used for global variables, function definitions,
function prototypes.
• Function scope
– Can only be referenced inside a function body
– Used only for labels (start:, case: , etc.)
16. Scope Rules
• Block scope
– Identifier declared inside a block
• Block scope begins at definition, ends at right brace
– Used for variables, function parameters (local
variables of function)
– Outer blocks "hidden" from inner blocks if there is a
variable with the same name in the inner block
• Function prototype scope
– Used for identifiers in parameter list
17. Function calling
• Call by value
– Copy of arguments passed to function.
– Changes in function do not effect original.
– Use when function does not need to modify
arguments.
• Call by reference
– Passes original arguments
– Changes in function effect original variables.
18. Header Files
• Contain function prototypes for library
functions.
– Ex: conio.h, stdio.h, stdlib.h, math.h.
• Load with #include <filename> or
#include “filename”
19. Some standard header files
Standard Explanation
library header
<float.h> Contains the floating point size limits of the system.
<limits.h> Contains the integral size limits of the system.
<locale.h> Contains function prototypes and other information that enables a program to be
modified for the current locale on which it is running. The notion of locale enables
the computer system to handle different conventions for expressing data like dates,
times, dollar amounts and large numbers throughout the world.
<math.h> Contains function prototypes for math library functions.
<stdio.h> Contains function prototypes for the standard input/output library functions, and
information used by them.
<stdlib.h> Contains function prototypes for conversions of numbers to text and text to
numbers, memory allocation, random numbers, and other utility functions.
<string.h> Contains function prototypes for string processing functions.
<time.h> Contains function prototypes and types for manipulating the time and date.
19
20. Custom header files
• Create header file with function.
• Save filename.h
• Load in other files with #include “filename.h”.
• Reuse function
21. Recursion
• Functions that call themselves.
• Function launches a new copy of itself.
• Ex:
– Calculate factorial using recursion.
22. Mind Bend
• Write a function table(int num) that print the
table of the number that is passed to it.
• Write a function table_atob(int a, int b) that
accept two numbers a and b as an input and
call table(int num) print the tables from a to b.
• Write a function that validate the input a and b
entered in the function table_atob(int a, int b).
22
23. To get complete benefit of this tutorial solve all the quiz on
www.learnbywatch.com
For any problem in this tutorial mail me at
yogendra@learnbywatch.com
with the subject “C”
For Other information mail at
info@learnbywatch.com
Keep Watching Keep Learning
NEXT IS ARRAYS
23