Click to See Complete Forum and Search --> : having problems understanding functions in C, anyone care to explain me? =)
freedon
04-10-2002, 02:18 PM
in my C class for engineering, i'm having problems understanding how to make/put functions on the program
these are my teachers notes
http://ringer.cs.utsa.edu/~mmaltrud/CS2073/lectures2073/functions.htm
i already had 2 homeworks using fucntions and well.. the first one i got a 50 caz i did it wrong and in the second one a 70.. and i have a test on april 19th so i want to learn how functions are created
i kind of have the idea of what the idea is, but when doing a 'recall funtion' or 'return function' i just don't get i do't know where to put them or what variables to put
care anyone to spend time in teaching me ;)
thanks
arcane_III
04-10-2002, 09:20 PM
Your best bet is to look at over the notes well and check out the case study examples.
"return fuctions" as you call them are fuctions that return a value, they are functions that are not declared
void function name(parameters)
some examples of fuctions that have a return value are:
x = pow(x,2)
x = sqrt(y)
etc...
if you are familar with VB which has both procedures and functions then you can relate a void function_name( parameters )
declaration with a procedure(SUB), and relate any function that has a return value to the VB function.
In general functions come in handy when you want to use a function in place of a variable or in the place of a parameter.
Example:
Suppose you want to find the square root of 2, and print the result.
printf( sqrt(2.0) );
would accomplish this.
In this example printf is declared:
void printf( ... ) and does not return anything.
but sqrt() has a return value.
This return value is then passed to the printf function.
Well. Hope that helped a little, and didn't just confuse you more.
freedon
04-10-2002, 10:00 PM
i kindof did understand :D
so how about the variables and parameters that start in the begining of each part?
for example
main
{..
.
.
.
.}
num(int..., int ...)
{
.
.
.
.}
does that num(int, int) share the same way as you explained ?
mefisto3
04-10-2002, 11:15 PM
it is very simple
functions basically perform some operations (do something, whether it is a calculation or printing to screen or anything else). they can return a value but do not have to. they can also take parameters but dont have to.
for example (using Java syntax):
void print(String text)
{
System.out.println(text)
}
takes String text as parameter and outputs it to screen, it does not return anything (hence void infront of the function). can be called like this:
print("blah blah blah");
int add(int a, int b)
{
return a + b;
}
takes two integers as parameters (a and b) adds them together and returns the result (as an integer too). can be called like this:
int x = add(1, 2);
int random()
{
// generate random number x (no code included)
return x;
}
takes no parameters and returns a random number. can be called like this:
int myRandomNo = random();
Hope it makes it easier for u.
If u r having trouble with something as simple as functions then maybe programming is not for u.
freedon
04-10-2002, 11:28 PM
yes i understod better :)
i have to take at least one class of C :(
i don't hate it, but i'm just having problems
Originally posted by mefisto3
If u r having trouble with something as simple as functions then maybe programming is not for u.
i think that to fail right now is to improve in the future
:p (like i said, i study engeenering) not computer science.. now that would make me worry
Galen of Edgewood
04-11-2002, 10:57 AM
Originally posted by freedonX
does that num(int, int) share the same way as you explained ?
If I understand what you are asking, then yes.
Take this function as an example:
int numSum (int a, int b) {
/* This function returns that sum of a + b */
int sum;
sum = a + b;
return sum;
}
Simple enough of a function, right? :) (No, I don't personally code like that, but it'd probably confuse you if I coded the way that I code sometimes. :D)
Anyways.... Say you have a value that is to be the sum of two integers, and we'll call it sumOfInts. (Yes, I'm highly original...) You want sumOfInts to be equal to intA and intB. To do that, using the function numSum, you'd use the following syntax:
sumOfInts = numSum(intA, intB);
What happens is that 'intA' and 'intB' are sent into the function. The functions adds 'intA'(now known as 'a' inside the function) and 'intB' (now known as 'b' inside the function) and puts that value into the varible 'sum', which was declared inside the function. The function is exited at return and 'sumOfInts' is set to the value of what 'sum'.
I hope that helps you... If not, ask again in a differnt way and hopefully we'll clarify some more.
arcane_III
04-11-2002, 11:09 AM
Yeah most engineering programs's make you take a CS class or two.
Some of them even have you learn a little assembly language and build a old computer (8086) or something like that.
Galen of Edgewood
04-11-2002, 11:33 AM
Originally posted by arcane_III
Yeah most engineering programs's make you take a CS class or two.
Some of them even have you learn a little assembly language and build a old computer (8086) or something like that.
I had to take assembly for an 8086 for my CS degree... Never had to build one though! :eek: I did do several interesting things with IC's though. Fun things, though messy as all get out with the wires all over the derned place. :D
arcane_III
04-11-2002, 11:39 AM
Yeah, assembly was my favorite CS class. I kinda wish I had completed my Engineering minor and had gotten to do all that cool hardware stuff.
freedon
04-11-2002, 10:21 PM
thanks everybody! :)
i have a homework due tomorow, so i give it my best shot in trying yo do it right :p