This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Returning Values | |
The return statement terminates the execution of a function and returns control to the calling function usually main(). | |
The following program demonstrates functions returning a value by passing 3 floating point numbers and calculating the average and returns the answer. | |
*/ | |
#include <stdio.h> | |
float gradeAvg(float test1, float test2, float test3); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Passing Arguments | |
When you pass a variable from one function to another, you are passing an argument from the first function to the next. The recieving function recieevs the parameters from the function that sent the variables. | |
Methods of Passing Arguments | |
The variables you want to pass go inside the parenthesis of the function call () and also in the recieving function. | |
You pass arguments from function to function in 2 ways, by value and by address, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Functions | |
In a lot of textbooks, the programs you see are usually brief (20 - 30 lines) however, in the real world, programs are much much longer and therefore it helps a lot if you can break your code into sections. Doing so ensures that it'll be much more easier to write and maintain your code. | |
Breaking programs into smaller functions is called structured programming | |
Now the following example program is a simple demonstration of the difference between global and local variables. | |
*/ | |
#include <stdio.h> | |
int g1 = 10; // This is a gloabal variable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Structures | |
In programming, if you want to store multiple values of a certain data type (integer, character, floating point etc), you use an array however, if you want to store data consisting of different kinds of data types, you can use a structure. | |
Here's how you would write it: | |
struct student { | |
char name [20]; | |
int roll; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Heap Memory | |
/* | |
The heap is a collection of unused memory in the computer. You usually want to access the heap because your program will need more memory than you initially defined in the variables and arrays. The only way to access heap memory is through pointer variables. | |
The heap anables your program to use only as much memory as it needs. When the user needs more memory, your program can allocate the memory. When the user is fisnished using the certain amount of memory, you can deallocate the memory, making it available for other tasks that might need it. | |
How do you allocate the heap? There are two functions you need to know: | |
malloc() --> Allocates Heap memory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Arrays and Pointers | |
/* | |
An array name is nothing more than a pointer to the first element in that array. It is important to mention that array names are not exactly pointer variables but rather pointer constants. | |
As you know, if you want to retrieve a value from an array, all you have to do is write it like this: | |
printf("The first array value is %d.\n", vals[0]); | |
but you can also write it like this: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Pointers | |
/* | |
Inside your computer is a bunch of memory which holds your program as it executes as well as the variables you declare. Just as your house has a unique address, so does a memory location. | |
Here are the pointer operators: & --> Address of operator | |
* --> Dereferencing operator | |
Here's how you define an integer and floating point variable: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Bubble Sort | |
The order you put values in can be either ascending (low to high) or descending (high to low) | |
In a bubble sort, the sort's float up the list each time a pass is made through the data. | |
The program below generates 10 random numbers and then sorts them | |
#include <stdio.h> | |
#include <stdlib.h> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Searching Arrays | |
/* | |
One important use of arrays is filling them with values that you don't have initially and that the user has to enter. | |
Now the following program below takes an id number from the user and then checks the ID against a list of customers in the database. If the customer exists, it uses that array element to check their current balance, and warns the user if the balance is more than 100. | |
#include <stdio.h> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Reviewing Arrays | |
/* | |
Remember: An array is simply a list of items with a specific name. An array of characters can only have characters. An array of integers can only contain integers and so on | |
To define an array, you must add brackets after the name and specify the max number of elements you'll put inside the array. | |
ex: char name[6] = "Italy"; |
NewerOlder