|
|
|
| Web Hosting |
![]() ![]() |
Basic C Language, Functions |
Oct 27 2008, 03:40 PM
Post
#1
|
|
|
Premium Member Group: [HOSTED] Posts: 255 Joined: 17-July 08 From: Atlantis Member No.: 31,503 myCENTs:73.77 |
In a complicated and/or big C programs, a programmer might need to use the same peice of code more than one time. However repeating the code can be both time consuming and frustrating. Thats why we use functions.
Definition: A function is a small subprogram having its own name and datatype. It is used to carry out certain operations more than one time without the need of repetition. One of the functions we all know is the main functions; void main(). I will explain the void later. Syntax: Each function is composed of 3 parts:
The coding inside a function is also enclosed by '{' and '}', inside these you will find the piece of coding which might include declaration of variables,arrays or even calling other functions. Info type of the function: There are 5 info types:
Void means the function does not have a return value, an example of such a function is the void main() function. A return value is the value that is returned by the function. And by info type i mean type of that returned value. Examples: CODE void show() {char x; puts ("Input a character:"); scanf("%c",&x); printf("You have inputed %c",x); } That function is a void function because it did not return any value, things printed on the screen are not considerd as returned values. What it does is it asks for you to input a character, once you do that it takes it ,stores it in x and then shows it and that it. CODE char getcharacter() {char x; puts ("Input a character:"); scanf("%c",&x); return x; } void main() {char y; y=getcharacter(); } Here the function getcharacter(), which is the same as the predefined getch(), asks for a character and returns it to the main program. If you are wondering, in the main program you could've used x again it doesn't affect the function. x inside that function is considered a local variable, not a global variable. I will explain this next. Types of variables:
CODE char name=a; <--- global variable void main() { printf("%c",name);} That code will display 'a' on the screen. Some might be wondering what happens if a global and local variable have the same name, it is easy. If you have a global variable x=1, and a local variable inside a function x=2, the function will use 2. It searches for a local variable before going for global. CODE x=1; void localx() {x=2; printf("%d",x); } void globalx() {printf("%d",x); } void main() {localx(); globalx(); } First one will display 2, the second will display 1. Parameters: the parameters are included inside the '()' in a function, when no parameters are needed we leave them empty. An example is the functions i used before. These are called dummy parameters.To use a parameter you have to include its data type in the declaration of a function. CODE void max(int a,int b) {if (a>b) {printf("%d is bigger",a); else printf("%d is bigger",b); } void main() { max(1,2); } This program will print the max of the 2 numbers inputed as parameters, which in this case is 2. Note: the 'return' used at the end of each function ends it, any commandes after it are ignored. Small program:
CODE #include <stdio.h> #include <conio.h> int getmax(int number, int max) {int x=number, y=max; if (x>y) return (x); return (y); } int getmin(int number, int min) {int x=number, y=min; if (x<y) return (x); return (y); } void main() {int x,min=0,max=10000; do { puts ("Input positive integer, negative to stop:"); scanf("%d",&x); if(x>0) {min=getmin(x,min); max=getmax(x,max); }} while(x>0); printf("Max is %d \n Min is %d",max,min); getch(); } This program is a simple program that shows you how parameters and return work. CODE #include <stdio.h> #include <conio.h> int getmax(int number, int max) {int x=number, y=max; if (x>y) return (x); return (y); } int getmin(int number, int min) {int x=number, y=min; if (x<y) return (x); return (y); } Here i used predefined libraries(note those were needed in previous programs), i also declared my functions getmin and getmax which both use 2 integer parameters and return an integer value. CODE void main() {int x,min=0,max=10000; do { puts ("Input positive integer, negative to stop:"); scanf("%d",&x); if(x>0) {min=getmin(x,min); max=getmax(x,max); }} while(x>0); printf("Max is %d \n Min is %d",max,min); getch(); } I declared min as 0 because that is the lowest positive integer, as for max being 10000 you can put any high number you want. Notice i used the do while loop not the while loop because we have a condition that needs to be checked after the user inputs an integer. However since i added an if statement to check if x is positive you could've used the while loop function. Every time a positive x is entered, it is inserted along with max in getmax function and their max is returned , and it is inserted with min in getmin and their min is returned. In the end, the max and min numbers are stored in min and max and are printed out. The usage of functions in C programming is very broad and hard to even begin to explain all its features in one tutorial so i'll have to stop now, I'll make another tutorial sometime to explain the usage of prototypes and recursive functions. Enjoy =) |
|
|
|
Oct 27 2008, 06:58 PM
Post
#2
|
|
|
Way Out Of Control - You need a life :) Group: [MODERATOR] Posts: 2,292 Joined: 16-August 05 Member No.: 7,896 myCENTs:99.39 |
Very basic, indeed. However, who knows, maybe some people would be happy finding this here.
|
|
|
|
Oct 28 2008, 01:39 AM
Post
#3
|
|
|
Colonel Panic Group: [MODERATOR] Posts: 2,939 Joined: 25-March 05 From: Toronto, Ontario, Canada Member No.: 3,233 myCENTs:38.79 |
This tutorial is very useful for people who start with programming in C like me. However, in my case, I've done Java programming so most of these concepts would just shift around.
xboxrulz |
|
|
|
Oct 28 2008, 08:08 AM
Post
#4
|
|
|
the Q Group: [HOSTED] Posts: 1,207 Joined: 13-July 05 From: Lithuania, Vilnius Member No.: 7,059 myCENTs:90.94 |
I agree that there are o lot of tutorials which really are very good for basic stuff, people learning C, which are very understandable, but what I miss is that there aren't a lot of good tutorials on C for advanced programmers which would be very well written to be understandable, so usually you need to take a big book for C, which can be to much.. Even though I know why most of tutorials are basic one and good for people starting to learn C or anything else, because it's easy to write them, even I could write a lot of tutorials on C,C++, C#, PHP, MySQL, Oracle, Java and etc. and after section 10 I could write, if you want to learn more read that book which is like a bible for C programmers
|
|
|
|
Oct 29 2008, 12:56 PM
Post
#5
|
|
|
Premium Member Group: [HOSTED] Posts: 268 Joined: 30-June 07 From: UK Member No.: 23,045 myCENTs:34.59 |
At my university we have a whole host of tutorials, exercises and reams of information that can be accessed by anyone else who studies here. For example, my lectures in programming and computer architecture have associated slideshows, each of which is available by anyone else at the university who wants to learn about programming. It may not be particularly easy to uncover the links, though, as many are buried away in the personal sites of the lecturers, but if you happen to know someone who does a subject that you have a casual interest in then it's easy enough to find out who the lecturers are and therefore the relevant links so that you can have a look in to the course as well.
As I'm in my first year here there is no prior knowledge of programming required or assumed (for an electronic engineer) meaning that the C lectures I have start from the very basics. I've already had a few friends (none of which study the same course...nor in fact anything really that similar to electronic engineering) ask me about it. A few quick clicks later and they're being taught C in their spare time (well...maybe not "being taught", but at least have access to resources to help them teach themselves). The same applies to most other courses and, apparently, quite a few other universities (at least in the UK) according to other friends of mine. Why do I mention this? If a university that you're studying at has a computer science department, an electronic engineering department, or something similar to that, then there's at least some chance that you'll be able to track down a way of teaching yourself C (or whatever other language you plan on learning). Or you can find yourself a decent book on the subject. Other than that, nice little tutorial, khalilov. I haven't quite reached the lecture about functions yet (and I've no previous C experience, but dabble in programming in general), but by reading through it I'm sure it'll help prep me for it. |
|
|
|
Nov 1 2008, 06:58 PM
Post
#6
|
|
|
Sparkx Group: [HOSTED] Posts: 371 Joined: 11-October 06 From: Dana Point, CA, USA Member No.: 16,496 myCENTs:0.61 |
Nice tutorial. Basic but it could be real useful for programming in general. I know that Java and even PHP has a similar structure of functions. I’m at about the same point that xboxrulz is at. I have learned Java and am going on C/C++ now. I think the hardest part about switching would probably be the fact that C++ seems to have a different variable setup then Java such as signed vs. unsigned est. (a little funny that the hardest parts for me are the basics while advanced 3D is easier to understand). I have only been able to find a handful of good online tutorials for advanced C or C++.
I noticed that the C code looks very similar to C++. Does anyone know what the major difference is? Is it like java vs. JavaScript and they are nothing alike or is if you know one you pretty much know the other or are they different versions of the same language? I’m fairly new to programming other then a having good knowledge of PHP, MySQL and Java (which all seemed to be aimed toward beginners). Thanks, Sparkx |
|
|
|
![]() ![]() |
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:
Similar Topics
| Topic Title | Replies | Topic Starter | Views | Last Action | |||
|---|---|---|---|---|---|---|---|
![]() |
8 | bridenhosen | 1,738 | 5th January 2009 - 01:38 PM Last post by: iG-Ernesto Gramcko |
|||
![]() |
7 | flute | 304 | 5th January 2009 - 04:07 AM Last post by: santaclaus |
|||
![]() |
8 | Grafitti | 16,011 | 2nd January 2009 - 08:45 AM Last post by: Guest |
|||
![]() |
7 | solanky | 3,315 | 19th December 2008 - 09:34 AM Last post by: iG-preety sharma |
|||
![]() |
11 | Propeng | 1,374 | 17th December 2008 - 10:40 PM Last post by: yordan |
|||
![]() |
12 | yungblood | 3,289 | 17th December 2008 - 03:53 PM Last post by: ryantommo |
|||
![]() |
11 | TheCapo | 448 | 13th December 2008 - 01:07 PM Last post by: tek3D |
|||
![]() |
28 | bigd1 | 4,907 | 13th December 2008 - 12:47 PM Last post by: tek3D |
|||
![]() |
17 | l337 Nurse Pedestrian | 9,010 | 12th December 2008 - 02:52 AM Last post by: iG-biswarup ghosh |
|||
![]() |
11 | ViRuaL | 2,360 | 10th December 2008 - 10:14 PM Last post by: iG-nick |
|||
![]() |
6 | zemon1 | 178 | 30th November 2008 - 06:17 AM Last post by: Darasen |
|||
![]() |
9 | khalilov | 311 | 14th November 2008 - 03:12 PM Last post by: Quatrux |
|||
![]() |
2 | veerumits | 185 | 24th October 2008 - 06:36 AM Last post by: Quatrux |
|||
![]() |
6 | turbopowerdmaxsteel | 640 | 23rd October 2008 - 10:53 PM Last post by: Moudey |
|||
![]() |
9 | nunocordeiro | 427 | 11th October 2008 - 11:54 PM Last post by: Quatrux |
|||
Expand / Collapse Navigation



Oct 27 2008, 03:40 PM





