# Assigning values to functions (Newbie)

• 10-06-2011
turke92
Assigning values to functions (Newbie)
I have just started learning C and I already have some issues.

Code:

```#include <stdio.h> int binom(int n, int k) { int result=1; int i; if(n == k) return 1; if(k == 0) return 1; for(i=n;i>n-k;i--) result=result*i; for(i=1;i<=k;++i) result=result/i; return result; } int main(void) { int bnm; int n, k; for(n=0;n<10;n++){ for(k=0;k<=n;k++){ bnm=binom(n,k); printf("%ld ",bnm); } printf("\n"); } return 0; }```
I understand that variables "n" and "k" from "main" and "binom" function are different but does the line:
Code:

`bnm=binom(n,k);`
mean it will assign "n" value from "main" function to "binom" function? (Same applies to "k")
I know this is stupid question, and yes I googled it but I havent found a decent answer. Thanks.
• 10-06-2011
MK27
Quote:

Originally Posted by turke92
does the line:
Code:

`bnm=binom(n,k);`
mean it will assign "n" value from "main" function to "binom" function? (Same applies to "k")

What you would say in programming-speak is: n and k are submitted as arguments to binom, and the result is assigned to bnm.

Quote:

I know this is stupid question, and yes I googled it but I havent found a decent answer. Thanks.
If you are unsure what something does, get as many hints as you can and then start playing around to see what happens. If you have a theory about what you think is happening, try to prove and disprove it.

Finally: LEARN TO INDENT. STARTING NOW. There are a bunch of examples in C for various "indent styles" here:

Indent style - Wikipedia, the free encyclopedia

Pick one (variants on K&R or Allman are most popular) and use it! This is an important part of keeping code comprehensible to yourself and others.
• 10-06-2011
turke92
Thanks!
One more question. I don't understand the logic behind this... In the "main" function we keep incrementing n and k at the same rate until n=9.
Shouldn't that keep the ratio of n and k equal (n==k) and therefore always return 1?
I know I got something wrong but I'm not sure what...
• 10-06-2011
CommonTater
Quote:

Originally Posted by turke92
Thanks!
One more question. I don't understand the logic behind this... In the "main" function we keep incrementing n and k at the same rate until n=9.
Shouldn't that keep the ratio of n and k equal (n==k) and therefore always return 1?
I know I got something wrong but I'm not sure what...

If you would learn to indent your code properly you would actually be able to see what is happening...

Code:

```#include <stdio.h> int binom(int n, int k) {   int result=1;   int i;   if(n == k)     return 1;   if(k == 0)     return 1;   for(i=n;i>n-k;i--)     result=result*i;   for(i=1;i<=k;++i)     result=result/i;   return result; } int main(void) {   int bnm;   int n, k;   for(n=0;n<10;n++)     {       for(k=0;k<=n;k++)         {           bnm=binom(n,k);           printf("%ld ",bnm);         }       printf("\n");     }   return 0; }```
See it now? the k loop is inside the n loop... for each value of n you call binom with every value of k.
• 10-06-2011
turke92
So that means that after testing if n<10 is true the program solves the "binom" function and prints it, increments n, goes into a new line, and after all that it looks if k<=n and does the orders in its brackets? I'm sorry if you find it difficult to understand my stupid question regarding this simple issue.

EDIT: "See it now? the k loop is inside the n loop... for each value of n you call binom with every value of k." I think I speak english well but this statement confused me and hence my even more confusing question.
• 10-06-2011
CommonTater
Here try this little code snippet...
Code:

```#include <stdio.h> int main (void)   { int i, j;     for (i = 0; i < 5; i++)       for (j = 0; j< i; j++)           printf( " i = %d, j = %d \t",i,j);   getchar();   return 0; }```
Get it now?
• 10-06-2011
turke92
So initialization of "j" (j = 0) is made every time if the statement "j < i" is false and initialization of "i" is done only once?
• 10-06-2011
CommonTater
Quote:

Originally Posted by turke92
So initialization of "j" (j = 0) is made every time if the statement "j < i" is false and initialization of "i" is done only once?

Initialization is done only at the beginning of a for() loop... any for() loop, every for() loop...

You have two loops, one running inside the other...
Look at the way the numbers count up...

Now apply that to your original code...
• 10-06-2011
turke92
I got it but I haven't said what I meant right.
I thought that the value of j stays saved if we get back to the first loop, I didn't know that after we get back to the second loop initialization is made again.

Thanks for helping!
• 10-06-2011
CommonTater
No j is not saved because you don't go back to the outer loop until the inner one is finished and each time you re-enter the inner loop it's starting over... the numerical printout from my example should have demonstrated that very clearly.
• 10-07-2011
turke92
Like I said, I got it now, but didn't know this when I encountered the code on the 1st post.