Question:
Okay, I know this is a noob question:?
anonymous
2014-03-12 18:12:51 UTC
So I just started learning c/objective-c, and I already know some python. In python there is the def function. I was wondering if there was any kind of equivalent function or object in c or objective-c, which I am completely new to. I was thinking about making a simple application that can calculate areas in various shapes. I did something similar in python and the code looked like:

def square(side):
return(side**4)

def circle(radius):
return(6.283058*radius)

def rectangle(base, height):
return(base*height)

def trapezoid(a, b, h):
return(a+b/2*h)

def ellipse(a, b):
return(3.141592*a*b)

and so on

I was wondering if there was any way to do achieve the same thing in c/ objective c.
Thanks in advance
Four answers:
Andrew S
2014-03-12 18:28:27 UTC
Function definition is implied by the context, there is no specific keyword for it. You'll also need to specify types for both the return value and any parameters, so



def square(side):

return(side**4)



might become



int square(int side)

{

   return side * side;

}



Assuming it is in fact supposed to return the area like the others rather than the hypercube.



Get a decent book on C and work through it - this is stuff that would be covered in the first couple of pages. By leaving Python you are moving away from scripting languages designed for convenience: you can't learn highly structured languages such as C in a piecemeal approach.
husoski
2014-03-13 01:32:25 UTC
Sure. I don't know Objective C, but it is reportedly a superset of C (unlike C++) such that every legal C program is also legal Objective C program.



C functions look much like your main() function. Define them before use and you won't have to use prototypes.



Note: The square function should be side**2 if you want area, or side*4 if you want perimiter, but side**4 is an oops! Also, C does not have an exponentiation operator (** in Python) so you perform small integer powers by multiplying



double square(double side) /* Python: def square(side): */

{

return side*side; /* Python: return side**2 */

}



C is strongly-typed so you must declare the data types for both arguments and the return value.



Second note: Python's return statement is a statement, not a function, so you don't need parentheses around values. The same is true for C, though you will see a lot of code, particularly old Unix code, using return(0); instead of return 0;. Speaking of parentheses, you do need them in your trapezoid area calcuation (both C and Python versions) to add a and b before dividing and then multiplying: (a+b)/2*h.



I will sometimes write simple expression functions like this as one-liners:



double trapezoid(double a, double b, double h) { return (a+b)*0.5*h; }



Multiplying by 0.5 is faster than division by 2. Also, "double" is the C type corresponding to "float" in Python. There is a "float" type in C, but avoid it unless you have a Real Good Reason not to use double.
?
2014-03-13 01:31:00 UTC
def isn't a function in python, it's a keyword or operator that lets the compiler know a function follows;



In c/c++ (not sure about objective c)



A function looks the same as a variable declaration except you put the return data type before the variable



int function() {return integer}



Or use void if you don't want to return anything



You should also check into overloading functions



Hope that helps!
Afaan Bilal
2014-03-13 01:28:29 UTC
In C, it's almost as simple.

double square(double side)

{

return side * 4;

}



double circle(double radius)

{

return 6.283058 * radius;

}



double rectangle(double base, double height)

{

return base * height;

}



double trapezoid(double a, double b, double h)

{

return ((a + b) / 2 ) * h;

}



double ellipse(double a, double b)

{

return 3.141592 * a * b;

}



Hope it helps!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...