Monday, May 24, 2010

Can someone pls help me with C programming?

what the hell is meant by a function!..please explain in as lucid and perspicuous a language as possible.

Can someone pls help me with C programming?
all that you have been given sofar is true as far as what functions are. here is my lucid and perce.... whatever version





NB: anything you see any where in a computer application, programming or scripting language that has two brackets after it is a function





eg sum()----in ms excel


printf()-----in C language


count()-----in excel etc





when you write a c program like





void main()


{


int x=2;


int y = 4;


int z=x+y;


printf("%d",z)


}





you would have just used two functions


1. main();


2. printf();





so what the hell are they??? hmmmm?


well look at them like this.





A function is a group of statements with a name, and that performs a specific task.





e.g





printf() is a function whose name is print, and when called by that name, displays on the screen all that you put in between its brackets





main() is a function in every c program, that is called by the operating system, in order to execute your c program.





every faction has its job. but there are generally two types


1. standard functions


2. user-defined functions





standard functions are those that are shipped along with the programming language ready for use e,g printf() scanf() main(), fflush() etc.





user defined functions are those ones you create and use on you own.





e.g you can create a function whose name is add() which when called adds two numbers and displays the result. e.g when you type add(10,20) you see 30 on the screen.





so how do you create them?





first grab these when creating your own.





1. every function has name and you are the one who chooses it. e,g add





2. all function names are followed by brackets ()


e.g add()





3 a function may or may not need parameters to perform its task. e.g (in ms excel when you type sum(50,10) the 10 and 50 are parameters to the function sum()





4. a function may or may not return a value when called( ask me incase you need more on this)





5. to use a function call it by its name. eg add(2,5), printf("are you with me?").





structure


------------------





data type funtionName(parameter list)


{


implementation


}





e.g





void add(int x, int y)


{


int z=x+y;


printf("%d",z);


}





that is a faction created it id called add() it accepts two integer (int x, int y), add them ---int z= x+y, and displays the result---printf("%d",z);





so when you add(2,3)





2 will go in x


3 will go int y above





try this complete program





#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;





//creating the function





void add(int x, int y)


{


int z= x+y;


printf("%d",z);


}





void main()


{


add(30,10); //calling the above function


getch();


}
Reply:where u from star?.. Report It

Reply:good Report It

Reply:Hi friend,





It is not really any hell. It is a heaven. A function is any block of code that does "something". Ok, what's this something? Well, it may print your name, calculate average, calculate some formulae... What's the use, you may ask, saying I'll better do this in the program itself? This is the actual use: Suppose you need to print your name 100 times but not in a loop. (i.e., when the program starts you need to print, then after some x value is displayed you need to print.. like that...). So, you can't print in a loop. So, function helps you in this situation... You write a function PrintName() and call it whenever you need to print your name.. So, there's no need to type printf ("My name is ..."); 100 times. Instead you type PrintName (); 100 times.. This is a simple example.. A function does a lot more than this... Refer books on C for details...
Reply:You make a function when you need to use a specific code (instructions or commands) many times in your program. To understand it in a better way, imagine a human analogy about this: imagine if you want to tell somebody to sit down for five seconds then stand up for ten seconds then catch a ball before he throws it, and you want him to do that many times, would you repeat all instructions everytime, or you would prefer to "teach" him a gesture so he does all that if you clap for example!
Reply:a function is a group of statements that get called by name.





this function adds two numbers together...





int AddTwo(int a, int b)


{


return a+b;


}





functiona can already exist (in a library, for example) or you can make your own...

hibiscus flower

No comments:

Post a Comment