17 May 2012

Functions in C (Continued)

If you want to read the previous posts about functions here are the links:
Use of Functions
Coding Functions in 'C'

It is possible to send a list of arguments to a function:

DoCalcs(Num1, Num2, Num3)
The function header must match this list exactly:
Begin DoCalcs(argNum1, argNum2, argNum3)

There is a complication to this process. It is possible to pass the variables By Value or By Reference. There is no need to indicate which method is being used when you write your pseudocode; It is only relevant when you write your 'C code.

If the variable is passed By Value, then the function

11 May 2012

Coding Functions in 'C'

In order to create and use a function in 'C', the programmer must provide three things which have matching descriptions:

  1. A function prototype - describes the structure of the function to the compiler.
  2. A function call - passes program control to the function.
  3. A function header - begins the program code of the function.
Function Prototype

A prototype shows the name of the function

Use of Functions

Complex problems can usually be broken down into a series of less complex problems, which may themselves be broken down, and so on.
Program design techniques (and programming languages) can accommodate this program approach, to allow the programmer to think about a problem in manageable chunks.

Consider the following pseudocode:
Begin
    Display "Triangle Area Calculator"
    Calculate Area
    Display "Program Complete"
End
The program displays a heading, does a calculation, then displays an end message. The process is quite clear, but the actual calculation process is

08 May 2012

String Functions in 'C' (#include<stdio.h>)

The 'C' language provides a number of functions which help when handling strings.

strcat(string1, string2);

This function concatenates (adds to the end of) string2 to string1.
char string1[30] = "First String", string2[15] = "Second String";
strcat(string1, string2);
printf("%s", string1); // will print First StringSecond String
strlen(string);

Use of Strings in 'C'

A string is defined in 'C' as an array (list) of characters.

char str[10];   defines a string variable of 10 characters.

When data is placed in a string variable, a null character is placed at the end of the data. It is possible to look for this null when processing the string, so that processing

05 May 2012

Loops with variable repetitions

'For' loops are controlled by a built-in counter and are used where the number of repetitions is known before the loops is executed. Some problems involve repetition based on a condition whose occurrence is unpredictable. For these problems,a pre-test loop is used with an appropriate condition.

04 May 2012

For Loops

Sometimes we need to write a program where a block of code needs to be repeated. Rather than write a code over and over, we use one of the Repetition Structures available in programming languages.

The simplest of these structures is the For Loop.

This loop is used when a set of instructions is to be repeated a predetermined number of times.