0

I am using a function to print numbers from 1 to N in C.But problem is that,the variables must be defined internally and there should be no use of loops.The function is being called by name.In short, function should print a digit each time it is called. I am unable to code the program since the variable is declared inside the function. Please tell me that is it possible to do so.

For exmple:To print numbers from 1 to 5. but it is printing only '1' 5 times.

#include <stdio.h>
void main()
{
repeat_function();
repeat_function();
repeat_function();
repeat_function();
repeat_function();
return 0;
}

void repeat_function()
{
int a=1;
printf (%d,a);
a = a + 1;
}
Subhrut Taori
  • 27
  • 1
  • 2
  • 11
  • 2
    What does defined internally mean? – nicomp Mar 09 '18 at 12:59
  • Pass a variable by reference. So the scope of the variable is in a different function. – clamentjohn Mar 09 '18 at 13:02
  • @ClamentJohn There is no pass by reference in C. – mch Mar 09 '18 at 13:05
  • @mch "C does not support implicitly passing a variable by reference" --you need to explicitly create a reference (with `&`) before calling the function and explicitly dereference it (with `*`) in the function – clamentjohn Mar 09 '18 at 13:11
  • @nicomp : defining internally means i can only declare the variables inside the function. I cannot use parameter passing through the function. – Subhrut Taori Mar 09 '18 at 13:13
  • @ClamentJohn so you are passing a pointer to the function? How is this better than passing the value directly? A pointer is also a variable and needs memory. – mch Mar 09 '18 at 13:14
  • @mch Yes but I'm only declaring the pointer once and keep modifying the same value it points too. I won't use any more memory. If I keep declaring a new `int` I keep eating up my memory. The program won't be scalable. It'll work on your Windows machine but what if you wish to reuse it, say in ARM? Write scalable code. Period. – clamentjohn Mar 09 '18 at 13:17
  • @ClamentJohn now I am very interested what you mean. Passing a pointer to a recursive function will also add up on the stack. I would write both versions like this: https://ideone.com/bE63VU , so why is the pointer version better than the value version? – mch Mar 09 '18 at 13:25
  • 1
    @ClamentJohn "Write scalable code. Period." Absolutely wrong. – nicomp Mar 09 '18 at 13:29
  • @ClamentJohn calling a function will create a stackframe that consumes a particular amount of memory no matter what. adding a single int or pointer sized value to that has only linear impact on the stack size – Nefrin Mar 09 '18 at 13:35
  • No. Passing a reference will not increase the stack as much as passing everything by value.Please refer to https://stackoverflow.com/questions/26552481/which-is-faster-pass-by-reference-vs-pass-by-value-c . Also please read the comments. – clamentjohn Mar 09 '18 at 13:35
  • 1
    did you read the answer @ClamentJohn? passing 4 or 8 bytes does not make a huge difference. on top of that passing a single int on a 64 bit machine copys 4 bytes whereas passing a pointer will copy 8 bytes so passing an int wll even be ever so slightly faster in this particular case – Nefrin Mar 09 '18 at 13:39
  • I might be wrong. So I asked a question on SO. I didn't know about stackframes. – clamentjohn Mar 09 '18 at 13:46
  • well to summarize stackframes: to remember where to return to afer a subroutine a stack is used. each call creates a stack frame that contains passed parameters, a caller context and most importantly the return adress (the address of the instuction that folows the jump to subroutine instruction) – Nefrin Mar 09 '18 at 14:02

3 Answers3

2

The simplest approach is a function that takes a number as a parameter. This function then calls itself with parameter - 1 if the parameter is greater than 1. Finally it prints the value of the parameter and returns.

EDIT As you are required to not pass a parameter or use a static variable you could use a global variable.

#include <stdio.h>

int value = 10;

void recursion() {
    if (value > 0) {
        int out = value;
        value--;
        recursion();
        printf("%d", out);
    }
    return;
}

int main()
{
    recursion();
    return 0;
}

This is not good style but it works and fullfills your requirements.

EDIT2

Using your code example I come to this solution using a global variable:

#include <stdio.h>

int value = 1; // <-- global variable

void main()
{
repeat_function();
repeat_function();
repeat_function();
repeat_function();
repeat_function();
return 0;
}

void repeat_function()
{
printf (%d,value);
value += 1;
}
Nefrin
  • 338
  • 3
  • 11
  • thanks for your answer but I am not allowed to pass any variable to a function. – Subhrut Taori Mar 09 '18 at 13:16
  • thanks but still 'Edit2' it will not work as condition given to me does not allow to use global variable . – Subhrut Taori Mar 09 '18 at 14:01
  • well then there is not a simple solution I know of. you should read up on the scope of variables. a variable created inside a function will be destroyed as soon as the function returns. this relates to the stackframe issue discussed below your question. local variables of functions are stored in its stackframe which is cleared after the function returns. Would using a file as temporary memory be an option? – Nefrin Mar 09 '18 at 14:07
1
void printMe(int num) {
    if (num == N+1) { return; }
    printf("%d", num);
    printMe(num+1);
}
nicomp
  • 4,344
  • 4
  • 27
  • 60
0

You could use static variable like this :

static int i = 1;

Static variables are only declared once.

You could declare global variable too. Global variable is simply a variable declared outside function. Those function can be used in every sub_scope.

Or you could do like so :

#include <stdio.h>

int ft_loop(int i, int limit) {
    printf("i = %d", i);
    if (i > limit)
        return 0;
    ft_loop(i++, limit);
}

int main()
{
    ft_loop(1, 30);

    return 0;
}
Jeremy M.
  • 1,154
  • 1
  • 9
  • 29
  • This will increase your stack size. You're declaring num at every function call. Remember it's being called recursively. – clamentjohn Mar 09 '18 at 13:05
  • What do you mean ? – Jeremy M. Mar 09 '18 at 13:06
  • When every you call `ft_loop` you are declaring two new `int` in your new scope. This means the program asks for more memory. This code of yours will work, but you are not thinking like a C programmer, where memory is `everything` – clamentjohn Mar 09 '18 at 13:14
  • @ClamentJohn If memory is everything, then code in assembly. – nicomp Mar 09 '18 at 13:18
  • Ok i understand. But you'r wrong. I'm not declaring two new int. I'm just look to same memory zone each time. – Jeremy M. Mar 09 '18 at 13:19
  • @nicomp Yes I do. Thanks for the support! – clamentjohn Mar 09 '18 at 13:20
  • thanks but I am given challenge not declare any static or global variable – Subhrut Taori Mar 09 '18 at 13:21
  • @JeremyM. You are passing the value and two new variables are declared at every scope. `You're not looking at the same memory zone`. – clamentjohn Mar 09 '18 at 13:22
  • @ClamentJohn if you live "memory is everythig" you probably compress any data you put in memory. I as a customer would certainly not buy an ambedded system or software for a microcontroller from you as they are probably hogged with compression at any time. ;) – Nefrin Mar 09 '18 at 13:48