Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

C

WEBINAR
COACHING
BY APTUTS.COM
EMAIL: LEARN@APTUTS.COM

ABOUT ME
Bachelor's Degree in Computer Science Engineering
Software Engineer in IT Industry & Teaching Experience
7 Years of IT experience in various technologies
Expertise in C, C++, Application Development, Manual Testing, Web Automation Testing, Mobile
Automation Testing, Website Designing, PHP and VBScript
Founder of LearnCOnline.com, LearnCPPOnline.com and Aptuts.com
Contact Info: prashant@aptuts.com

COURSE CURRICULUM
Following topics would be covered:
Overview
Fundamentals and Control Statements
Data Input and Output in C
Functions and Function Overloading

Arrays, Structures and Pointers


Strings and String handling functions
Storage Classes in C
Introduction to File Operations in C

The C Pre-processor Directives

DEFINING OF FUNCTIONS
A function definition has two principal components:
1.

the first line (including the argument declaration), and

2.

the body of the function

In general, the first line can be written as:


data-type function_name(type 1 arg 1, type 2 arg 2, ..., type n arg n)
where,
data-type represents the data type of the item that is returned by the function,
function_name represents the function name, and
type 1, type 2, , type n represents the data-type of the arguments arg 1, arg 2, , arg n.
The data-types are assumed to be of the type int if they are not shown explicitly. However, the omission of the
data type is considered poor programming practice, even if the data items are integers.
The arguments are called formal arguments, because they represent the names of the data items that are
transferred into the function from the calling portion of the program. The names of the formal arguments
need not be the same as the names of the actual arguments in the calling function of the program. However,
each formal argument must be of the same data type, as the data item it receives from the calling function of
the program.

DEFINING OF FUNCTIONS (CONDT...)


The remainder of the function definition is a compound statement that defines the action to be taken
by the function. This compound statement is referred to as the body of the function. Like any
other compound statement, this statement can contain expression statements, other compound
statements, control statements, and so on.
It should include one or more return statements, in order to return a value to the calling function of
the program.
Example of Function Definition:

int addnumbers(int a, int b)


{
//body of the function enclosed in curly braces
int c;

c = a + b;
return c;
}

ACCESSING OF FUNCTIONS
A function can be accessed (i.e., called) by specifying its name, followed by a list of arguments enclosed
in parentheses and separated by commas as shown below.
function_name(int a, int b);
If the function call does not require any arguments, an empty pair of parentheses must follow the
name of the function as shown below.
function_name();
The function call may be a part of a single expression (such as an assignment statement), or it may be
one of the operands within the more complex expression as shown below.
int returnedvalue = function_name();
int c = 3 * function_name() + 5;
The arguments appearing in the function call are referred to as actual arguments, in contrast to the
formal arguments that appear in the first line of the function definition.
In normal function call, there will be one actual argument for each formal argument. The actual
arguments may be expressed as constants, single variables or more complex expressions.
However, each actual arguments must be of the same data type as its corresponding formal
argument.

ACCESSING OF FUNCTIONS (CONDT...)


Consider the below function definition:
int addnumbers(int a, int b)
{
//body of the function enclosed in curly braces
int c;

c = a + b;
return c;
}
We can access the above defined function as shown below:

int returnedvalue = addnumbers(2, 4);


In the above function call, values 2 and 3 are called actual arguments.

FUNCTION PROTOTYPES
All the functions in C (which are used first and defined later in the program) need to be prototyped.
Function prototypes are usually written at the beginning of the program, ahead of any programmer-defined functions (including main()).
The general form of a function prototype is:
data-type function_name(type 1 arg 1, type 2 arg 2, ..., type n arg n);
Where,
data-type represents the data-type of the item that is returned by the function,
function_name represents the function name, and
type 1, type 2, , type n represents the data-type of the arguments arg 1, arg 2, , arg n.
Notice that a function prototype resembles the first line of a function definition (though a function prototype ends with a semicolon).
Use of function prototypes in C offers following advantages:
Prototypes enables the compilers to provide stronger type checking
Because of the use of prototypes, the compiler can find and report any questionable type conversions between the arguments used to
call a function and the types of its (function;s) parameters
Because of the use of prototypes, the compiler can also catch differences between the number of arguments used to call a function and
the number of parameters in the functions
Function prototypes help to trap bugs before they occur
Function prototypes help to verify that the program is working correctly by not allowing functions to be called with mismatched
arguments

DEFAULT ARGUMENTS IN FUNCTIONS


In C, if a function is defined to receive 2 arguments, whenever we call this function we have to pass 2 values to this function. If we pass one
value then some garbage value is assumed for the last argument. As against this, functions in C++ have an ability to define default
values for arguments that are not passed when the function call is made. The default arguments are given only in the function
prototype and should not be repeated in the function definition.The compiler uses the prototype information to build a
call, not the function definition.
Here is an example of a prototype (i.e., function declaration) with default values:
float amount(float principal, int period, float rate = 0.15);
The default value is specified in a manner syntactically similar to the variable initialization. The above prototype declares a default value of
0.15 to the argument rate.
A subsequent function call like:
value = amount(5000, 7);

//one argument missing

passes the value 5000 to principal and 7 to period and then lets the function use the default value of 0.15 for rate.
One more important point to note here is that only the trailing arguments can have default values.That is, we must add defaults
from right to left. We cannot provide a default values to a particular argument in the middle of the argument list.
Hence, the prototypes:
float amount(float principal = 5000, int period = 7, float rate);
float amount(float principal = 5000, int period, float rate = 0.15);
are invalid as they does not add defaults from right to left.

ARRAYS
An array can be defined as a sequence of data in memory, wherein all the data are of
the same type, and are placed in physically adjacent locations.

An array is a group of elements that share a common name, and that are differentiated from one
another by their positions within the array.
The position of each of these elements can be indicated by
means of a index as:
x[0] = 65;
x[1] = 33;
x[2] = 64;
x[3] = 88;
x[4] = 5;

Also, an array must be declared, since it is a type of variable. An array containing five elements, all
of which are integers, can be declared as follows:
int x[5];

ARRAYS (CONDT...)
Defining of 1-Dimensional Arrays in C++
In general terms, a 1-Dimensional array definition may be represented as:
storage-class data-type array[expression];
Where storage-class refers to the storage class of the array,
data-type is the data type,

array is the array name, and


expression is a positive-valued integer expression which indicates the number of array elements.
The storage-class is optional; default values are automatic for arrays that are defined within a
function or a block, and external for arrays that are defined outside of a function.

ARRAYS (CONDT...)
Defining of Multi-Dimensional Arrays in C++
In general terms, Multi-Dimensional array definition may be represented as:
storage-class data-type array[expression 1][expression 2]...[expression n];
Where storage-class refers to the storage class of the array,
data-type is the data type,

array is the array name, and


expression 1, expression 2,..., expression n are positive-valued integer expression that indicates
the number of array elements associated with each subscript.

Remember that the storage-class is optional; default values are automatic for arrays that are
defined within a function or a block, and external for arrays that are defined outside of a
function.

ARRAYS (CONDT...)
Defining of Multi-Dimensional Arrays in C
In general terms, Multi-Dimensional array definition may be represented as:
storage-class data-type array[expression 1][expression 2]...[expression n];
Where storage-class refers to the storage class of the array,
data-type is the data type,

array is the array name, and


expression 1, expression 2,..., expression n are positive-valued integer expression that indicates
the number of array elements associated with each subscript.

Remember that the storage-class is optional; default values are automatic for arrays that are
defined within a function or a block, and external for arrays that are defined outside of a
function.

ARRAYS (CONDT...)
Defining of Multi-Dimensional Arrays in C
In general terms, Multi-Dimensional array definition may be represented as:
storage-class data-type array[expression 1][expression 2]...[expression n];
Where storage-class refers to the storage class of the array,
data-type is the data type,

array is the array name, and


expression 1, expression 2,..., expression n are positive-valued integer expression that indicates
the number of array elements associated with each subscript.

Remember that the storage-class is optional; default values are automatic for arrays that are
defined within a function or a block, and external for arrays that are defined outside of a
function.

THANK YOU

Web: www.aptuts.com
Email: learn@aptuts.com
Social: www.facebook.com/aptuts

You might also like