0% found this document useful (0 votes)
31 views25 pages

Fundamentals of Computer Programming: Jehangir Arshad Meo

Structured and unstructured programming: - Unstructured programming involves writing a program as a single unit, while structured programming divides the program logic into smaller modules or structures that are combined. Functions: - Functions break programs into smaller, reusable pieces called building blocks. They avoid duplicating code and make programs easier to understand and modify. Function declaration: - A function declaration specifies the name, return type, arguments, etc. of a function to tell the compiler how it is used correctly. It includes the function name, return type, and number/types of arguments. Local variables: - Local variables declared inside functions have a lifetime only within that function. They are automatically destroyed when the

Uploaded by

Alpha Romeo
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
31 views25 pages

Fundamentals of Computer Programming: Jehangir Arshad Meo

Structured and unstructured programming: - Unstructured programming involves writing a program as a single unit, while structured programming divides the program logic into smaller modules or structures that are combined. Functions: - Functions break programs into smaller, reusable pieces called building blocks. They avoid duplicating code and make programs easier to understand and modify. Function declaration: - A function declaration specifies the name, return type, arguments, etc. of a function to tell the compiler how it is used correctly. It includes the function name, return type, and number/types of arguments. Local variables: - Local variables declared inside functions have a lifetime only within that function. They are automatically destroyed when the

Uploaded by

Alpha Romeo
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 25

Fundamentals of Computer

Programming

Jehangir Arshad Meo

COMSATS Institute of Information Technology, Sahiwal

1
Structured and unstructured programming:

• The style of writing a program in which whole program


consists of a single unit is called unstructured
programming.

• In unstructured programming the logic of the program


or whole problem is divided in to smaller structures or
modules.
These small modules are combined to make a single
program.
Functions
 Functions are the building blocks of C programs.

 Each function is essentially a small program, with its own


declarations and statements.

 Using functions, programs are divided in to small pieces


that are easier for us as well as others to understand and
modify.
Why functions are used?
 Functions are used to avoid duplicating code that’s used more than once.

 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:

1. Built in functions : The functions that are already defined as part of


the language are called user built-in functions. These functions can
be used in any program. These are also called library functions.
These functions are defined in header files and to use these
functions respective header file should be included.
For example sqrt is used to calculate square root and it is defined in
math.h.

2. User-defined functions: The functions that are defined by the user


are called user-defined functions. These functions are used to
perform one task in the program
Function Declaration (Prototyping)
• Specifying the name, return type, arguments, etc of a function is
called function declaration (also called function prototype).

• Function is declared in the program in which it is to be used.


Following information should be provided by the function
declaration to the compiler:
Name of the function, Data type returned by the function and
the number and types of arguments or parameters used in the
function.

Syntax:
Return type function-name (arguments);
Example: Int sum (int x, int y);
Structure of a Function

There are two main parts of the function.

1. Function header
2. Function body

Example:

int sum(int x, int y)


{
int ans = 0; //holds the answer that will be returned
ans = x + y; //calculate the sum
return ans //return the answer
}
Function Header and body:

Function Header:
In the first line of the above code:

int sum(int x, int y)

It has three main parts:

1. The name of the function i.e. sum.


2. The parameters of the function enclosed in parenthesis x and y.
3. Return value type i.e. int.

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.

• It contains the same information as the function header contains.


The prototype of the function in the above example would be like

int sum (int x, int y);

• 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 order of functions inside a file is arbitrary.

• It does not matter if you put function one at


the top of the file and function two at the
bottom, or vice versa.
Defining Functions
return-type function-name ( parameters )
{
declarations
statements
}

•Functions may not return arrays;

•Specifying that the return type is void indicates that the


function doesn’t return a value
Example 1:
• The function that takes two integer values as arguments and find out the
maximum of the two?

int max(int a , int b)


{
if (a>b)
printf (“a is greater”);
else
printf(“b is greater”);
}
How functions are used?
• The name of the function is unique in a C Program and is Global.
It means that a function can be accessed from any location with
in a C Program. We pass information to the function called
arguments specified when the function is called.

• The function either returns some value to the point it was called
from or returns nothing.

• We can divide a long C program into small blocks which can


perform a certain task. A function is a self contained block of
statements that perform a coherent task of same kind.
Function call:
 Function can be called at any point in the program.

 Function call is a mechanism that is used to invoke or execute a function. Function is


invoked to perform the specific task for which it has been written. It is done by the
following syntax:
function_name ( args, ...);

 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.

 The number of args "passed" into a function must exactly match the number of


parameters required for the function. The type of each arg must exactly match the
type of each parameter. The return variable type must exactly match the return type of
the function.
Example 2:
• Write a function to convert kg(kilo grams) in to grams(g). Input the values into the main
function, convert it in to grams using the function and then print the weights in the grams?

#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

Definitions: They are dummy names that represent values to


be supplied when the function is called.

Arguments are expressions that appear in function calls.


Arguments --- passed by value
When a function is called, each argument is evaluated and its
value assigned to the corresponding parameter.

Each parameter behaves like a variable that’s been initialized


to the value of the matching argument.
Parameters in C functions
A Parameter is the symbolic name for "data" that goes into a function. There are two types of
parameters in C:

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.

There are two ways to make a pass by reference parameter:


1- ARRAYS
Arrays are always pass by reference in C. Any change made to the parameter containing the
array will change the value of the original array.

2- The ampersand used in the function prototype.


function ( & parameter_name )

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

• Specify name without brackets

– To pass array myArray to myFunction

int myArray[ 24 ];
myFunction( myArray, 24 );

– Array size usually passed, but not required


• Useful to iterate over all elements

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.

• Main function is the scope of these variables because these are


declared only for main function
Example:
#include<stdio.h>
main()
{
int a, b, s;
int sum(int, int);
a= 10;
b= 100;
s=sum(a,b);
printf(“sum = %d\n”, s);
}
int sum(int x, int y)
{
int rs;
rs = x + y;
return rs
}

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.

• Time from creation till the destruction of that variable is


called life time of the variable. These variables are
permanently stayed in the memory.

• Main function is the scope of these variables because these


are declared only for main function
Example:
#include<stdio.h>
int a, b, s;
main()
{
int sum(int, int);
a= 10;
b= 100;
sum (a, b);
printf(“sum = %d\n”, s);
}
int sum(int x, int y)
{
s = x + y;
}

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:

• The functions that called it self to perform a


specific task is called recursive functions.

You might also like