Saturday, November 29, 2008

Structure and function...

For the continuation of our lesson in structure and function of programming...

I also learned about the following:

<#include

  • sqrt(x)
  • fabs(x) - calculates the absolute value of a number
  • ceil(x) - ceil (11.25)=12
  • floor(x) - floor(11.25)=11
  • sin(x)
  • cos(x)
  • tan(x)
  • pow(x)

C Program Structure

A C program basically has the following form:

  • Preprocessor Commands
  • Type definitions
  • Function prototypes -- declare function types and variables passed to function.
  • Variables
  • Functions

We must have a main() function.


A function has the form:


type function_name (parameters)
{
local variables

C Statements

}

If the type definition is omitted C assumes that function returns an integer type. NOTE: This can be a source of problems in a program.

So returning to our first C program:


/* Sample program */

main()
{

printf( ``I Like C \n'' );
exit ( 0 );

}
NOTE:
  • C requires a semicolon at the end of every statement.
  • printf is a standard C function -- called from main.
  • \n signifies newline. Formatted output -- more later.
  • exit() is also a standard function that causes the program to terminate. Strictly speaking it is not needed here as it is the last line of main() and the program will terminate anyway.

Let us look at another printing statement:
printf(``.\n.1\n..2\n...3\n'');

The output of this would be:


.
.1
..2
...3




For What???

C++ Structure

What is a Structure?

Structure is a collection of variables under a single name. Variables can be of any type: int, float, char etc. The main difference between structure and array is that arrays are collections of the same data type and structure is a collection of variables under a single name.


How to declare and create a Structure

Declaring a Structure:

The structure is declared by using the keyword struct followed by structure name, also called a tag. Then the structure members (variables) are defined with their type and variable names inside the open and close braces { and }. Finally, the closed braces end with a semicolon denoted as ; following the statement. The above structure declaration is also called a Structure Specifier.

Example:


Three variables: custnum of type int, salary of type int, commission of type float are structure members and the structure name is Customer. This structure is declared as follows:



In the above example, it is seen that variables of different types such as int and float are grouped in a single structure name Customer.

Arrays behave in the same way, declaring structures does not mean that memory is allocated. Structure declaration gives a skeleton or template for the structure.

After declaring the structure, the next step is to define a structure variable.


How to declare Structure Variable?

This is similar to variable declaration. For variable declaration, data type is defined followed by variable name. For structure variable declaration, the data type is the name of the structure followed by the structure variable name.

In the above example, structure variable cust1 is defined as:



What happens when this is defined? When structure is defined, it allocates or reserves space in memory. The memory space allocated will be cumulative of all defined structure members. In the above example, there are 3 structure members: custnum, salary and commission. Of these, two are of type in and one is of type float. If integer space allocated by a system is 2 bytes and float four bytes the above would allo9acter 2bytes for custnum, 2 bytes for salary and 4 bytes for commission.

For example:

A programmer wants to assign 2000 for the structure member salary in the above example of structure Customer with structure variable cust1 this is written as:


chapter 10_con't

This blog is the continuation of my last blog. . .

This is my additional learnings. . .

Type of functions

Void Functions – which does not return any value when invoked.

Function that returns a value once invoked.

Actual and Formal Parameters

Actual Parameters are the variables found in the function call whose values will be passed to the formal parameters of the called function.

Formal Parameters are the variables found in the function header that will receive from the actual parameters.


That's all. . .

Sunday, November 23, 2008

Chapter 10_discussion

In our discussion this week, we are finished with our lesson about looping! And we are now in Chapter 10 which is about the Functions and Structured Programming This includes the following facts:

- In functions and structured programming, it is a C Program that is composed at least one function which is the main() function and it executes the program that begins with main() and also ends with the main() function. It is also composed of other functions aside from main() function.

- The functions are defined as the “building blocks of C” in which all program activity occurs. It is also called as a subprogram or subroutine. It performs a task, operation or computation then may return to the calling part of the program. Other functions can be executed by the program through a “function call”. This function call is used to call a function to execute C statements found inside the function body.

The General form of a Function

function_type function_name (parameters list)

{

body of the function;

}

Where:

= function_type specifies the type of value that the function will return.

= function_name is any valid identifier name which will name the function.

= Parameters list is a comma separated list of variables that receive the values when the function is called.

= body of the function is composed of valid c statements that the function will execute.

- The types of functions include the Void Functions which does not return any value when invoked and the Functions that return a value once invoked.

- In parameters list, there are two kinds; the actual parameters are the variables found in the function call whose values will be passed to the formal parameters of the called functions and the Formal Parameters are the variables found in the function header that will receive from the actual parameters.

Call by Value – in this method, the values of the actual parameters are passed to the formal parameters. The changes that will happen to the values of the formal parameters inside the function will not affect the values of the actual parameters.

Pass by Value – in this method, the actual parameters pass their value to the formal parameters. The changes that will happen to the values of the formal parameters inside the function will affect the values of the actual parameters.

<#include

= sqrt(x)

= fabs(x) - calculates the absolute value of a number

= ceil(x) - ceil (11.25)=12

= floor(x) - floor(11.25)=11

= sin(x)

= cos(x)

= tan(x)

= pow(x,y)


This are one of the facts that is important in this chapter! ♥

My Learnings of the Week

For this week we've taking up about FUNCTIONS AND STRUCTURED PROGRAMMING...

  • A c program is composed of at least one function definition, that is the main() function.
  • Execution of the program begins with main() and also ends with the main() function.
  • However, a C program can also be composed of other functions aside from the main().
  • The c program presented in previous slide is composed of 3 functions: the main function, the function greet1 and the function greet2.
  • Therefore we can say that we can create a program that is composed of other function aside from the main function.

Example:

#include

main()

{
clrscr( );

printf (“Hi”);

greet1( );

greet2( );

getch( );

}

greet1 ( )

{

printf ( “Hello”);

}

greet2 ( )

{

printf (“How are you”);

}

Output:

Hi! Hello! How are you?

Functions

  • Functions are the building blocks of C in which all program activity occurs.
  • A function is also called a subprogram or subroutine. It is a part of a C program that performs a task, operation or computation then may return to the calling part of the program.
  • Other functions aside from the main( ) can only be executed by the program through a “function call”.
  • Note: Function call is a C statement that is used to call a function to execute C statements found inside the function body.
  • Going back to the example, greet1( ); is an example of a function call, calling the function greet ( ).

Type of Functions

  • Void Functions – which does not return any value when invoked.
  • Function that returns a value once invoked.

Actual and Formal Parameters

  • Actual Parameters are the variables found in the function call whose values will be passed to the formal parameters of the called function.
  • Formal Parameters are the variables found in the function header that will receive from the actual parameters.

Call by value

  • In the method call by value, the values of the actual parameters are passed to the formal parameters.
  • Changes that happen to the values of the formal parameters inside the function will not affect the values of the actual parameters.

Pass by value or Call by reference

  • The actual parameters also pass their value to the formal parameters.
  • But the changes that happen to the values of the formal parameters inside the function will affect the values of the actual parameters.
  • This is because the actual address of the variables is passed using the address of operator (&) together with the pointer operator (*).