i'm an 2nd year IT student i'm having a hard time understanding functions can anyone please explain to me in layman's terms what are functions,kinds of functionsand i would really apprieciate it if you could give me some basic function oriented program in C

Recommended Answers

All 2 Replies

Not to be an asshole, but:

click here! :D

Too bad that doesn't work, but you can figure it out. Examples are abundant on the web.

Well, let me make it as brief note, but not too much information. All these should be researched on the web. There are plenty notes on these concepts on the we. But anyway.

A function can be thought of a group of statements which does a specific job for you. So for example, if you take remote control, the controller itself has many different functions in it. Each button on the remote does some specific job. But the whole device it self is called as a remote controller.

In the same way, functions are meant to designed or implemented to do some specific job. These can be function could call many other different functions to get their job done. You might also come cross a word “divide-conquer”. There are two different sort of function which you could come across user-defined function and library functions. The following is the prototype for an declaring a function in C

<return type> function_name( arguments )
{
     <statement 1>
     <statement 2>
     <statement 3>
      .
      .
}

Example:

const double Cal_Square( const double num )
{
      return ( num * num );
}

int main()
{
      printf( “%f”,  Cal_Square( 10.8 ) );
      return 0;
}

As you can see from the above example, Cal_square is function which is called in main to get the square for a given num. The function calculates the square and returns the results back to main and result is printed out.

The function can extended to do many other things, but not just the square. In the above example the contains a printf statement. The printf its self is a function which we are calling get a job done for us. What job, to print the result to the screen. Here printf is a library function which is been already implemented for us.

As you can see the user-defined function are those functions which are implemented by the users. But the library functions are those which are already implemented and there are ready to be used at anytime. Once more thing which need to be noted is that function prototype. Which should be declared before the function is called in the file. In the above the cal_square is been declare and defined before the main. So the compiler known about the function cal_square before its been called in the main. It you miss that, you will get an error message from the compiler.

Hope I gave you much information; there is more than this which you need to know to understand the function. But this I more than enough to get you started. Post if you find any problems. But don’t expect us to sit and spoon feed you ;)

ssharish

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.