Question:
how to make function using C.?
MyYOu31
2009-12-07 23:40:42 UTC
i have a simple program using C language...

here's my code:

#include

struct num
{
int x;
int y;
int ans;
};

void main()
{
struct num* res;
struct num p;
res = &p;

printf("\nEnter 1st Num:\n");
scanf("%d",&res->x);
printf("\nEnter 2nd Num:\n");
scanf("%d",&res->y);

res->ans = res->x + res->y;

printf("\nThe Result is: %d \n",res->ans);
}

this code is okay but i want to make a function using this code and i do not know how to make it...
can you help me..?

Thanks in advance.
Four answers:
kustom_ace
2009-12-08 00:11:27 UTC
Ok so to make a simple function you need three things..

1) you need to declare it before main or in a seperate header file much like you did with the struct.

2) you need to make the funtion, it can be written after main or put in a seperate header file.

3) call the function in main and pass in any variables that you are using.



When I first started I found it made the most senst for me to build my function first and I can think of what parameters to use and such. Lets make a function tha adds to integers and returns the result. So what is going to happen is that your going to provide the function with numbers to use when you call it in main. Your going to do something with these numbers, in this case add them, and then return the result back to main so you can use it. C



First we tell the program what kind of data we are returning back to main by typing the type out first (i.e. int, double, char ect...) ths case its an int.

Next we name the function, lets call this one GetSum

Then we tell the program what input we are expecting to pass in by providing the type and a name for each variable. Then we can write our function and return a value. Heres what this would look like...



int GetSum ( int firstValue, int secondValue )

{



int sum = 0; // initialize the sum value for when we add the two numbers being passed in



sum = firstValue + secondValue; // add two numbers



return sum; // this returns the sum we just got so you can use it in main

} // end function



You would put this part after main



If you dont declare this function before main, your program will not know what you are referring to when you use it. It would be like you trying to access your struct without declaring it before your main function.

Your declaration looks excatly like the first line of your function except that you add a semicolon at the end like a statement... It looks like this...



int GetSum ( int firstValue, int secondValue ); // declaration before main



now your funtion is ready to use. All you need to do is call it in main. Remember that it returns a value and we also need to pass in two numbers for it to work. If you look at the declaration what is first? Its the int, its what the function returns, then the name, then the values you pass in, in this order. Think about this when you use it in main.



lets say you have two values x and y that you want to add. And you want to get the sum of these from your function. This is how you'd use it in main...



sum = GetSum( x, y );



see how the order goes in main. Compared to how its declared?



sum = GetSum ( x, y );

int GetSum ( int firstValue, int secondValue)



Now you have all you need to write a basic function. You can pass in as many variables as you want and any type you want. But you can only return one thing for each function. There are ways around this but for now just know you can only return one thing at a time.

I'll code how the overall structure so you can get a feel for how it looks in a program...



#include "stdio.h"

struct num

{

int x;

int y;

int ans;

};



int GetSum ( int firstValue, int secondValue );



int main ( void )

{

struct num P;



P.x = 1;

P.y = 5;

P.ans = GetSum ( P.x, P.y ); // use funtion to get sum



printf ("The Result is: %d",P.ans); // print sum out

return 0;

}// end main



int GetSum ( int firstValue, int secondValue )

{

return ( firstValue + secondValue );



// returns the result of adding these two numbers.

// NOTE: you can use the way I did this before or this way it doesnt matter

// this way there is just less to write and it looks a little cleaner

}



Hope this helps. And that you really understand what a function is doing. Good Luck!

kustom
Kelp
2009-12-07 23:54:43 UTC
main's a function just like any other. here's a simple function:



int addTwoNumbers(int a, int b){

return a+b;

}

void main(){

int x, y;

printf("\nEnter 1st Num:\n");

scanf("%d", x);

printf("\nEnter 2nd Num:\n");

scanf("%d", y);

int z = addTwoNumbers(x, y);

printf("\nThe Result is: %d \n",z);

}



Hope that helped.
single_minded
2009-12-07 23:56:20 UTC
A function in C language is a block of code that performs a specific task. It has a name and it is reusable i.e. it can be executed from as many different parts in a C Program as required. It also optionally returns a value to the calling program



So function in a C program has some properties discussed below.



Every function has a unique name. This name is used to call function from “main()” function. A function can be called from within another function.



A function is independent and it can perform its task without intervention from or interfering with other parts of the program.



A function performs a specific task. A task is a distinct job that your program must perform as a part of its overall operation, such as adding two or more integer, sorting an array into numerical order, or calculating a cube root etc.



A function returns a value to the calling program. This is optional and depends upon the task your function is going to accomplish. Suppose you want to just show few lines through function then it is not necessary to return a value. But if you are calculating area of rectangle and wanted to use result somewhere in program then you have to send back (return) value to the calling function.



C language is collection of various inbuilt functions. If you have written a program in C then it is evident that you have used C’s inbuilt functions. Printf, scanf, clrscr etc. all are C’s inbuilt functions. You cannot imagine a C program without function



example:

What is Function in C Language?

79

rate or flag this page



By rajkishor09



A function in C language is a block of code that performs a specific task. It has a name and it is reusable i.e. it can be executed from as many different parts in a C Program as required. It also optionally returns a value to the calling program



So function in a C program has some properties discussed below.



*



Every function has a unique name. This name is used to call function from “main()” function. A function can be called from within another function.



*



A function is independent and it can perform its task without intervention from or interfering with other parts of the program.



*



A function performs a specific task. A task is a distinct job that your program must perform as a part of its overall operation, such as adding two or more integer, sorting an array into numerical order, or calculating a cube root etc.



*



A function returns a value to the calling program. This is optional and depends upon the task your function is going to accomplish. Suppose you want to just show few lines through function then it is not necessary to return a value. But if you are calculating area of rectangle and wanted to use result somewhere in program then you have to send back (return) value to the calling function.



C language is collection of various inbuilt functions. If you have written a program in C then it is evident that you have used C’s inbuilt functions. Printf, scanf, clrscr etc. all are C’s inbuilt functions. You cannot imagine a C program without function.

C Language



Structure of a Function



A general form of a C function looks like this:





FunctionName (Argument1, Argument2, Argument3……)

{

Statement1;

Statement2;

Statement3;

}



An example of function.



int sum (int x, int y)

{

int result;

result = x + y;

return (result);

}



Some links which you should explore:

http://www.cprogramming.com/function.html

http://www.cplusplus.com/doc/tutorial/functions/
weissler
2016-10-29 08:41:44 UTC
the finished C I/O equipment is secure below cstdio in C++. whether, utilising that's not embodying the "merchandise oriented spirit" and is considered undesirable sort by using maximum C++ coders.


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