Hello i am new to c language i am unable to understand Functions in so any one help me to understand that topic. Thank you in advance.

Recommended Answers

All 3 Replies

Imagine you're in a large house, with several rooms. Each room is a function:

*you can only carry in *copies* of what you want, into each room, unless it's already been put there (ie, it's global in scope).

*you can only walk out of a room, carrying *one* item

*if the item you walk out with was created within the room, then the item will self-destruct right at the doorway as you walk out out of the room

*each room is organized so you can more easily do some type of work, in it. It has a natural purpose (sorting, searching, reading data, writing data, editing data, getting input from the user, etc.)

Hope that helps.

What you want to do is post up some code (the smaller and simpler the better), that you don't understand, or that doesn't work correctly, and ask specific questions about what you don't understand about that code. General questions like this, are too vague, and really require a chapter in a book, to answer thoroughly.

commented: that's kinda cool. never heard anyone put it like that before. +4

Thank you Mr Adak now i am understanding the topic of functions. first time i am posting a quest in forum like this thats why i dont know how to ask question as you guided me i will ask specific doubts where i am not able to understand thank you once again Mr Adak.

The easy was undersatnding the function is, by think of something like this well. If you have huge lot of code in main, then its a vewry bad programming practice. It is very important that we think before we code it.

As said before, if you a huge main function, that need to be broken down into several groups. And each group of statments will be indentifies through an name indentifer and the each group will be called with some input and function return some outputs.

Like make things clear, consider the following example

#include <stdio.h>

int sum( int, int);

int main()
{
    printf("The sum is - %d\n", sum( 1, 2) );
    
    getchar();
    return 0;

}

int sum(int a, int b)
{
    return (a+b);
}

/* my output

*/

As you can see, the ,first thing you need to do when you work on function is to declare a function prototype where you are making aware the compiler, that somewhere in the future the function sum wil defined and it will used. But make sure that you call the function after it has been declared. Otherwise you will get a error thrown up. And you can also see the how i'am calling the function with some parameter. Parameters or argumnts are something like an input to those function. I my case it integers. It takes those two ints and sums them and retruns them.

Hope that shold roughly should have given you an idea on what it is. No go aand write me program for the following spec.

** Write a function which takes three integers, which can perform the requested task such as add, mul and sub. The function prototype is as follow

int Maths(int num1, int num2, int choice)

If the choice is 1 do addition, if choice 2 sub and so on.

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.