Fundamentals of Computer Programming: Jehangir Arshad Meo
Fundamentals of Computer Programming: Jehangir Arshad Meo
Programming
1
Structured and unstructured programming:
A function is a block of code that has a name and it has a property that it is reusable i.e. it can
be executed from as many different points in a C Program as required.
Function groups a number of program statements into a unit and gives it a name.
A computer program cannot handle all the tasks by it self. Instead its requests other program
like entities – called functions in C – to get its tasks done. A function is a self contained block
of statements that perform a coherent task of same kind.
Types of functions:
• There are two types of functions:
Syntax:
Return type function-name (arguments);
Example: Int sum (int x, int y);
Structure of a Function
1. Function header
2. Function body
Example:
Function Header:
In the first line of the above code:
Function Body:
What ever is written with in { } in the above example is the body of the
function.
Function Prototypes:
• The prototype of a function provides the basic information about a
function which tells the compiler that the function is used correctly
or not.
• The only difference between the header and the prototype is the
semicolon (;) there must the a semicolon at the end of the
prototype.
Ordering of functions in a file
• The function either returns some value to the point it was called
from or returns nothing.
The function name must match exactly the name of the function in the function
prototype.
The args are a list of values (or variables containing values) that are "passed" into the
function.
#include<stdio.h>
Void main ()
{
Int kg, gm;
Int grams (int);
Printf (“please enter the weight in kilograms :”);
Scanf (“%d\n”, &kg);
gm= grams (kg);
Printf (“%d kilograms = %d gm”, kg,grams);
}
// Function
Int grams (int weight)
{
Return weight * 1000;
}
Arguments
Parameters appear in function
Pass by Value:
Pass by Value, means that a copy of the data is made and stored by way of the name of the
parameter. Any changes to the parameter have NOaffect on data in the calling function.
Pass by Reference:
A reference parameter "refers" to the original data in the calling function. Thus any changes made to the parameter are ALSO MADE TO THE
ORIGINAL variable.
To make a normal parameter into a pass by reference parameter, we use the "& param"
notation. The ampersand (&) is the syntax to tell C that any changes made to the parameter
also modify the original variable containing the data.
Comparision:
// C function using pass by value. // C using pass Reference Parameter!
(Notice no &)
void doit( int & x )
void doit( int x )
{
{ x = 5;
x= 5; }
} // // Test code for passing by a variable by reference
// Test function for passing by value (i.e., int main()
making a copy) {
int z = 27;
int main()
{
doit( z );
int z = 27;
doit( z ); printf('z is now %d\n', z);
printf('z is now %d\n', z);
return 0; return 0;
}
}
Passing Arrays to Functions
int myArray[ 24 ];
myFunction( myArray, 24 );
20
Local variables, their lifetime and scope:
• Local variables are those variable which are declared inside the
main function or inside the user defined function. These variables
are also called automatic variables. The keyword auto can be used
to declare these variables.
auto int a , b , c; //using auto is optional
• Time from creation till the destruction of that variable is called life
time of the variable. These variables are temporarily created in the
memory and destructed after use.
1- a , b and s are declared in the main function so these are local to main function.
2- x, y and (rs) are also local variables for sum function. As the sum called control transfer to
the sum and passed values of a, and b will be stored in x and y and then there sum will be
stored and returned in (rs) and stored in sum.
Global variables, their lifetime and scope:
• Global variables are those variable which are declared
outside the main function or outside the user defined
function. These variables are also called external variables.
These variables can be used in any function of the program.
1- a , b and s are declared out of the main function so these are global for all functions.
2- x, y are local variables for sum function. As the sum called control transfer to
the sum and passed values of a, and b will be stored in x and y and then there sum will be
stored and returned in (s). Now s can be directly used to print the sum
Recursive Functions: